From 8647acf07c560d46172bd704b4d0fa95fbbca146 Mon Sep 17 00:00:00 2001 From: dim Date: Sun, 19 Feb 2012 23:35:56 +0000 Subject: [PATCH] MFC r231057: Add a WITH_CLANG_EXTRAS option for src.conf(5), disabled by default, that builds the following additional llvm/clang tools: - bugpoint - llc - lli - llvm-ar - llvm-as - llvm-bcanalyzer - llvm-diff - llvm-dis - llvm-extract - llvm-ld - llvm-link - llvm-mc - llvm-nm - llvm-objdump - llvm-prof - llvm-ranlib - llvm-rtdyld - llvm-stub - macho-dump - opt These tools are mainly useful for people that want to manipulate llvm bitcode (.bc) and llvm assembly language (.ll) files, or want to tinker with llvm and clang themselves. git-svn-id: svn://svn.freebsd.org/base/stable/9@231919 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- contrib/llvm/tools/bugpoint/BugDriver.cpp | 247 ++++ contrib/llvm/tools/bugpoint/BugDriver.h | 330 +++++ contrib/llvm/tools/bugpoint/CMakeLists.txt | 14 + contrib/llvm/tools/bugpoint/CrashDebugger.cpp | 667 ++++++++++ .../llvm/tools/bugpoint/ExecutionDriver.cpp | 516 ++++++++ .../llvm/tools/bugpoint/ExtractFunction.cpp | 370 ++++++ contrib/llvm/tools/bugpoint/FindBugs.cpp | 113 ++ contrib/llvm/tools/bugpoint/ListReducer.h | 201 +++ contrib/llvm/tools/bugpoint/Makefile | 16 + .../llvm/tools/bugpoint/Miscompilation.cpp | 1078 +++++++++++++++++ .../llvm/tools/bugpoint/OptimizerDriver.cpp | 265 ++++ contrib/llvm/tools/bugpoint/ToolRunner.cpp | 977 +++++++++++++++ contrib/llvm/tools/bugpoint/ToolRunner.h | 247 ++++ contrib/llvm/tools/bugpoint/bugpoint.cpp | 209 ++++ contrib/llvm/tools/llc/CMakeLists.txt | 5 + contrib/llvm/tools/llc/Makefile | 21 + contrib/llvm/tools/llc/llc.cpp | 381 ++++++ contrib/llvm/tools/lli/CMakeLists.txt | 5 + contrib/llvm/tools/lli/Makefile | 15 + contrib/llvm/tools/lli/lli.cpp | 305 +++++ contrib/llvm/tools/llvm-ar/CMakeLists.txt | 8 + contrib/llvm/tools/llvm-ar/Makefile | 25 + contrib/llvm/tools/llvm-ar/llvm-ar.cpp | 781 ++++++++++++ contrib/llvm/tools/llvm-as/CMakeLists.txt | 6 + contrib/llvm/tools/llvm-as/Makefile | 17 + contrib/llvm/tools/llvm-as/llvm-as.cpp | 119 ++ .../llvm/tools/llvm-bcanalyzer/CMakeLists.txt | 6 + contrib/llvm/tools/llvm-bcanalyzer/Makefile | 17 + .../tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 629 ++++++++++ contrib/llvm/tools/llvm-diff/CMakeLists.txt | 8 + contrib/llvm/tools/llvm-diff/DiffConsumer.cpp | 209 ++++ contrib/llvm/tools/llvm-diff/DiffConsumer.h | 92 ++ contrib/llvm/tools/llvm-diff/DiffLog.cpp | 53 + contrib/llvm/tools/llvm-diff/DiffLog.h | 80 ++ .../llvm/tools/llvm-diff/DifferenceEngine.cpp | 677 +++++++++++ .../llvm/tools/llvm-diff/DifferenceEngine.h | 91 ++ contrib/llvm/tools/llvm-diff/Makefile | 17 + contrib/llvm/tools/llvm-diff/llvm-diff.cpp | 98 ++ contrib/llvm/tools/llvm-dis/CMakeLists.txt | 6 + contrib/llvm/tools/llvm-dis/Makefile | 17 + contrib/llvm/tools/llvm-dis/llvm-dis.cpp | 186 +++ .../llvm/tools/llvm-extract/CMakeLists.txt | 5 + contrib/llvm/tools/llvm-extract/Makefile | 18 + .../llvm/tools/llvm-extract/llvm-extract.cpp | 238 ++++ contrib/llvm/tools/llvm-ld/CMakeLists.txt | 8 + contrib/llvm/tools/llvm-ld/Makefile | 15 + contrib/llvm/tools/llvm-ld/Optimize.cpp | 130 ++ contrib/llvm/tools/llvm-ld/llvm-ld.cpp | 732 +++++++++++ contrib/llvm/tools/llvm-link/CMakeLists.txt | 5 + contrib/llvm/tools/llvm-link/Makefile | 17 + contrib/llvm/tools/llvm-link/llvm-link.cpp | 142 +++ contrib/llvm/tools/llvm-mc/CMakeLists.txt | 6 + contrib/llvm/tools/llvm-mc/Disassembler.cpp | 368 ++++++ contrib/llvm/tools/llvm-mc/Disassembler.h | 42 + contrib/llvm/tools/llvm-mc/Makefile | 24 + contrib/llvm/tools/llvm-mc/llvm-mc.cpp | 517 ++++++++ contrib/llvm/tools/llvm-nm/CMakeLists.txt | 5 + contrib/llvm/tools/llvm-nm/Makefile | 17 + contrib/llvm/tools/llvm-nm/llvm-nm.cpp | 392 ++++++ .../llvm/tools/llvm-objdump/CMakeLists.txt | 14 + .../llvm/tools/llvm-objdump/MCFunction.cpp | 138 +++ contrib/llvm/tools/llvm-objdump/MCFunction.h | 100 ++ contrib/llvm/tools/llvm-objdump/MachODump.cpp | 617 ++++++++++ contrib/llvm/tools/llvm-objdump/Makefile | 18 + .../llvm/tools/llvm-objdump/llvm-objdump.cpp | 459 +++++++ .../llvm/tools/llvm-objdump/llvm-objdump.h | 46 + contrib/llvm/tools/llvm-prof/CMakeLists.txt | 5 + contrib/llvm/tools/llvm-prof/Makefile | 17 + contrib/llvm/tools/llvm-prof/llvm-prof.cpp | 293 +++++ contrib/llvm/tools/llvm-ranlib/CMakeLists.txt | 6 + contrib/llvm/tools/llvm-ranlib/Makefile | 18 + .../llvm/tools/llvm-ranlib/llvm-ranlib.cpp | 101 ++ contrib/llvm/tools/llvm-rtdyld/CMakeLists.txt | 5 + contrib/llvm/tools/llvm-rtdyld/Makefile | 23 + .../llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp | 151 +++ contrib/llvm/tools/llvm-stub/CMakeLists.txt | 3 + contrib/llvm/tools/llvm-stub/Makefile | 13 + contrib/llvm/tools/llvm-stub/llvm-stub.c | 77 ++ contrib/llvm/tools/macho-dump/CMakeLists.txt | 5 + contrib/llvm/tools/macho-dump/Makefile | 23 + contrib/llvm/tools/macho-dump/macho-dump.cpp | 400 ++++++ contrib/llvm/tools/opt/AnalysisWrappers.cpp | 94 ++ contrib/llvm/tools/opt/CMakeLists.txt | 8 + contrib/llvm/tools/opt/GraphPrinters.cpp | 118 ++ contrib/llvm/tools/opt/Makefile | 14 + contrib/llvm/tools/opt/PrintSCC.cpp | 112 ++ contrib/llvm/tools/opt/opt.cpp | 713 +++++++++++ lib/clang/Makefile | 15 + lib/clang/libllvmanalysis/Makefile | 10 + lib/clang/libllvmarchive/Makefile | 10 + lib/clang/libllvmdebuginfo/Makefile | 17 + lib/clang/libllvmexecutionengine/Makefile | 10 + lib/clang/libllvminterpreter/Makefile | 12 + lib/clang/libllvmipa/Makefile | 6 + lib/clang/libllvmipo/Makefile | 6 + lib/clang/libllvmjit/Makefile | 14 + lib/clang/libllvmlinker/Makefile | 11 + lib/clang/libllvmmc/Makefile | 6 + lib/clang/libllvmmcdisassembler/Makefile | 12 + lib/clang/libllvmmcjit/Makefile | 9 + lib/clang/libllvmobject/Makefile | 16 + lib/clang/libllvmruntimedyld/Makefile | 9 + lib/clang/libllvmscalaropts/Makefile | 9 + lib/clang/libllvmsupport/Makefile | 12 + lib/clang/libllvmtransformutils/Makefile | 7 + lib/clang/libllvmx86disassembler/Makefile | 6 + share/man/man5/src.conf.5 | 11 +- share/mk/bsd.own.mk | 5 + tools/build/mk/OptionalObsoleteFiles.inc | 38 + tools/build/options/WITH_CLANG_EXTRAS | 2 + usr.bin/clang/Makefile | 25 + usr.bin/clang/bugpoint/Makefile | 34 + usr.bin/clang/bugpoint/bugpoint.1 | 291 +++++ usr.bin/clang/llc/Makefile | 45 + usr.bin/clang/llc/llc.1 | 285 +++++ usr.bin/clang/lli/Makefile | 35 + usr.bin/clang/lli/lli.1 | 310 +++++ usr.bin/clang/llvm-ar/Makefile | 14 + usr.bin/clang/llvm-ar/llvm-ar.1 | 461 +++++++ usr.bin/clang/llvm-as/Makefile | 14 + usr.bin/clang/llvm-as/llvm-as.1 | 182 +++ usr.bin/clang/llvm-bcanalyzer/Makefile | 13 + .../clang/llvm-bcanalyzer/llvm-bcanalyzer.1 | 370 ++++++ usr.bin/clang/llvm-diff/Makefile | 16 + usr.bin/clang/llvm-diff/llvm-diff.1 | 175 +++ usr.bin/clang/llvm-dis/Makefile | 17 + usr.bin/clang/llvm-dis/llvm-dis.1 | 175 +++ usr.bin/clang/llvm-extract/Makefile | 22 + usr.bin/clang/llvm-extract/llvm-extract.1 | 195 +++ usr.bin/clang/llvm-ld/Makefile | 25 + usr.bin/clang/llvm-ld/llvm-ld.1 | 319 +++++ usr.bin/clang/llvm-link/Makefile | 21 + usr.bin/clang/llvm-link/llvm-link.1 | 190 +++ usr.bin/clang/llvm-mc/Makefile | 46 + usr.bin/clang/llvm-nm/Makefile | 14 + usr.bin/clang/llvm-nm/llvm-nm.1 | 219 ++++ usr.bin/clang/llvm-objdump/Makefile | 49 + usr.bin/clang/llvm-prof/Makefile | 15 + usr.bin/clang/llvm-prof/llvm-prof.1 | 173 +++ usr.bin/clang/llvm-ranlib/Makefile | 14 + usr.bin/clang/llvm-ranlib/llvm-ranlib.1 | 167 +++ usr.bin/clang/llvm-rtdyld/Makefile | 48 + usr.bin/clang/llvm-stub/Makefile | 9 + usr.bin/clang/macho-dump/Makefile | 12 + usr.bin/clang/opt/Makefile | 27 + usr.bin/clang/opt/opt.1 | 250 ++++ 146 files changed, 19925 insertions(+), 1 deletion(-) create mode 100644 contrib/llvm/tools/bugpoint/BugDriver.cpp create mode 100644 contrib/llvm/tools/bugpoint/BugDriver.h create mode 100644 contrib/llvm/tools/bugpoint/CMakeLists.txt create mode 100644 contrib/llvm/tools/bugpoint/CrashDebugger.cpp create mode 100644 contrib/llvm/tools/bugpoint/ExecutionDriver.cpp create mode 100644 contrib/llvm/tools/bugpoint/ExtractFunction.cpp create mode 100644 contrib/llvm/tools/bugpoint/FindBugs.cpp create mode 100644 contrib/llvm/tools/bugpoint/ListReducer.h create mode 100644 contrib/llvm/tools/bugpoint/Makefile create mode 100644 contrib/llvm/tools/bugpoint/Miscompilation.cpp create mode 100644 contrib/llvm/tools/bugpoint/OptimizerDriver.cpp create mode 100644 contrib/llvm/tools/bugpoint/ToolRunner.cpp create mode 100644 contrib/llvm/tools/bugpoint/ToolRunner.h create mode 100644 contrib/llvm/tools/bugpoint/bugpoint.cpp create mode 100644 contrib/llvm/tools/llc/CMakeLists.txt create mode 100644 contrib/llvm/tools/llc/Makefile create mode 100644 contrib/llvm/tools/llc/llc.cpp create mode 100644 contrib/llvm/tools/lli/CMakeLists.txt create mode 100644 contrib/llvm/tools/lli/Makefile create mode 100644 contrib/llvm/tools/lli/lli.cpp create mode 100644 contrib/llvm/tools/llvm-ar/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-ar/Makefile create mode 100644 contrib/llvm/tools/llvm-ar/llvm-ar.cpp create mode 100644 contrib/llvm/tools/llvm-as/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-as/Makefile create mode 100644 contrib/llvm/tools/llvm-as/llvm-as.cpp create mode 100644 contrib/llvm/tools/llvm-bcanalyzer/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-bcanalyzer/Makefile create mode 100644 contrib/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp create mode 100644 contrib/llvm/tools/llvm-diff/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-diff/DiffConsumer.cpp create mode 100644 contrib/llvm/tools/llvm-diff/DiffConsumer.h create mode 100644 contrib/llvm/tools/llvm-diff/DiffLog.cpp create mode 100644 contrib/llvm/tools/llvm-diff/DiffLog.h create mode 100644 contrib/llvm/tools/llvm-diff/DifferenceEngine.cpp create mode 100644 contrib/llvm/tools/llvm-diff/DifferenceEngine.h create mode 100644 contrib/llvm/tools/llvm-diff/Makefile create mode 100644 contrib/llvm/tools/llvm-diff/llvm-diff.cpp create mode 100644 contrib/llvm/tools/llvm-dis/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-dis/Makefile create mode 100644 contrib/llvm/tools/llvm-dis/llvm-dis.cpp create mode 100644 contrib/llvm/tools/llvm-extract/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-extract/Makefile create mode 100644 contrib/llvm/tools/llvm-extract/llvm-extract.cpp create mode 100644 contrib/llvm/tools/llvm-ld/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-ld/Makefile create mode 100644 contrib/llvm/tools/llvm-ld/Optimize.cpp create mode 100644 contrib/llvm/tools/llvm-ld/llvm-ld.cpp create mode 100644 contrib/llvm/tools/llvm-link/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-link/Makefile create mode 100644 contrib/llvm/tools/llvm-link/llvm-link.cpp create mode 100644 contrib/llvm/tools/llvm-mc/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-mc/Disassembler.cpp create mode 100644 contrib/llvm/tools/llvm-mc/Disassembler.h create mode 100644 contrib/llvm/tools/llvm-mc/Makefile create mode 100644 contrib/llvm/tools/llvm-mc/llvm-mc.cpp create mode 100644 contrib/llvm/tools/llvm-nm/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-nm/Makefile create mode 100644 contrib/llvm/tools/llvm-nm/llvm-nm.cpp create mode 100644 contrib/llvm/tools/llvm-objdump/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-objdump/MCFunction.cpp create mode 100644 contrib/llvm/tools/llvm-objdump/MCFunction.h create mode 100644 contrib/llvm/tools/llvm-objdump/MachODump.cpp create mode 100644 contrib/llvm/tools/llvm-objdump/Makefile create mode 100644 contrib/llvm/tools/llvm-objdump/llvm-objdump.cpp create mode 100644 contrib/llvm/tools/llvm-objdump/llvm-objdump.h create mode 100644 contrib/llvm/tools/llvm-prof/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-prof/Makefile create mode 100644 contrib/llvm/tools/llvm-prof/llvm-prof.cpp create mode 100644 contrib/llvm/tools/llvm-ranlib/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-ranlib/Makefile create mode 100644 contrib/llvm/tools/llvm-ranlib/llvm-ranlib.cpp create mode 100644 contrib/llvm/tools/llvm-rtdyld/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-rtdyld/Makefile create mode 100644 contrib/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp create mode 100644 contrib/llvm/tools/llvm-stub/CMakeLists.txt create mode 100644 contrib/llvm/tools/llvm-stub/Makefile create mode 100644 contrib/llvm/tools/llvm-stub/llvm-stub.c create mode 100644 contrib/llvm/tools/macho-dump/CMakeLists.txt create mode 100644 contrib/llvm/tools/macho-dump/Makefile create mode 100644 contrib/llvm/tools/macho-dump/macho-dump.cpp create mode 100644 contrib/llvm/tools/opt/AnalysisWrappers.cpp create mode 100644 contrib/llvm/tools/opt/CMakeLists.txt create mode 100644 contrib/llvm/tools/opt/GraphPrinters.cpp create mode 100644 contrib/llvm/tools/opt/Makefile create mode 100644 contrib/llvm/tools/opt/PrintSCC.cpp create mode 100644 contrib/llvm/tools/opt/opt.cpp create mode 100644 lib/clang/libllvmarchive/Makefile create mode 100644 lib/clang/libllvmdebuginfo/Makefile create mode 100644 lib/clang/libllvmexecutionengine/Makefile create mode 100644 lib/clang/libllvminterpreter/Makefile create mode 100644 lib/clang/libllvmjit/Makefile create mode 100644 lib/clang/libllvmlinker/Makefile create mode 100644 lib/clang/libllvmmcdisassembler/Makefile create mode 100644 lib/clang/libllvmmcjit/Makefile create mode 100644 lib/clang/libllvmobject/Makefile create mode 100644 lib/clang/libllvmruntimedyld/Makefile create mode 100644 tools/build/options/WITH_CLANG_EXTRAS create mode 100644 usr.bin/clang/bugpoint/Makefile create mode 100644 usr.bin/clang/bugpoint/bugpoint.1 create mode 100644 usr.bin/clang/llc/Makefile create mode 100644 usr.bin/clang/llc/llc.1 create mode 100644 usr.bin/clang/lli/Makefile create mode 100644 usr.bin/clang/lli/lli.1 create mode 100644 usr.bin/clang/llvm-ar/Makefile create mode 100644 usr.bin/clang/llvm-ar/llvm-ar.1 create mode 100644 usr.bin/clang/llvm-as/Makefile create mode 100644 usr.bin/clang/llvm-as/llvm-as.1 create mode 100644 usr.bin/clang/llvm-bcanalyzer/Makefile create mode 100644 usr.bin/clang/llvm-bcanalyzer/llvm-bcanalyzer.1 create mode 100644 usr.bin/clang/llvm-diff/Makefile create mode 100644 usr.bin/clang/llvm-diff/llvm-diff.1 create mode 100644 usr.bin/clang/llvm-dis/Makefile create mode 100644 usr.bin/clang/llvm-dis/llvm-dis.1 create mode 100644 usr.bin/clang/llvm-extract/Makefile create mode 100644 usr.bin/clang/llvm-extract/llvm-extract.1 create mode 100644 usr.bin/clang/llvm-ld/Makefile create mode 100644 usr.bin/clang/llvm-ld/llvm-ld.1 create mode 100644 usr.bin/clang/llvm-link/Makefile create mode 100644 usr.bin/clang/llvm-link/llvm-link.1 create mode 100644 usr.bin/clang/llvm-mc/Makefile create mode 100644 usr.bin/clang/llvm-nm/Makefile create mode 100644 usr.bin/clang/llvm-nm/llvm-nm.1 create mode 100644 usr.bin/clang/llvm-objdump/Makefile create mode 100644 usr.bin/clang/llvm-prof/Makefile create mode 100644 usr.bin/clang/llvm-prof/llvm-prof.1 create mode 100644 usr.bin/clang/llvm-ranlib/Makefile create mode 100644 usr.bin/clang/llvm-ranlib/llvm-ranlib.1 create mode 100644 usr.bin/clang/llvm-rtdyld/Makefile create mode 100644 usr.bin/clang/llvm-stub/Makefile create mode 100644 usr.bin/clang/macho-dump/Makefile create mode 100644 usr.bin/clang/opt/Makefile create mode 100644 usr.bin/clang/opt/opt.1 diff --git a/contrib/llvm/tools/bugpoint/BugDriver.cpp b/contrib/llvm/tools/bugpoint/BugDriver.cpp new file mode 100644 index 000000000..677d17887 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/BugDriver.cpp @@ -0,0 +1,247 @@ +//===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class contains all of the shared state and information that is used by +// the BugPoint tool to track down errors in optimizations. This class is the +// main driver class that invokes all sub-functionality. +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "ToolRunner.h" +#include "llvm/Linker.h" +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Host.h" +#include +using namespace llvm; + +namespace llvm { + Triple TargetTriple; +} + +// Anonymous namespace to define command line options for debugging. +// +namespace { + // Output - The user can specify a file containing the expected output of the + // program. If this filename is set, it is used as the reference diff source, + // otherwise the raw input run through an interpreter is used as the reference + // source. + // + cl::opt + OutputFile("output", cl::desc("Specify a reference program output " + "(for miscompilation detection)")); +} + +/// setNewProgram - If we reduce or update the program somehow, call this method +/// to update bugdriver with it. This deletes the old module and sets the +/// specified one as the current program. +void BugDriver::setNewProgram(Module *M) { + delete Program; + Program = M; +} + + +/// getPassesString - Turn a list of passes into a string which indicates the +/// command line options that must be passed to add the passes. +/// +std::string llvm::getPassesString(const std::vector &Passes) { + std::string Result; + for (unsigned i = 0, e = Passes.size(); i != e; ++i) { + if (i) Result += " "; + Result += "-"; + Result += Passes[i]; + } + return Result; +} + +BugDriver::BugDriver(const char *toolname, bool find_bugs, + unsigned timeout, unsigned memlimit, bool use_valgrind, + LLVMContext& ctxt) + : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile), + Program(0), Interpreter(0), SafeInterpreter(0), gcc(0), + run_find_bugs(find_bugs), Timeout(timeout), + MemoryLimit(memlimit), UseValgrind(use_valgrind) {} + +BugDriver::~BugDriver() { + delete Program; +} + + +/// ParseInputFile - Given a bitcode or assembly input filename, parse and +/// return it, or return null if not possible. +/// +Module *llvm::ParseInputFile(const std::string &Filename, + LLVMContext& Ctxt) { + SMDiagnostic Err; + Module *Result = ParseIRFile(Filename, Err, Ctxt); + if (!Result) + Err.Print("bugpoint", errs()); + + // If we don't have an override triple, use the first one to configure + // bugpoint, or use the host triple if none provided. + if (Result) { + if (TargetTriple.getTriple().empty()) { + Triple TheTriple(Result->getTargetTriple()); + + if (TheTriple.getTriple().empty()) + TheTriple.setTriple(sys::getHostTriple()); + + TargetTriple.setTriple(TheTriple.getTriple()); + } + + Result->setTargetTriple(TargetTriple.getTriple()); // override the triple + } + return Result; +} + +// This method takes the specified list of LLVM input files, attempts to load +// them, either as assembly or bitcode, then link them together. It returns +// true on failure (if, for example, an input bitcode file could not be +// parsed), and false on success. +// +bool BugDriver::addSources(const std::vector &Filenames) { + assert(Program == 0 && "Cannot call addSources multiple times!"); + assert(!Filenames.empty() && "Must specify at least on input filename!"); + + // Load the first input file. + Program = ParseInputFile(Filenames[0], Context); + if (Program == 0) return true; + + outs() << "Read input file : '" << Filenames[0] << "'\n"; + + for (unsigned i = 1, e = Filenames.size(); i != e; ++i) { + std::auto_ptr M(ParseInputFile(Filenames[i], Context)); + if (M.get() == 0) return true; + + outs() << "Linking in input file: '" << Filenames[i] << "'\n"; + std::string ErrorMessage; + if (Linker::LinkModules(Program, M.get(), Linker::DestroySource, + &ErrorMessage)) { + errs() << ToolName << ": error linking in '" << Filenames[i] << "': " + << ErrorMessage << '\n'; + return true; + } + } + + outs() << "*** All input ok\n"; + + // All input files read successfully! + return false; +} + + + +/// run - The top level method that is invoked after all of the instance +/// variables are set up from command line arguments. +/// +bool BugDriver::run(std::string &ErrMsg) { + if (run_find_bugs) { + // Rearrange the passes and apply them to the program. Repeat this process + // until the user kills the program or we find a bug. + return runManyPasses(PassesToRun, ErrMsg); + } + + // If we're not running as a child, the first thing that we must do is + // determine what the problem is. Does the optimization series crash the + // compiler, or does it produce illegal code? We make the top-level + // decision by trying to run all of the passes on the the input program, + // which should generate a bitcode file. If it does generate a bitcode + // file, then we know the compiler didn't crash, so try to diagnose a + // miscompilation. + if (!PassesToRun.empty()) { + outs() << "Running selected passes on program to test for crash: "; + if (runPasses(Program, PassesToRun)) + return debugOptimizerCrash(); + } + + // Set up the execution environment, selecting a method to run LLVM bitcode. + if (initializeExecutionEnvironment()) return true; + + // Test to see if we have a code generator crash. + outs() << "Running the code generator to test for a crash: "; + std::string Error; + compileProgram(Program, &Error); + if (!Error.empty()) { + outs() << Error; + return debugCodeGeneratorCrash(ErrMsg); + } + outs() << '\n'; + + // Run the raw input to see where we are coming from. If a reference output + // was specified, make sure that the raw output matches it. If not, it's a + // problem in the front-end or the code generator. + // + bool CreatedOutput = false; + if (ReferenceOutputFile.empty()) { + outs() << "Generating reference output from raw program: "; + if (!createReferenceFile(Program)) { + return debugCodeGeneratorCrash(ErrMsg); + } + CreatedOutput = true; + } + + // Make sure the reference output file gets deleted on exit from this + // function, if appropriate. + sys::Path ROF(ReferenceOutputFile); + FileRemover RemoverInstance(ROF.str(), CreatedOutput && !SaveTemps); + + // Diff the output of the raw program against the reference output. If it + // matches, then we assume there is a miscompilation bug and try to + // diagnose it. + outs() << "*** Checking the code generator...\n"; + bool Diff = diffProgram(Program, "", "", false, &Error); + if (!Error.empty()) { + errs() << Error; + return debugCodeGeneratorCrash(ErrMsg); + } + if (!Diff) { + outs() << "\n*** Output matches: Debugging miscompilation!\n"; + debugMiscompilation(&Error); + if (!Error.empty()) { + errs() << Error; + return debugCodeGeneratorCrash(ErrMsg); + } + return false; + } + + outs() << "\n*** Input program does not match reference diff!\n"; + outs() << "Debugging code generator problem!\n"; + bool Failure = debugCodeGenerator(&Error); + if (!Error.empty()) { + errs() << Error; + return debugCodeGeneratorCrash(ErrMsg); + } + return Failure; +} + +void llvm::PrintFunctionList(const std::vector &Funcs) { + unsigned NumPrint = Funcs.size(); + if (NumPrint > 10) NumPrint = 10; + for (unsigned i = 0; i != NumPrint; ++i) + outs() << " " << Funcs[i]->getName(); + if (NumPrint < Funcs.size()) + outs() << "... <" << Funcs.size() << " total>"; + outs().flush(); +} + +void llvm::PrintGlobalVariableList(const std::vector &GVs) { + unsigned NumPrint = GVs.size(); + if (NumPrint > 10) NumPrint = 10; + for (unsigned i = 0; i != NumPrint; ++i) + outs() << " " << GVs[i]->getName(); + if (NumPrint < GVs.size()) + outs() << "... <" << GVs.size() << " total>"; + outs().flush(); +} diff --git a/contrib/llvm/tools/bugpoint/BugDriver.h b/contrib/llvm/tools/bugpoint/BugDriver.h new file mode 100644 index 000000000..cc78489e3 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/BugDriver.h @@ -0,0 +1,330 @@ +//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class contains all of the shared state and information that is used by +// the BugPoint tool to track down errors in optimizations. This class is the +// main driver class that invokes all sub-functionality. +// +//===----------------------------------------------------------------------===// + +#ifndef BUGDRIVER_H +#define BUGDRIVER_H + +#include "llvm/ADT/ValueMap.h" +#include "llvm/Transforms/Utils/ValueMapper.h" +#include +#include + +namespace llvm { + +class Value; +class PassInfo; +class Module; +class GlobalVariable; +class Function; +class BasicBlock; +class AbstractInterpreter; +class Instruction; +class LLVMContext; + +class DebugCrashes; + +class GCC; + +extern bool DisableSimplifyCFG; + +/// BugpointIsInterrupted - Set to true when the user presses ctrl-c. +/// +extern bool BugpointIsInterrupted; + +class BugDriver { + LLVMContext& Context; + const char *ToolName; // argv[0] of bugpoint + std::string ReferenceOutputFile; // Name of `good' output file + Module *Program; // The raw program, linked together + std::vector PassesToRun; + AbstractInterpreter *Interpreter; // How to run the program + AbstractInterpreter *SafeInterpreter; // To generate reference output, etc. + GCC *gcc; + bool run_find_bugs; + unsigned Timeout; + unsigned MemoryLimit; + bool UseValgrind; + + // FIXME: sort out public/private distinctions... + friend class ReducePassList; + friend class ReduceMisCodegenFunctions; + +public: + BugDriver(const char *toolname, bool find_bugs, + unsigned timeout, unsigned memlimit, bool use_valgrind, + LLVMContext& ctxt); + ~BugDriver(); + + const char *getToolName() const { return ToolName; } + + LLVMContext& getContext() const { return Context; } + + // Set up methods... these methods are used to copy information about the + // command line arguments into instance variables of BugDriver. + // + bool addSources(const std::vector &FileNames); + void addPass(std::string p) { PassesToRun.push_back(p); } + void setPassesToRun(const std::vector &PTR) { + PassesToRun = PTR; + } + const std::vector &getPassesToRun() const { + return PassesToRun; + } + + /// run - The top level method that is invoked after all of the instance + /// variables are set up from command line arguments. The \p as_child argument + /// indicates whether the driver is to run in parent mode or child mode. + /// + bool run(std::string &ErrMsg); + + /// debugOptimizerCrash - This method is called when some optimizer pass + /// crashes on input. It attempts to prune down the testcase to something + /// reasonable, and figure out exactly which pass is crashing. + /// + bool debugOptimizerCrash(const std::string &ID = "passes"); + + /// debugCodeGeneratorCrash - This method is called when the code generator + /// crashes on an input. It attempts to reduce the input as much as possible + /// while still causing the code generator to crash. + bool debugCodeGeneratorCrash(std::string &Error); + + /// debugMiscompilation - This method is used when the passes selected are not + /// crashing, but the generated output is semantically different from the + /// input. + void debugMiscompilation(std::string *Error); + + /// debugPassMiscompilation - This method is called when the specified pass + /// miscompiles Program as input. It tries to reduce the testcase to + /// something that smaller that still miscompiles the program. + /// ReferenceOutput contains the filename of the file containing the output we + /// are to match. + /// + bool debugPassMiscompilation(const PassInfo *ThePass, + const std::string &ReferenceOutput); + + /// compileSharedObject - This method creates a SharedObject from a given + /// BitcodeFile for debugging a code generator. + /// + std::string compileSharedObject(const std::string &BitcodeFile, + std::string &Error); + + /// debugCodeGenerator - This method narrows down a module to a function or + /// set of functions, using the CBE as a ``safe'' code generator for other + /// functions that are not under consideration. + bool debugCodeGenerator(std::string *Error); + + /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT + /// + bool isExecutingJIT(); + + /// runPasses - Run all of the passes in the "PassesToRun" list, discard the + /// output, and return true if any of the passes crashed. + bool runPasses(Module *M) const { + return runPasses(M, PassesToRun); + } + + Module *getProgram() const { return Program; } + + /// swapProgramIn - Set the current module to the specified module, returning + /// the old one. + Module *swapProgramIn(Module *M) { + Module *OldProgram = Program; + Program = M; + return OldProgram; + } + + AbstractInterpreter *switchToSafeInterpreter() { + AbstractInterpreter *Old = Interpreter; + Interpreter = (AbstractInterpreter*)SafeInterpreter; + return Old; + } + + void switchToInterpreter(AbstractInterpreter *AI) { + Interpreter = AI; + } + + /// setNewProgram - If we reduce or update the program somehow, call this + /// method to update bugdriver with it. This deletes the old module and sets + /// the specified one as the current program. + void setNewProgram(Module *M); + + /// compileProgram - Try to compile the specified module, returning false and + /// setting Error if an error occurs. This is used for code generation + /// crash testing. + /// + void compileProgram(Module *M, std::string *Error) const; + + /// executeProgram - This method runs "Program", capturing the output of the + /// program to a file. A recommended filename may be optionally specified. + /// + std::string executeProgram(const Module *Program, + std::string OutputFilename, + std::string Bitcode, + const std::string &SharedObjects, + AbstractInterpreter *AI, + std::string *Error) const; + + /// executeProgramSafely - Used to create reference output with the "safe" + /// backend, if reference output is not provided. If there is a problem with + /// the code generator (e.g., llc crashes), this will return false and set + /// Error. + /// + std::string executeProgramSafely(const Module *Program, + std::string OutputFile, + std::string *Error) const; + + /// createReferenceFile - calls compileProgram and then records the output + /// into ReferenceOutputFile. Returns true if reference file created, false + /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE + /// this function. + /// + bool createReferenceFile(Module *M, const std::string &Filename + = "bugpoint.reference.out"); + + /// diffProgram - This method executes the specified module and diffs the + /// output against the file specified by ReferenceOutputFile. If the output + /// is different, 1 is returned. If there is a problem with the code + /// generator (e.g., llc crashes), this will return -1 and set Error. + /// + bool diffProgram(const Module *Program, + const std::string &BitcodeFile = "", + const std::string &SharedObj = "", + bool RemoveBitcode = false, + std::string *Error = 0) const; + + /// EmitProgressBitcode - This function is used to output M to a file named + /// "bugpoint-ID.bc". + /// + void EmitProgressBitcode(const Module *M, const std::string &ID, + bool NoFlyer = false) const; + + /// deleteInstructionFromProgram - This method clones the current Program and + /// deletes the specified instruction from the cloned module. It then runs a + /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code + /// which depends on the value. The modified module is then returned. + /// + Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp); + + /// performFinalCleanups - This method clones the current Program and performs + /// a series of cleanups intended to get rid of extra cruft on the module. If + /// the MayModifySemantics argument is true, then the cleanups is allowed to + /// modify how the code behaves. + /// + Module *performFinalCleanups(Module *M, bool MayModifySemantics = false); + + /// ExtractLoop - Given a module, extract up to one loop from it into a new + /// function. This returns null if there are no extractable loops in the + /// program or if the loop extractor crashes. + Module *ExtractLoop(Module *M); + + /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks + /// into their own functions. The only detail is that M is actually a module + /// cloned from the one the BBs are in, so some mapping needs to be performed. + /// If this operation fails for some reason (ie the implementation is buggy), + /// this function should return null, otherwise it returns a new Module. + Module *ExtractMappedBlocksFromModule(const std::vector &BBs, + Module *M); + + /// runPassesOn - Carefully run the specified set of pass on the specified + /// module, returning the transformed module on success, or a null pointer on + /// failure. If AutoDebugCrashes is set to true, then bugpoint will + /// automatically attempt to track down a crashing pass if one exists, and + /// this method will never return null. + Module *runPassesOn(Module *M, const std::vector &Passes, + bool AutoDebugCrashes = false, unsigned NumExtraArgs = 0, + const char * const *ExtraArgs = NULL); + + /// runPasses - Run the specified passes on Program, outputting a bitcode + /// file and writting the filename into OutputFile if successful. If the + /// optimizations fail for some reason (optimizer crashes), return true, + /// otherwise return false. If DeleteOutput is set to true, the bitcode is + /// deleted on success, and the filename string is undefined. This prints to + /// outs() a single line message indicating whether compilation was successful + /// or failed, unless Quiet is set. ExtraArgs specifies additional arguments + /// to pass to the child bugpoint instance. + /// + bool runPasses(Module *Program, + const std::vector &PassesToRun, + std::string &OutputFilename, bool DeleteOutput = false, + bool Quiet = false, unsigned NumExtraArgs = 0, + const char * const *ExtraArgs = NULL) const; + + /// runManyPasses - Take the specified pass list and create different + /// combinations of passes to compile the program with. Compile the program with + /// each set and mark test to see if it compiled correctly. If the passes + /// compiled correctly output nothing and rearrange the passes into a new order. + /// If the passes did not compile correctly, output the command required to + /// recreate the failure. This returns true if a compiler error is found. + /// + bool runManyPasses(const std::vector &AllPasses, + std::string &ErrMsg); + + /// writeProgramToFile - This writes the current "Program" to the named + /// bitcode file. If an error occurs, true is returned. + /// + bool writeProgramToFile(const std::string &Filename, const Module *M) const; + +private: + /// runPasses - Just like the method above, but this just returns true or + /// false indicating whether or not the optimizer crashed on the specified + /// input (true = crashed). + /// + bool runPasses(Module *M, + const std::vector &PassesToRun, + bool DeleteOutput = true) const { + std::string Filename; + return runPasses(M, PassesToRun, Filename, DeleteOutput); + } + + /// initializeExecutionEnvironment - This method is used to set up the + /// environment for executing LLVM programs. + /// + bool initializeExecutionEnvironment(); +}; + +/// ParseInputFile - Given a bitcode or assembly input filename, parse and +/// return it, or return null if not possible. +/// +Module *ParseInputFile(const std::string &InputFilename, + LLVMContext& ctxt); + + +/// getPassesString - Turn a list of passes into a string which indicates the +/// command line options that must be passed to add the passes. +/// +std::string getPassesString(const std::vector &Passes); + +/// PrintFunctionList - prints out list of problematic functions +/// +void PrintFunctionList(const std::vector &Funcs); + +/// PrintGlobalVariableList - prints out list of problematic global variables +/// +void PrintGlobalVariableList(const std::vector &GVs); + +// DeleteFunctionBody - "Remove" the function by deleting all of it's basic +// blocks, making it external. +// +void DeleteFunctionBody(Function *F); + +/// SplitFunctionsOutOfModule - Given a module and a list of functions in the +/// module, split the functions OUT of the specified module, and place them in +/// the new module. +Module *SplitFunctionsOutOfModule(Module *M, const std::vector &F, + ValueToValueMapTy &VMap); + +} // End llvm namespace + +#endif diff --git a/contrib/llvm/tools/bugpoint/CMakeLists.txt b/contrib/llvm/tools/bugpoint/CMakeLists.txt new file mode 100644 index 000000000..e06feb100 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/CMakeLists.txt @@ -0,0 +1,14 @@ +set(LLVM_LINK_COMPONENTS asmparser instrumentation scalaropts ipo + linker bitreader bitwriter) + +add_llvm_tool(bugpoint + BugDriver.cpp + CrashDebugger.cpp + ExecutionDriver.cpp + ExtractFunction.cpp + FindBugs.cpp + Miscompilation.cpp + OptimizerDriver.cpp + ToolRunner.cpp + bugpoint.cpp + ) diff --git a/contrib/llvm/tools/bugpoint/CrashDebugger.cpp b/contrib/llvm/tools/bugpoint/CrashDebugger.cpp new file mode 100644 index 000000000..f19ef6222 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/CrashDebugger.cpp @@ -0,0 +1,667 @@ +//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the bugpoint internals that narrow down compilation crashes +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "ToolRunner.h" +#include "ListReducer.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Instructions.h" +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/PassManager.h" +#include "llvm/ValueSymbolTable.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Support/CFG.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/CommandLine.h" +#include +using namespace llvm; + +namespace { + cl::opt + KeepMain("keep-main", + cl::desc("Force function reduction to keep main"), + cl::init(false)); + cl::opt + NoGlobalRM ("disable-global-remove", + cl::desc("Do not remove global variables"), + cl::init(false)); +} + +namespace llvm { + class ReducePassList : public ListReducer { + BugDriver &BD; + public: + ReducePassList(BugDriver &bd) : BD(bd) {} + + // doTest - Return true iff running the "removed" passes succeeds, and + // running the "Kept" passes fail when run on the output of the "removed" + // passes. If we return true, we update the current module of bugpoint. + // + virtual TestResult doTest(std::vector &Removed, + std::vector &Kept, + std::string &Error); + }; +} + +ReducePassList::TestResult +ReducePassList::doTest(std::vector &Prefix, + std::vector &Suffix, + std::string &Error) { + sys::Path PrefixOutput; + Module *OrigProgram = 0; + if (!Prefix.empty()) { + outs() << "Checking to see if these passes crash: " + << getPassesString(Prefix) << ": "; + std::string PfxOutput; + if (BD.runPasses(BD.getProgram(), Prefix, PfxOutput)) + return KeepPrefix; + + PrefixOutput.set(PfxOutput); + OrigProgram = BD.Program; + + BD.Program = ParseInputFile(PrefixOutput.str(), BD.getContext()); + if (BD.Program == 0) { + errs() << BD.getToolName() << ": Error reading bitcode file '" + << PrefixOutput.str() << "'!\n"; + exit(1); + } + PrefixOutput.eraseFromDisk(); + } + + outs() << "Checking to see if these passes crash: " + << getPassesString(Suffix) << ": "; + + if (BD.runPasses(BD.getProgram(), Suffix)) { + delete OrigProgram; // The suffix crashes alone... + return KeepSuffix; + } + + // Nothing failed, restore state... + if (OrigProgram) { + delete BD.Program; + BD.Program = OrigProgram; + } + return NoFailure; +} + +namespace { + /// ReduceCrashingGlobalVariables - This works by removing the global + /// variable's initializer and seeing if the program still crashes. If it + /// does, then we keep that program and try again. + /// + class ReduceCrashingGlobalVariables : public ListReducer { + BugDriver &BD; + bool (*TestFn)(const BugDriver &, Module *); + public: + ReduceCrashingGlobalVariables(BugDriver &bd, + bool (*testFn)(const BugDriver &, Module *)) + : BD(bd), TestFn(testFn) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Kept, + std::string &Error) { + if (!Kept.empty() && TestGlobalVariables(Kept)) + return KeepSuffix; + if (!Prefix.empty() && TestGlobalVariables(Prefix)) + return KeepPrefix; + return NoFailure; + } + + bool TestGlobalVariables(std::vector &GVs); + }; +} + +bool +ReduceCrashingGlobalVariables::TestGlobalVariables( + std::vector &GVs) { + // Clone the program to try hacking it apart... + ValueToValueMapTy VMap; + Module *M = CloneModule(BD.getProgram(), VMap); + + // Convert list to set for fast lookup... + std::set GVSet; + + for (unsigned i = 0, e = GVs.size(); i != e; ++i) { + GlobalVariable* CMGV = cast(VMap[GVs[i]]); + assert(CMGV && "Global Variable not in module?!"); + GVSet.insert(CMGV); + } + + outs() << "Checking for crash with only these global variables: "; + PrintGlobalVariableList(GVs); + outs() << ": "; + + // Loop over and delete any global variables which we aren't supposed to be + // playing with... + for (Module::global_iterator I = M->global_begin(), E = M->global_end(); + I != E; ++I) + if (I->hasInitializer() && !GVSet.count(I)) { + I->setInitializer(0); + I->setLinkage(GlobalValue::ExternalLinkage); + } + + // Try running the hacked up program... + if (TestFn(BD, M)) { + BD.setNewProgram(M); // It crashed, keep the trimmed version... + + // Make sure to use global variable pointers that point into the now-current + // module. + GVs.assign(GVSet.begin(), GVSet.end()); + return true; + } + + delete M; + return false; +} + +namespace llvm { + /// ReduceCrashingFunctions reducer - This works by removing functions and + /// seeing if the program still crashes. If it does, then keep the newer, + /// smaller program. + /// + class ReduceCrashingFunctions : public ListReducer { + BugDriver &BD; + bool (*TestFn)(const BugDriver &, Module *); + public: + ReduceCrashingFunctions(BugDriver &bd, + bool (*testFn)(const BugDriver &, Module *)) + : BD(bd), TestFn(testFn) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Kept, + std::string &Error) { + if (!Kept.empty() && TestFuncs(Kept)) + return KeepSuffix; + if (!Prefix.empty() && TestFuncs(Prefix)) + return KeepPrefix; + return NoFailure; + } + + bool TestFuncs(std::vector &Prefix); + }; +} + +bool ReduceCrashingFunctions::TestFuncs(std::vector &Funcs) { + + //if main isn't present, claim there is no problem + if (KeepMain && find(Funcs.begin(), Funcs.end(), + BD.getProgram()->getFunction("main")) == Funcs.end()) + return false; + + // Clone the program to try hacking it apart... + ValueToValueMapTy VMap; + Module *M = CloneModule(BD.getProgram(), VMap); + + // Convert list to set for fast lookup... + std::set Functions; + for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { + Function *CMF = cast(VMap[Funcs[i]]); + assert(CMF && "Function not in module?!"); + assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty"); + assert(CMF->getName() == Funcs[i]->getName() && "wrong name"); + Functions.insert(CMF); + } + + outs() << "Checking for crash with only these functions: "; + PrintFunctionList(Funcs); + outs() << ": "; + + // Loop over and delete any functions which we aren't supposed to be playing + // with... + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + if (!I->isDeclaration() && !Functions.count(I)) + DeleteFunctionBody(I); + + // Try running the hacked up program... + if (TestFn(BD, M)) { + BD.setNewProgram(M); // It crashed, keep the trimmed version... + + // Make sure to use function pointers that point into the now-current + // module. + Funcs.assign(Functions.begin(), Functions.end()); + return true; + } + delete M; + return false; +} + + +namespace { + /// ReduceCrashingBlocks reducer - This works by setting the terminators of + /// all terminators except the specified basic blocks to a 'ret' instruction, + /// then running the simplify-cfg pass. This has the effect of chopping up + /// the CFG really fast which can reduce large functions quickly. + /// + class ReduceCrashingBlocks : public ListReducer { + BugDriver &BD; + bool (*TestFn)(const BugDriver &, Module *); + public: + ReduceCrashingBlocks(BugDriver &bd, + bool (*testFn)(const BugDriver &, Module *)) + : BD(bd), TestFn(testFn) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Kept, + std::string &Error) { + if (!Kept.empty() && TestBlocks(Kept)) + return KeepSuffix; + if (!Prefix.empty() && TestBlocks(Prefix)) + return KeepPrefix; + return NoFailure; + } + + bool TestBlocks(std::vector &Prefix); + }; +} + +bool ReduceCrashingBlocks::TestBlocks(std::vector &BBs) { + // Clone the program to try hacking it apart... + ValueToValueMapTy VMap; + Module *M = CloneModule(BD.getProgram(), VMap); + + // Convert list to set for fast lookup... + SmallPtrSet Blocks; + for (unsigned i = 0, e = BBs.size(); i != e; ++i) + Blocks.insert(cast(VMap[BBs[i]])); + + outs() << "Checking for crash with only these blocks:"; + unsigned NumPrint = Blocks.size(); + if (NumPrint > 10) NumPrint = 10; + for (unsigned i = 0, e = NumPrint; i != e; ++i) + outs() << " " << BBs[i]->getName(); + if (NumPrint < Blocks.size()) + outs() << "... <" << Blocks.size() << " total>"; + outs() << ": "; + + // Loop over and delete any hack up any blocks that are not listed... + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) + if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) { + // Loop over all of the successors of this block, deleting any PHI nodes + // that might include it. + for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) + (*SI)->removePredecessor(BB); + + TerminatorInst *BBTerm = BB->getTerminator(); + + if (!BB->getTerminator()->getType()->isVoidTy()) + BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType())); + + // Replace the old terminator instruction. + BB->getInstList().pop_back(); + new UnreachableInst(BB->getContext(), BB); + } + + // The CFG Simplifier pass may delete one of the basic blocks we are + // interested in. If it does we need to take the block out of the list. Make + // a "persistent mapping" by turning basic blocks into pairs. + // This won't work well if blocks are unnamed, but that is just the risk we + // have to take. + std::vector > BlockInfo; + + for (SmallPtrSet::iterator I = Blocks.begin(), + E = Blocks.end(); I != E; ++I) + BlockInfo.push_back(std::make_pair((*I)->getParent()->getName(), + (*I)->getName())); + + // Now run the CFG simplify pass on the function... + std::vector Passes; + Passes.push_back("simplifycfg"); + Passes.push_back("verify"); + Module *New = BD.runPassesOn(M, Passes); + delete M; + if (!New) { + errs() << "simplifycfg failed!\n"; + exit(1); + } + M = New; + + // Try running on the hacked up program... + if (TestFn(BD, M)) { + BD.setNewProgram(M); // It crashed, keep the trimmed version... + + // Make sure to use basic block pointers that point into the now-current + // module, and that they don't include any deleted blocks. + BBs.clear(); + const ValueSymbolTable &GST = M->getValueSymbolTable(); + for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { + Function *F = cast(GST.lookup(BlockInfo[i].first)); + ValueSymbolTable &ST = F->getValueSymbolTable(); + Value* V = ST.lookup(BlockInfo[i].second); + if (V && V->getType() == Type::getLabelTy(V->getContext())) + BBs.push_back(cast(V)); + } + return true; + } + delete M; // It didn't crash, try something else. + return false; +} + +namespace { + /// ReduceCrashingInstructions reducer - This works by removing the specified + /// non-terminator instructions and replacing them with undef. + /// + class ReduceCrashingInstructions : public ListReducer { + BugDriver &BD; + bool (*TestFn)(const BugDriver &, Module *); + public: + ReduceCrashingInstructions(BugDriver &bd, + bool (*testFn)(const BugDriver &, Module *)) + : BD(bd), TestFn(testFn) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Kept, + std::string &Error) { + if (!Kept.empty() && TestInsts(Kept)) + return KeepSuffix; + if (!Prefix.empty() && TestInsts(Prefix)) + return KeepPrefix; + return NoFailure; + } + + bool TestInsts(std::vector &Prefix); + }; +} + +bool ReduceCrashingInstructions::TestInsts(std::vector + &Insts) { + // Clone the program to try hacking it apart... + ValueToValueMapTy VMap; + Module *M = CloneModule(BD.getProgram(), VMap); + + // Convert list to set for fast lookup... + SmallPtrSet Instructions; + for (unsigned i = 0, e = Insts.size(); i != e; ++i) { + assert(!isa(Insts[i])); + Instructions.insert(cast(VMap[Insts[i]])); + } + + outs() << "Checking for crash with only " << Instructions.size(); + if (Instructions.size() == 1) + outs() << " instruction: "; + else + outs() << " instructions: "; + + for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) + for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) + for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { + Instruction *Inst = I++; + if (!Instructions.count(Inst) && !isa(Inst)) { + if (!Inst->getType()->isVoidTy()) + Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); + Inst->eraseFromParent(); + } + } + + // Verify that this is still valid. + PassManager Passes; + Passes.add(createVerifierPass()); + Passes.run(*M); + + // Try running on the hacked up program... + if (TestFn(BD, M)) { + BD.setNewProgram(M); // It crashed, keep the trimmed version... + + // Make sure to use instruction pointers that point into the now-current + // module, and that they don't include any deleted blocks. + Insts.clear(); + for (SmallPtrSet::const_iterator I = Instructions.begin(), + E = Instructions.end(); I != E; ++I) + Insts.push_back(*I); + return true; + } + delete M; // It didn't crash, try something else. + return false; +} + +/// DebugACrash - Given a predicate that determines whether a component crashes +/// on a program, try to destructively reduce the program while still keeping +/// the predicate true. +static bool DebugACrash(BugDriver &BD, + bool (*TestFn)(const BugDriver &, Module *), + std::string &Error) { + // See if we can get away with nuking some of the global variable initializers + // in the program... + if (!NoGlobalRM && + BD.getProgram()->global_begin() != BD.getProgram()->global_end()) { + // Now try to reduce the number of global variable initializers in the + // module to something small. + Module *M = CloneModule(BD.getProgram()); + bool DeletedInit = false; + + for (Module::global_iterator I = M->global_begin(), E = M->global_end(); + I != E; ++I) + if (I->hasInitializer()) { + I->setInitializer(0); + I->setLinkage(GlobalValue::ExternalLinkage); + DeletedInit = true; + } + + if (!DeletedInit) { + delete M; // No change made... + } else { + // See if the program still causes a crash... + outs() << "\nChecking to see if we can delete global inits: "; + + if (TestFn(BD, M)) { // Still crashes? + BD.setNewProgram(M); + outs() << "\n*** Able to remove all global initializers!\n"; + } else { // No longer crashes? + outs() << " - Removing all global inits hides problem!\n"; + delete M; + + std::vector GVs; + + for (Module::global_iterator I = BD.getProgram()->global_begin(), + E = BD.getProgram()->global_end(); I != E; ++I) + if (I->hasInitializer()) + GVs.push_back(I); + + if (GVs.size() > 1 && !BugpointIsInterrupted) { + outs() << "\n*** Attempting to reduce the number of global " + << "variables in the testcase\n"; + + unsigned OldSize = GVs.size(); + ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error); + if (!Error.empty()) + return true; + + if (GVs.size() < OldSize) + BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables"); + } + } + } + } + + // Now try to reduce the number of functions in the module to something small. + std::vector Functions; + for (Module::iterator I = BD.getProgram()->begin(), + E = BD.getProgram()->end(); I != E; ++I) + if (!I->isDeclaration()) + Functions.push_back(I); + + if (Functions.size() > 1 && !BugpointIsInterrupted) { + outs() << "\n*** Attempting to reduce the number of functions " + "in the testcase\n"; + + unsigned OldSize = Functions.size(); + ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error); + + if (Functions.size() < OldSize) + BD.EmitProgressBitcode(BD.getProgram(), "reduced-function"); + } + + // Attempt to delete entire basic blocks at a time to speed up + // convergence... this actually works by setting the terminator of the blocks + // to a return instruction then running simplifycfg, which can potentially + // shrinks the code dramatically quickly + // + if (!DisableSimplifyCFG && !BugpointIsInterrupted) { + std::vector Blocks; + for (Module::const_iterator I = BD.getProgram()->begin(), + E = BD.getProgram()->end(); I != E; ++I) + for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI) + Blocks.push_back(FI); + unsigned OldSize = Blocks.size(); + ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error); + if (Blocks.size() < OldSize) + BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks"); + } + + // Attempt to delete instructions using bisection. This should help out nasty + // cases with large basic blocks where the problem is at one end. + if (!BugpointIsInterrupted) { + std::vector Insts; + for (Module::const_iterator MI = BD.getProgram()->begin(), + ME = BD.getProgram()->end(); MI != ME; ++MI) + for (Function::const_iterator FI = MI->begin(), FE = MI->end(); FI != FE; + ++FI) + for (BasicBlock::const_iterator I = FI->begin(), E = FI->end(); + I != E; ++I) + if (!isa(I)) + Insts.push_back(I); + + ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error); + } + + // FIXME: This should use the list reducer to converge faster by deleting + // larger chunks of instructions at a time! + unsigned Simplification = 2; + do { + if (BugpointIsInterrupted) break; + --Simplification; + outs() << "\n*** Attempting to reduce testcase by deleting instruc" + << "tions: Simplification Level #" << Simplification << '\n'; + + // Now that we have deleted the functions that are unnecessary for the + // program, try to remove instructions that are not necessary to cause the + // crash. To do this, we loop through all of the instructions in the + // remaining functions, deleting them (replacing any values produced with + // nulls), and then running ADCE and SimplifyCFG. If the transformed input + // still triggers failure, keep deleting until we cannot trigger failure + // anymore. + // + unsigned InstructionsToSkipBeforeDeleting = 0; + TryAgain: + + // Loop over all of the (non-terminator) instructions remaining in the + // function, attempting to delete them. + unsigned CurInstructionNum = 0; + for (Module::const_iterator FI = BD.getProgram()->begin(), + E = BD.getProgram()->end(); FI != E; ++FI) + if (!FI->isDeclaration()) + for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E; + ++BI) + for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end(); + I != E; ++I, ++CurInstructionNum) + if (InstructionsToSkipBeforeDeleting) { + --InstructionsToSkipBeforeDeleting; + } else { + if (BugpointIsInterrupted) goto ExitLoops; + + outs() << "Checking instruction: " << *I; + Module *M = BD.deleteInstructionFromProgram(I, Simplification); + + // Find out if the pass still crashes on this pass... + if (TestFn(BD, M)) { + // Yup, it does, we delete the old module, and continue trying + // to reduce the testcase... + BD.setNewProgram(M); + InstructionsToSkipBeforeDeleting = CurInstructionNum; + goto TryAgain; // I wish I had a multi-level break here! + } + + // This pass didn't crash without this instruction, try the next + // one. + delete M; + } + + if (InstructionsToSkipBeforeDeleting) { + InstructionsToSkipBeforeDeleting = 0; + goto TryAgain; + } + + } while (Simplification); +ExitLoops: + + // Try to clean up the testcase by running funcresolve and globaldce... + if (!BugpointIsInterrupted) { + outs() << "\n*** Attempting to perform final cleanups: "; + Module *M = CloneModule(BD.getProgram()); + M = BD.performFinalCleanups(M, true); + + // Find out if the pass still crashes on the cleaned up program... + if (TestFn(BD, M)) { + BD.setNewProgram(M); // Yup, it does, keep the reduced version... + } else { + delete M; + } + } + + BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified"); + + return false; +} + +static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) { + return BD.runPasses(M); +} + +/// debugOptimizerCrash - This method is called when some pass crashes on input. +/// It attempts to prune down the testcase to something reasonable, and figure +/// out exactly which pass is crashing. +/// +bool BugDriver::debugOptimizerCrash(const std::string &ID) { + outs() << "\n*** Debugging optimizer crash!\n"; + + std::string Error; + // Reduce the list of passes which causes the optimizer to crash... + if (!BugpointIsInterrupted) + ReducePassList(*this).reduceList(PassesToRun, Error); + assert(Error.empty()); + + outs() << "\n*** Found crashing pass" + << (PassesToRun.size() == 1 ? ": " : "es: ") + << getPassesString(PassesToRun) << '\n'; + + EmitProgressBitcode(Program, ID); + + bool Success = DebugACrash(*this, TestForOptimizerCrash, Error); + assert(Error.empty()); + return Success; +} + +static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) { + std::string Error; + BD.compileProgram(M, &Error); + if (!Error.empty()) { + errs() << "\n"; + return true; // Tool is still crashing. + } + errs() << '\n'; + return false; +} + +/// debugCodeGeneratorCrash - This method is called when the code generator +/// crashes on an input. It attempts to reduce the input as much as possible +/// while still causing the code generator to crash. +bool BugDriver::debugCodeGeneratorCrash(std::string &Error) { + errs() << "*** Debugging code generator crash!\n"; + + return DebugACrash(*this, TestForCodeGenCrash, Error); +} diff --git a/contrib/llvm/tools/bugpoint/ExecutionDriver.cpp b/contrib/llvm/tools/bugpoint/ExecutionDriver.cpp new file mode 100644 index 000000000..77c01ac55 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/ExecutionDriver.cpp @@ -0,0 +1,516 @@ +//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains code used to execute the program utilizing one of the +// various ways of running LLVM bitcode. +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "ToolRunner.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/raw_ostream.h" +#include + +using namespace llvm; + +namespace { + // OutputType - Allow the user to specify the way code should be run, to test + // for miscompilation. + // + enum OutputType { + AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, RunCBE, CBE_bug, LLC_Safe, + CompileCustom, Custom + }; + + cl::opt + AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"), + cl::init(0.0)); + cl::opt + RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"), + cl::init(0.0)); + + cl::opt + InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"), + cl::values(clEnumValN(AutoPick, "auto", "Use best guess"), + clEnumValN(RunLLI, "run-int", + "Execute with the interpreter"), + clEnumValN(RunJIT, "run-jit", "Execute with JIT"), + clEnumValN(RunLLC, "run-llc", "Compile with LLC"), + clEnumValN(RunLLCIA, "run-llc-ia", + "Compile with LLC with integrated assembler"), + clEnumValN(RunCBE, "run-cbe", "Compile with CBE"), + clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"), + clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"), + clEnumValN(CompileCustom, "compile-custom", + "Use -compile-command to define a command to " + "compile the bitcode. Useful to avoid linking."), + clEnumValN(Custom, "run-custom", + "Use -exec-command to define a command to execute " + "the bitcode. Useful for cross-compilation."), + clEnumValEnd), + cl::init(AutoPick)); + + cl::opt + SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"), + cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"), + clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"), + clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"), + clEnumValN(Custom, "safe-run-custom", + "Use -exec-command to define a command to execute " + "the bitcode. Useful for cross-compilation."), + clEnumValEnd), + cl::init(AutoPick)); + + cl::opt + SafeInterpreterPath("safe-path", + cl::desc("Specify the path to the \"safe\" backend program"), + cl::init("")); + + cl::opt + AppendProgramExitCode("append-exit-code", + cl::desc("Append the exit code to the output so it gets diff'd too"), + cl::init(false)); + + cl::opt + InputFile("input", cl::init("/dev/null"), + cl::desc("Filename to pipe in as stdin (default: /dev/null)")); + + cl::list + AdditionalSOs("additional-so", + cl::desc("Additional shared objects to load " + "into executing programs")); + + cl::list + AdditionalLinkerArgs("Xlinker", + cl::desc("Additional arguments to pass to the linker")); + + cl::opt + CustomCompileCommand("compile-command", cl::init("llc"), + cl::desc("Command to compile the bitcode (use with -compile-custom) " + "(default: llc)")); + + cl::opt + CustomExecCommand("exec-command", cl::init("simulate"), + cl::desc("Command to execute the bitcode (use with -run-custom) " + "(default: simulate)")); +} + +namespace llvm { + // Anything specified after the --args option are taken as arguments to the + // program being debugged. + cl::list + InputArgv("args", cl::Positional, cl::desc("..."), + cl::ZeroOrMore, cl::PositionalEatsArgs); + + cl::opt + OutputPrefix("output-prefix", cl::init("bugpoint"), + cl::desc("Prefix to use for outputs (default: 'bugpoint')")); +} + +namespace { + cl::list + ToolArgv("tool-args", cl::Positional, cl::desc("..."), + cl::ZeroOrMore, cl::PositionalEatsArgs); + + cl::list + SafeToolArgv("safe-tool-args", cl::Positional, + cl::desc("..."), + cl::ZeroOrMore, cl::PositionalEatsArgs); + + cl::opt + GCCBinary("gcc", cl::init("gcc"), + cl::desc("The gcc binary to use. (default 'gcc')")); + + cl::list + GCCToolArgv("gcc-tool-args", cl::Positional, + cl::desc("..."), + cl::ZeroOrMore, cl::PositionalEatsArgs); +} + +//===----------------------------------------------------------------------===// +// BugDriver method implementation +// + +/// initializeExecutionEnvironment - This method is used to set up the +/// environment for executing LLVM programs. +/// +bool BugDriver::initializeExecutionEnvironment() { + outs() << "Initializing execution environment: "; + + // Create an instance of the AbstractInterpreter interface as specified on + // the command line + SafeInterpreter = 0; + std::string Message; + + switch (InterpreterSel) { + case AutoPick: + InterpreterSel = RunCBE; + Interpreter = + AbstractInterpreter::createCBE(getToolName(), Message, GCCBinary, + &ToolArgv, &GCCToolArgv); + if (!Interpreter) { + InterpreterSel = RunJIT; + Interpreter = AbstractInterpreter::createJIT(getToolName(), Message, + &ToolArgv); + } + if (!Interpreter) { + InterpreterSel = RunLLC; + Interpreter = AbstractInterpreter::createLLC(getToolName(), Message, + GCCBinary, &ToolArgv, + &GCCToolArgv); + } + if (!Interpreter) { + InterpreterSel = RunLLI; + Interpreter = AbstractInterpreter::createLLI(getToolName(), Message, + &ToolArgv); + } + if (!Interpreter) { + InterpreterSel = AutoPick; + Message = "Sorry, I can't automatically select an interpreter!\n"; + } + break; + case RunLLI: + Interpreter = AbstractInterpreter::createLLI(getToolName(), Message, + &ToolArgv); + break; + case RunLLC: + case RunLLCIA: + case LLC_Safe: + Interpreter = AbstractInterpreter::createLLC(getToolName(), Message, + GCCBinary, &ToolArgv, + &GCCToolArgv, + InterpreterSel == RunLLCIA); + break; + case RunJIT: + Interpreter = AbstractInterpreter::createJIT(getToolName(), Message, + &ToolArgv); + break; + case RunCBE: + case CBE_bug: + Interpreter = AbstractInterpreter::createCBE(getToolName(), Message, + GCCBinary, &ToolArgv, + &GCCToolArgv); + break; + case CompileCustom: + Interpreter = + AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand); + break; + case Custom: + Interpreter = + AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand); + break; + default: + Message = "Sorry, this back-end is not supported by bugpoint right now!\n"; + break; + } + if (!Interpreter) + errs() << Message; + else // Display informational messages on stdout instead of stderr + outs() << Message; + + std::string Path = SafeInterpreterPath; + if (Path.empty()) + Path = getToolName(); + std::vector SafeToolArgs = SafeToolArgv; + switch (SafeInterpreterSel) { + case AutoPick: + // In "cbe-bug" mode, default to using LLC as the "safe" backend. + if (!SafeInterpreter && + InterpreterSel == CBE_bug) { + SafeInterpreterSel = RunLLC; + SafeToolArgs.push_back("--relocation-model=pic"); + SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, + GCCBinary, + &SafeToolArgs, + &GCCToolArgv); + } + + // In "llc-safe" mode, default to using LLC as the "safe" backend. + if (!SafeInterpreter && + InterpreterSel == LLC_Safe) { + SafeInterpreterSel = RunLLC; + SafeToolArgs.push_back("--relocation-model=pic"); + SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, + GCCBinary, + &SafeToolArgs, + &GCCToolArgv); + } + + // Pick a backend that's different from the test backend. The JIT and + // LLC backends share a lot of code, so prefer to use the CBE as the + // safe back-end when testing them. + if (!SafeInterpreter && + InterpreterSel != RunCBE) { + SafeInterpreterSel = RunCBE; + SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message, + GCCBinary, + &SafeToolArgs, + &GCCToolArgv); + } + if (!SafeInterpreter && + InterpreterSel != RunLLC && + InterpreterSel != RunJIT) { + SafeInterpreterSel = RunLLC; + SafeToolArgs.push_back("--relocation-model=pic"); + SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, + GCCBinary, + &SafeToolArgs, + &GCCToolArgv); + } + if (!SafeInterpreter) { + SafeInterpreterSel = AutoPick; + Message = "Sorry, I can't automatically select an interpreter!\n"; + } + break; + case RunLLC: + case RunLLCIA: + SafeToolArgs.push_back("--relocation-model=pic"); + SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, + GCCBinary, &SafeToolArgs, + &GCCToolArgv, + SafeInterpreterSel == RunLLCIA); + break; + case RunCBE: + SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message, + GCCBinary, &SafeToolArgs, + &GCCToolArgv); + break; + case Custom: + SafeInterpreter = + AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand); + break; + default: + Message = "Sorry, this back-end is not supported by bugpoint as the " + "\"safe\" backend right now!\n"; + break; + } + if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); } + + gcc = GCC::create(Message, GCCBinary, &GCCToolArgv); + if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); } + + // If there was an error creating the selected interpreter, quit with error. + return Interpreter == 0; +} + +/// compileProgram - Try to compile the specified module, returning false and +/// setting Error if an error occurs. This is used for code generation +/// crash testing. +/// +void BugDriver::compileProgram(Module *M, std::string *Error) const { + // Emit the program to a bitcode file... + sys::Path BitcodeFile (OutputPrefix + "-test-program.bc"); + std::string ErrMsg; + if (BitcodeFile.makeUnique(true, &ErrMsg)) { + errs() << ToolName << ": Error making unique filename: " << ErrMsg + << "\n"; + exit(1); + } + if (writeProgramToFile(BitcodeFile.str(), M)) { + errs() << ToolName << ": Error emitting bitcode to file '" + << BitcodeFile.str() << "'!\n"; + exit(1); + } + + // Remove the temporary bitcode file when we are done. + FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps); + + // Actually compile the program! + Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit); +} + + +/// executeProgram - This method runs "Program", capturing the output of the +/// program to a file, returning the filename of the file. A recommended +/// filename may be optionally specified. +/// +std::string BugDriver::executeProgram(const Module *Program, + std::string OutputFile, + std::string BitcodeFile, + const std::string &SharedObj, + AbstractInterpreter *AI, + std::string *Error) const { + if (AI == 0) AI = Interpreter; + assert(AI && "Interpreter should have been created already!"); + bool CreatedBitcode = false; + std::string ErrMsg; + if (BitcodeFile.empty()) { + // Emit the program to a bitcode file... + sys::Path uniqueFilename(OutputPrefix + "-test-program.bc"); + if (uniqueFilename.makeUnique(true, &ErrMsg)) { + errs() << ToolName << ": Error making unique filename: " + << ErrMsg << "!\n"; + exit(1); + } + BitcodeFile = uniqueFilename.str(); + + if (writeProgramToFile(BitcodeFile, Program)) { + errs() << ToolName << ": Error emitting bitcode to file '" + << BitcodeFile << "'!\n"; + exit(1); + } + CreatedBitcode = true; + } + + // Remove the temporary bitcode file when we are done. + sys::Path BitcodePath(BitcodeFile); + FileRemover BitcodeFileRemover(BitcodePath.str(), + CreatedBitcode && !SaveTemps); + + if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output"; + + // Check to see if this is a valid output filename... + sys::Path uniqueFile(OutputFile); + if (uniqueFile.makeUnique(true, &ErrMsg)) { + errs() << ToolName << ": Error making unique filename: " + << ErrMsg << "\n"; + exit(1); + } + OutputFile = uniqueFile.str(); + + // Figure out which shared objects to run, if any. + std::vector SharedObjs(AdditionalSOs); + if (!SharedObj.empty()) + SharedObjs.push_back(SharedObj); + + int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile, + Error, AdditionalLinkerArgs, SharedObjs, + Timeout, MemoryLimit); + if (!Error->empty()) + return OutputFile; + + if (RetVal == -1) { + errs() << ""; + static bool FirstTimeout = true; + if (FirstTimeout) { + outs() << "\n" + "*** Program execution timed out! This mechanism is designed to handle\n" + " programs stuck in infinite loops gracefully. The -timeout option\n" + " can be used to change the timeout threshold or disable it completely\n" + " (with -timeout=0). This message is only displayed once.\n"; + FirstTimeout = false; + } + } + + if (AppendProgramExitCode) { + std::ofstream outFile(OutputFile.c_str(), std::ios_base::app); + outFile << "exit " << RetVal << '\n'; + outFile.close(); + } + + // Return the filename we captured the output to. + return OutputFile; +} + +/// executeProgramSafely - Used to create reference output with the "safe" +/// backend, if reference output is not provided. +/// +std::string BugDriver::executeProgramSafely(const Module *Program, + std::string OutputFile, + std::string *Error) const { + return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error); +} + +std::string BugDriver::compileSharedObject(const std::string &BitcodeFile, + std::string &Error) { + assert(Interpreter && "Interpreter should have been created already!"); + sys::Path OutputFile; + + // Using the known-good backend. + GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile, + Error); + if (!Error.empty()) + return ""; + + std::string SharedObjectFile; + bool Failure = gcc->MakeSharedObject(OutputFile.str(), FT, SharedObjectFile, + AdditionalLinkerArgs, Error); + if (!Error.empty()) + return ""; + if (Failure) + exit(1); + + // Remove the intermediate C file + OutputFile.eraseFromDisk(); + + return "./" + SharedObjectFile; +} + +/// createReferenceFile - calls compileProgram and then records the output +/// into ReferenceOutputFile. Returns true if reference file created, false +/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE +/// this function. +/// +bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) { + std::string Error; + compileProgram(Program, &Error); + if (!Error.empty()) + return false; + + ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error); + if (!Error.empty()) { + errs() << Error; + if (Interpreter != SafeInterpreter) { + errs() << "*** There is a bug running the \"safe\" backend. Either" + << " debug it (for example with the -run-cbe bugpoint option," + << " if CBE is being used as the \"safe\" backend), or fix the" + << " error some other way.\n"; + } + return false; + } + outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n"; + return true; +} + +/// diffProgram - This method executes the specified module and diffs the +/// output against the file specified by ReferenceOutputFile. If the output +/// is different, 1 is returned. If there is a problem with the code +/// generator (e.g., llc crashes), this will set ErrMsg. +/// +bool BugDriver::diffProgram(const Module *Program, + const std::string &BitcodeFile, + const std::string &SharedObject, + bool RemoveBitcode, + std::string *ErrMsg) const { + // Execute the program, generating an output file... + sys::Path Output(executeProgram(Program, "", BitcodeFile, SharedObject, 0, + ErrMsg)); + if (!ErrMsg->empty()) + return false; + + std::string Error; + bool FilesDifferent = false; + if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile), + sys::Path(Output.str()), + AbsTolerance, RelTolerance, &Error)) { + if (Diff == 2) { + errs() << "While diffing output: " << Error << '\n'; + exit(1); + } + FilesDifferent = true; + } + else { + // Remove the generated output if there are no differences. + Output.eraseFromDisk(); + } + + // Remove the bitcode file if we are supposed to. + if (RemoveBitcode) + sys::Path(BitcodeFile).eraseFromDisk(); + return FilesDifferent; +} + +bool BugDriver::isExecutingJIT() { + return InterpreterSel == RunJIT; +} + diff --git a/contrib/llvm/tools/bugpoint/ExtractFunction.cpp b/contrib/llvm/tools/bugpoint/ExtractFunction.cpp new file mode 100644 index 000000000..73b65ca94 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/ExtractFunction.cpp @@ -0,0 +1,370 @@ +//===- ExtractFunction.cpp - Extract a function from Program --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements several methods that are used to extract functions, +// loops, or portions of a module from the rest of the module. +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Pass.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Assembly/Writer.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Transforms/Utils/FunctionUtils.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Signals.h" +#include +using namespace llvm; + +namespace llvm { + bool DisableSimplifyCFG = false; + extern cl::opt OutputPrefix; +} // End llvm namespace + +namespace { + cl::opt + NoDCE ("disable-dce", + cl::desc("Do not use the -dce pass to reduce testcases")); + cl::opt + NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), + cl::desc("Do not use the -simplifycfg pass to reduce testcases")); +} + +/// deleteInstructionFromProgram - This method clones the current Program and +/// deletes the specified instruction from the cloned module. It then runs a +/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which +/// depends on the value. The modified module is then returned. +/// +Module *BugDriver::deleteInstructionFromProgram(const Instruction *I, + unsigned Simplification) { + // FIXME, use vmap? + Module *Clone = CloneModule(Program); + + const BasicBlock *PBB = I->getParent(); + const Function *PF = PBB->getParent(); + + Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn + std::advance(RFI, std::distance(PF->getParent()->begin(), + Module::const_iterator(PF))); + + Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB + std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); + + BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst + std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); + Instruction *TheInst = RI; // Got the corresponding instruction! + + // If this instruction produces a value, replace any users with null values + if (!TheInst->getType()->isVoidTy()) + TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); + + // Remove the instruction from the program. + TheInst->getParent()->getInstList().erase(TheInst); + + // Spiff up the output a little bit. + std::vector Passes; + + /// Can we get rid of the -disable-* options? + if (Simplification > 1 && !NoDCE) + Passes.push_back("dce"); + if (Simplification && !DisableSimplifyCFG) + Passes.push_back("simplifycfg"); // Delete dead control flow + + Passes.push_back("verify"); + Module *New = runPassesOn(Clone, Passes); + delete Clone; + if (!New) { + errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n"; + exit(1); + } + return New; +} + +/// performFinalCleanups - This method clones the current Program and performs +/// a series of cleanups intended to get rid of extra cruft on the module +/// before handing it to the user. +/// +Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { + // Make all functions external, so GlobalDCE doesn't delete them... + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + I->setLinkage(GlobalValue::ExternalLinkage); + + std::vector CleanupPasses; + CleanupPasses.push_back("globaldce"); + + if (MayModifySemantics) + CleanupPasses.push_back("deadarghaX0r"); + else + CleanupPasses.push_back("deadargelim"); + + Module *New = runPassesOn(M, CleanupPasses); + if (New == 0) { + errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; + return M; + } + delete M; + return New; +} + + +/// ExtractLoop - Given a module, extract up to one loop from it into a new +/// function. This returns null if there are no extractable loops in the +/// program or if the loop extractor crashes. +Module *BugDriver::ExtractLoop(Module *M) { + std::vector LoopExtractPasses; + LoopExtractPasses.push_back("loop-extract-single"); + + Module *NewM = runPassesOn(M, LoopExtractPasses); + if (NewM == 0) { + outs() << "*** Loop extraction failed: "; + EmitProgressBitcode(M, "loopextraction", true); + outs() << "*** Sorry. :( Please report a bug!\n"; + return 0; + } + + // Check to see if we created any new functions. If not, no loops were + // extracted and we should return null. Limit the number of loops we extract + // to avoid taking forever. + static unsigned NumExtracted = 32; + if (M->size() == NewM->size() || --NumExtracted == 0) { + delete NewM; + return 0; + } else { + assert(M->size() < NewM->size() && "Loop extract removed functions?"); + Module::iterator MI = NewM->begin(); + for (unsigned i = 0, e = M->size(); i != e; ++i) + ++MI; + } + + return NewM; +} + + +// DeleteFunctionBody - "Remove" the function by deleting all of its basic +// blocks, making it external. +// +void llvm::DeleteFunctionBody(Function *F) { + // delete the body of the function... + F->deleteBody(); + assert(F->isDeclaration() && "This didn't make the function external!"); +} + +/// GetTorInit - Given a list of entries for static ctors/dtors, return them +/// as a constant array. +static Constant *GetTorInit(std::vector > &TorList) { + assert(!TorList.empty() && "Don't create empty tor list!"); + std::vector ArrayElts; + Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext()); + + StructType *STy = + StructType::get(Int32Ty, TorList[0].first->getType(), NULL); + for (unsigned i = 0, e = TorList.size(); i != e; ++i) { + Constant *Elts[] = { + ConstantInt::get(Int32Ty, TorList[i].second), + TorList[i].first + }; + ArrayElts.push_back(ConstantStruct::get(STy, Elts)); + } + return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(), + ArrayElts.size()), + ArrayElts); +} + +/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and +/// M1 has all of the global variables. If M2 contains any functions that are +/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and +/// prune appropriate entries out of M1s list. +static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, + ValueToValueMapTy &VMap) { + GlobalVariable *GV = M1->getNamedGlobal(GlobalName); + if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() || + !GV->use_empty()) return; + + std::vector > M1Tors, M2Tors; + ConstantArray *InitList = dyn_cast(GV->getInitializer()); + if (!InitList) return; + + for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { + if (ConstantStruct *CS = dyn_cast(InitList->getOperand(i))){ + if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. + + if (CS->getOperand(1)->isNullValue()) + break; // Found a null terminator, stop here. + + ConstantInt *CI = dyn_cast(CS->getOperand(0)); + int Priority = CI ? CI->getSExtValue() : 0; + + Constant *FP = CS->getOperand(1); + if (ConstantExpr *CE = dyn_cast(FP)) + if (CE->isCast()) + FP = CE->getOperand(0); + if (Function *F = dyn_cast(FP)) { + if (!F->isDeclaration()) + M1Tors.push_back(std::make_pair(F, Priority)); + else { + // Map to M2's version of the function. + F = cast(VMap[F]); + M2Tors.push_back(std::make_pair(F, Priority)); + } + } + } + } + + GV->eraseFromParent(); + if (!M1Tors.empty()) { + Constant *M1Init = GetTorInit(M1Tors); + new GlobalVariable(*M1, M1Init->getType(), false, + GlobalValue::AppendingLinkage, + M1Init, GlobalName); + } + + GV = M2->getNamedGlobal(GlobalName); + assert(GV && "Not a clone of M1?"); + assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); + + GV->eraseFromParent(); + if (!M2Tors.empty()) { + Constant *M2Init = GetTorInit(M2Tors); + new GlobalVariable(*M2, M2Init->getType(), false, + GlobalValue::AppendingLinkage, + M2Init, GlobalName); + } +} + + +/// SplitFunctionsOutOfModule - Given a module and a list of functions in the +/// module, split the functions OUT of the specified module, and place them in +/// the new module. +Module * +llvm::SplitFunctionsOutOfModule(Module *M, + const std::vector &F, + ValueToValueMapTy &VMap) { + // Make sure functions & globals are all external so that linkage + // between the two modules will work. + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + I->setLinkage(GlobalValue::ExternalLinkage); + for (Module::global_iterator I = M->global_begin(), E = M->global_end(); + I != E; ++I) { + if (I->hasName() && I->getName()[0] == '\01') + I->setName(I->getName().substr(1)); + I->setLinkage(GlobalValue::ExternalLinkage); + } + + ValueToValueMapTy NewVMap; + Module *New = CloneModule(M, NewVMap); + + // Make sure global initializers exist only in the safe module (CBE->.so) + for (Module::global_iterator I = New->global_begin(), E = New->global_end(); + I != E; ++I) + I->setInitializer(0); // Delete the initializer to make it external + + // Remove the Test functions from the Safe module + std::set TestFunctions; + for (unsigned i = 0, e = F.size(); i != e; ++i) { + Function *TNOF = cast(VMap[F[i]]); + DEBUG(errs() << "Removing function "); + DEBUG(WriteAsOperand(errs(), TNOF, false)); + DEBUG(errs() << "\n"); + TestFunctions.insert(cast(NewVMap[TNOF])); + DeleteFunctionBody(TNOF); // Function is now external in this module! + } + + + // Remove the Safe functions from the Test module + for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) + if (!TestFunctions.count(I)) + DeleteFunctionBody(I); + + + // Make sure that there is a global ctor/dtor array in both halves of the + // module if they both have static ctor/dtor functions. + SplitStaticCtorDtor("llvm.global_ctors", M, New, NewVMap); + SplitStaticCtorDtor("llvm.global_dtors", M, New, NewVMap); + + return New; +} + +//===----------------------------------------------------------------------===// +// Basic Block Extraction Code +//===----------------------------------------------------------------------===// + +/// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks +/// into their own functions. The only detail is that M is actually a module +/// cloned from the one the BBs are in, so some mapping needs to be performed. +/// If this operation fails for some reason (ie the implementation is buggy), +/// this function should return null, otherwise it returns a new Module. +Module *BugDriver::ExtractMappedBlocksFromModule(const + std::vector &BBs, + Module *M) { + sys::Path uniqueFilename(OutputPrefix + "-extractblocks"); + std::string ErrMsg; + if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) { + outs() << "*** Basic Block extraction failed!\n"; + errs() << "Error creating temporary file: " << ErrMsg << "\n"; + EmitProgressBitcode(M, "basicblockextractfail", true); + return 0; + } + sys::RemoveFileOnSignal(uniqueFilename); + + std::string ErrorInfo; + tool_output_file BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo); + if (!ErrorInfo.empty()) { + outs() << "*** Basic Block extraction failed!\n"; + errs() << "Error writing list of blocks to not extract: " << ErrorInfo + << "\n"; + EmitProgressBitcode(M, "basicblockextractfail", true); + return 0; + } + for (std::vector::const_iterator I = BBs.begin(), E = BBs.end(); + I != E; ++I) { + BasicBlock *BB = *I; + // If the BB doesn't have a name, give it one so we have something to key + // off of. + if (!BB->hasName()) BB->setName("tmpbb"); + BlocksToNotExtractFile.os() << BB->getParent()->getNameStr() << " " + << BB->getName() << "\n"; + } + BlocksToNotExtractFile.os().close(); + if (BlocksToNotExtractFile.os().has_error()) { + errs() << "Error writing list of blocks to not extract: " << ErrorInfo + << "\n"; + EmitProgressBitcode(M, "basicblockextractfail", true); + BlocksToNotExtractFile.os().clear_error(); + return 0; + } + BlocksToNotExtractFile.keep(); + + std::string uniqueFN = "--extract-blocks-file=" + uniqueFilename.str(); + const char *ExtraArg = uniqueFN.c_str(); + + std::vector PI; + PI.push_back("extract-blocks"); + Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg); + + uniqueFilename.eraseFromDisk(); // Free disk space + + if (Ret == 0) { + outs() << "*** Basic Block extraction failed, please report a bug!\n"; + EmitProgressBitcode(M, "basicblockextractfail", true); + } + return Ret; +} diff --git a/contrib/llvm/tools/bugpoint/FindBugs.cpp b/contrib/llvm/tools/bugpoint/FindBugs.cpp new file mode 100644 index 000000000..a291f9fb0 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/FindBugs.cpp @@ -0,0 +1,113 @@ +//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines an interface that allows bugpoint to choose different +// combinations of optimizations to run on the selected input. Bugpoint will +// run these optimizations and record the success/failure of each. This way +// we can hopefully spot bugs in the optimizations. +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "ToolRunner.h" +#include "llvm/Pass.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +using namespace llvm; + +/// runManyPasses - Take the specified pass list and create different +/// combinations of passes to compile the program with. Compile the program with +/// each set and mark test to see if it compiled correctly. If the passes +/// compiled correctly output nothing and rearrange the passes into a new order. +/// If the passes did not compile correctly, output the command required to +/// recreate the failure. This returns true if a compiler error is found. +/// +bool BugDriver::runManyPasses(const std::vector &AllPasses, + std::string &ErrMsg) { + setPassesToRun(AllPasses); + outs() << "Starting bug finding procedure...\n\n"; + + // Creating a reference output if necessary + if (initializeExecutionEnvironment()) return false; + + outs() << "\n"; + if (ReferenceOutputFile.empty()) { + outs() << "Generating reference output from raw program: \n"; + if (!createReferenceFile(Program)) + return false; + } + + srand(time(NULL)); + + unsigned num = 1; + while(1) { + // + // Step 1: Randomize the order of the optimizer passes. + // + std::random_shuffle(PassesToRun.begin(), PassesToRun.end()); + + // + // Step 2: Run optimizer passes on the program and check for success. + // + outs() << "Running selected passes on program to test for crash: "; + for(int i = 0, e = PassesToRun.size(); i != e; i++) { + outs() << "-" << PassesToRun[i] << " "; + } + + std::string Filename; + if(runPasses(Program, PassesToRun, Filename, false)) { + outs() << "\n"; + outs() << "Optimizer passes caused failure!\n\n"; + debugOptimizerCrash(); + return true; + } else { + outs() << "Combination " << num << " optimized successfully!\n"; + } + + // + // Step 3: Compile the optimized code. + // + outs() << "Running the code generator to test for a crash: "; + std::string Error; + compileProgram(Program, &Error); + if (!Error.empty()) { + outs() << "\n*** compileProgram threw an exception: "; + outs() << Error; + return debugCodeGeneratorCrash(ErrMsg); + } + outs() << '\n'; + + // + // Step 4: Run the program and compare its output to the reference + // output (created above). + // + outs() << "*** Checking if passes caused miscompliation:\n"; + bool Diff = diffProgram(Program, Filename, "", false, &Error); + if (Error.empty() && Diff) { + outs() << "\n*** diffProgram returned true!\n"; + debugMiscompilation(&Error); + if (Error.empty()) + return true; + } + if (!Error.empty()) { + errs() << Error; + debugCodeGeneratorCrash(ErrMsg); + return true; + } + outs() << "\n*** diff'd output matches!\n"; + + sys::Path(Filename).eraseFromDisk(); + + outs() << "\n\n"; + num++; + } //end while + + // Unreachable. +} diff --git a/contrib/llvm/tools/bugpoint/ListReducer.h b/contrib/llvm/tools/bugpoint/ListReducer.h new file mode 100644 index 000000000..bd1c5da65 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/ListReducer.h @@ -0,0 +1,201 @@ +//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class is to be used as a base class for operations that want to zero in +// on a subset of the input which still causes the bug we are tracking. +// +//===----------------------------------------------------------------------===// + +#ifndef BUGPOINT_LIST_REDUCER_H +#define BUGPOINT_LIST_REDUCER_H + +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/ErrorHandling.h" +#include +#include +#include + +namespace llvm { + + extern bool BugpointIsInterrupted; + +template +struct ListReducer { + enum TestResult { + NoFailure, // No failure of the predicate was detected + KeepSuffix, // The suffix alone satisfies the predicate + KeepPrefix, // The prefix alone satisfies the predicate + InternalError // Encountered an error trying to run the predicate + }; + + virtual ~ListReducer() {} + + // doTest - This virtual function should be overriden by subclasses to + // implement the test desired. The testcase is only required to test to see + // if the Kept list still satisfies the property, but if it is going to check + // the prefix anyway, it can. + // + virtual TestResult doTest(std::vector &Prefix, + std::vector &Kept, + std::string &Error) = 0; + + // reduceList - This function attempts to reduce the length of the specified + // list while still maintaining the "test" property. This is the core of the + // "work" that bugpoint does. + // + bool reduceList(std::vector &TheList, std::string &Error) { + std::vector empty; + std::srand(0x6e5ea738); // Seed the random number generator + switch (doTest(TheList, empty, Error)) { + case KeepPrefix: + if (TheList.size() == 1) // we are done, it's the base case and it fails + return true; + else + break; // there's definitely an error, but we need to narrow it down + + case KeepSuffix: + // cannot be reached! + llvm_unreachable("bugpoint ListReducer internal error: " + "selected empty set."); + + case NoFailure: + return false; // there is no failure with the full set of passes/funcs! + + case InternalError: + assert(!Error.empty()); + return true; + } + + // Maximal number of allowed splitting iterations, + // before the elements are randomly shuffled. + const unsigned MaxIterationsWithoutProgress = 3; + bool ShufflingEnabled = true; + +Backjump: + unsigned MidTop = TheList.size(); + unsigned MaxIterations = MaxIterationsWithoutProgress; + unsigned NumOfIterationsWithoutProgress = 0; + while (MidTop > 1) { // Binary split reduction loop + // Halt if the user presses ctrl-c. + if (BugpointIsInterrupted) { + errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; + return true; + } + + // If the loop doesn't make satisfying progress, try shuffling. + // The purpose of shuffling is to avoid the heavy tails of the + // distribution (improving the speed of convergence). + if (ShufflingEnabled && + NumOfIterationsWithoutProgress > MaxIterations) { + std::vector ShuffledList(TheList); + std::random_shuffle(ShuffledList.begin(), ShuffledList.end()); + errs() << "\n\n*** Testing shuffled set...\n\n"; + // Check that random shuffle doesn't loose the bug + if (doTest(ShuffledList, empty, Error) == KeepPrefix) { + // If the bug is still here, use the shuffled list. + TheList.swap(ShuffledList); + MidTop = TheList.size(); + // Must increase the shuffling treshold to avoid the small + // probability of inifinite looping without making progress. + MaxIterations += 2; + errs() << "\n\n*** Shuffling does not hide the bug...\n\n"; + } else { + ShufflingEnabled = false; // Disable shuffling further on + errs() << "\n\n*** Shuffling hides the bug...\n\n"; + } + NumOfIterationsWithoutProgress = 0; + } + + unsigned Mid = MidTop / 2; + std::vector Prefix(TheList.begin(), TheList.begin()+Mid); + std::vector Suffix(TheList.begin()+Mid, TheList.end()); + + switch (doTest(Prefix, Suffix, Error)) { + case KeepSuffix: + // The property still holds. We can just drop the prefix elements, and + // shorten the list to the "kept" elements. + TheList.swap(Suffix); + MidTop = TheList.size(); + // Reset progress treshold and progress counter + MaxIterations = MaxIterationsWithoutProgress; + NumOfIterationsWithoutProgress = 0; + break; + case KeepPrefix: + // The predicate still holds, shorten the list to the prefix elements. + TheList.swap(Prefix); + MidTop = TheList.size(); + // Reset progress treshold and progress counter + MaxIterations = MaxIterationsWithoutProgress; + NumOfIterationsWithoutProgress = 0; + break; + case NoFailure: + // Otherwise the property doesn't hold. Some of the elements we removed + // must be necessary to maintain the property. + MidTop = Mid; + NumOfIterationsWithoutProgress++; + break; + case InternalError: + return true; // Error was set by doTest. + } + assert(Error.empty() && "doTest did not return InternalError for error"); + } + + // Probability of backjumping from the trimming loop back to the binary + // split reduction loop. + const int BackjumpProbability = 10; + + // Okay, we trimmed as much off the top and the bottom of the list as we + // could. If there is more than two elements in the list, try deleting + // interior elements and testing that. + // + if (TheList.size() > 2) { + bool Changed = true; + std::vector EmptyList; + while (Changed) { // Trimming loop. + Changed = false; + + // If the binary split reduction loop made an unfortunate sequence of + // splits, the trimming loop might be left off with a huge number of + // remaining elements (large search space). Backjumping out of that + // search space and attempting a different split can significantly + // improve the convergence speed. + if (std::rand() % 100 < BackjumpProbability) + goto Backjump; + + for (unsigned i = 1; i < TheList.size()-1; ++i) { // Check interior elts + if (BugpointIsInterrupted) { + errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; + return true; + } + + std::vector TestList(TheList); + TestList.erase(TestList.begin()+i); + + if (doTest(EmptyList, TestList, Error) == KeepSuffix) { + // We can trim down the list! + TheList.swap(TestList); + --i; // Don't skip an element of the list + Changed = true; + } + if (!Error.empty()) + return true; + } + // This can take a long time if left uncontrolled. For now, don't + // iterate. + break; + } + } + + return true; // there are some failure and we've narrowed them down + } +}; + +} // End llvm namespace + +#endif diff --git a/contrib/llvm/tools/bugpoint/Makefile b/contrib/llvm/tools/bugpoint/Makefile new file mode 100644 index 000000000..5d287ef18 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/Makefile @@ -0,0 +1,16 @@ +##===- tools/bugpoint/Makefile -----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = bugpoint + +LINK_COMPONENTS := asmparser instrumentation scalaropts ipo \ + linker bitreader bitwriter + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/bugpoint/Miscompilation.cpp b/contrib/llvm/tools/bugpoint/Miscompilation.cpp new file mode 100644 index 000000000..7ff16dbf9 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/Miscompilation.cpp @@ -0,0 +1,1078 @@ +//===- Miscompilation.cpp - Debug program miscompilations -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements optimizer and code generation miscompilation debugging +// support. +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "ListReducer.h" +#include "ToolRunner.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Instructions.h" +#include "llvm/Linker.h" +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Config/config.h" // for HAVE_LINK_R +using namespace llvm; + +namespace llvm { + extern cl::opt OutputPrefix; + extern cl::list InputArgv; +} + +namespace { + static llvm::cl::opt + DisableLoopExtraction("disable-loop-extraction", + cl::desc("Don't extract loops when searching for miscompilations"), + cl::init(false)); + static llvm::cl::opt + DisableBlockExtraction("disable-block-extraction", + cl::desc("Don't extract blocks when searching for miscompilations"), + cl::init(false)); + + class ReduceMiscompilingPasses : public ListReducer { + BugDriver &BD; + public: + ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Suffix, + std::string &Error); + }; +} + +/// TestResult - After passes have been split into a test group and a control +/// group, see if they still break the program. +/// +ReduceMiscompilingPasses::TestResult +ReduceMiscompilingPasses::doTest(std::vector &Prefix, + std::vector &Suffix, + std::string &Error) { + // First, run the program with just the Suffix passes. If it is still broken + // with JUST the kept passes, discard the prefix passes. + outs() << "Checking to see if '" << getPassesString(Suffix) + << "' compiles correctly: "; + + std::string BitcodeResult; + if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/, + true/*quiet*/)) { + errs() << " Error running this sequence of passes" + << " on the input program!\n"; + BD.setPassesToRun(Suffix); + BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); + exit(BD.debugOptimizerCrash()); + } + + // Check to see if the finished program matches the reference output... + bool Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", + true /*delete bitcode*/, &Error); + if (!Error.empty()) + return InternalError; + if (Diff) { + outs() << " nope.\n"; + if (Suffix.empty()) { + errs() << BD.getToolName() << ": I'm confused: the test fails when " + << "no passes are run, nondeterministic program?\n"; + exit(1); + } + return KeepSuffix; // Miscompilation detected! + } + outs() << " yup.\n"; // No miscompilation! + + if (Prefix.empty()) return NoFailure; + + // Next, see if the program is broken if we run the "prefix" passes first, + // then separately run the "kept" passes. + outs() << "Checking to see if '" << getPassesString(Prefix) + << "' compiles correctly: "; + + // If it is not broken with the kept passes, it's possible that the prefix + // passes must be run before the kept passes to break it. If the program + // WORKS after the prefix passes, but then fails if running the prefix AND + // kept passes, we can update our bitcode file to include the result of the + // prefix passes, then discard the prefix passes. + // + if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false/*delete*/, + true/*quiet*/)) { + errs() << " Error running this sequence of passes" + << " on the input program!\n"; + BD.setPassesToRun(Prefix); + BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); + exit(BD.debugOptimizerCrash()); + } + + // If the prefix maintains the predicate by itself, only keep the prefix! + Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false, &Error); + if (!Error.empty()) + return InternalError; + if (Diff) { + outs() << " nope.\n"; + sys::Path(BitcodeResult).eraseFromDisk(); + return KeepPrefix; + } + outs() << " yup.\n"; // No miscompilation! + + // Ok, so now we know that the prefix passes work, try running the suffix + // passes on the result of the prefix passes. + // + OwningPtr PrefixOutput(ParseInputFile(BitcodeResult, + BD.getContext())); + if (PrefixOutput == 0) { + errs() << BD.getToolName() << ": Error reading bitcode file '" + << BitcodeResult << "'!\n"; + exit(1); + } + sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk + + // Don't check if there are no passes in the suffix. + if (Suffix.empty()) + return NoFailure; + + outs() << "Checking to see if '" << getPassesString(Suffix) + << "' passes compile correctly after the '" + << getPassesString(Prefix) << "' passes: "; + + OwningPtr OriginalInput(BD.swapProgramIn(PrefixOutput.take())); + if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/, + true/*quiet*/)) { + errs() << " Error running this sequence of passes" + << " on the input program!\n"; + BD.setPassesToRun(Suffix); + BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); + exit(BD.debugOptimizerCrash()); + } + + // Run the result... + Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", + true /*delete bitcode*/, &Error); + if (!Error.empty()) + return InternalError; + if (Diff) { + outs() << " nope.\n"; + return KeepSuffix; + } + + // Otherwise, we must not be running the bad pass anymore. + outs() << " yup.\n"; // No miscompilation! + // Restore orig program & free test. + delete BD.swapProgramIn(OriginalInput.take()); + return NoFailure; +} + +namespace { + class ReduceMiscompilingFunctions : public ListReducer { + BugDriver &BD; + bool (*TestFn)(BugDriver &, Module *, Module *, std::string &); + public: + ReduceMiscompilingFunctions(BugDriver &bd, + bool (*F)(BugDriver &, Module *, Module *, + std::string &)) + : BD(bd), TestFn(F) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Suffix, + std::string &Error) { + if (!Suffix.empty()) { + bool Ret = TestFuncs(Suffix, Error); + if (!Error.empty()) + return InternalError; + if (Ret) + return KeepSuffix; + } + if (!Prefix.empty()) { + bool Ret = TestFuncs(Prefix, Error); + if (!Error.empty()) + return InternalError; + if (Ret) + return KeepPrefix; + } + return NoFailure; + } + + bool TestFuncs(const std::vector &Prefix, std::string &Error); + }; +} + +/// TestMergedProgram - Given two modules, link them together and run the +/// program, checking to see if the program matches the diff. If there is +/// an error, return NULL. If not, return the merged module. The Broken argument +/// will be set to true if the output is different. If the DeleteInputs +/// argument is set to true then this function deletes both input +/// modules before it returns. +/// +static Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *M2, + bool DeleteInputs, std::string &Error, + bool &Broken) { + // Link the two portions of the program back to together. + std::string ErrorMsg; + if (!DeleteInputs) { + M1 = CloneModule(M1); + M2 = CloneModule(M2); + } + if (Linker::LinkModules(M1, M2, Linker::DestroySource, &ErrorMsg)) { + errs() << BD.getToolName() << ": Error linking modules together:" + << ErrorMsg << '\n'; + exit(1); + } + delete M2; // We are done with this module. + + // Execute the program. + Broken = BD.diffProgram(M1, "", "", false, &Error); + if (!Error.empty()) { + // Delete the linked module + delete M1; + return NULL; + } + return M1; +} + +/// TestFuncs - split functions in a Module into two groups: those that are +/// under consideration for miscompilation vs. those that are not, and test +/// accordingly. Each group of functions becomes a separate Module. +/// +bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, + std::string &Error) { + // Test to see if the function is misoptimized if we ONLY run it on the + // functions listed in Funcs. + outs() << "Checking to see if the program is misoptimized when " + << (Funcs.size()==1 ? "this function is" : "these functions are") + << " run through the pass" + << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; + PrintFunctionList(Funcs); + outs() << '\n'; + + // Create a clone for two reasons: + // * If the optimization passes delete any function, the deleted function + // will be in the clone and Funcs will still point to valid memory + // * If the optimization passes use interprocedural information to break + // a function, we want to continue with the original function. Otherwise + // we can conclude that a function triggers the bug when in fact one + // needs a larger set of original functions to do so. + ValueToValueMapTy VMap; + Module *Clone = CloneModule(BD.getProgram(), VMap); + Module *Orig = BD.swapProgramIn(Clone); + + std::vector FuncsOnClone; + for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { + Function *F = cast(VMap[Funcs[i]]); + FuncsOnClone.push_back(F); + } + + // Split the module into the two halves of the program we want. + VMap.clear(); + Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); + Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone, + VMap); + + // Run the predicate, note that the predicate will delete both input modules. + bool Broken = TestFn(BD, ToOptimize, ToNotOptimize, Error); + + delete BD.swapProgramIn(Orig); + + return Broken; +} + +/// DisambiguateGlobalSymbols - Give anonymous global values names. +/// +static void DisambiguateGlobalSymbols(Module *M) { + for (Module::global_iterator I = M->global_begin(), E = M->global_end(); + I != E; ++I) + if (!I->hasName()) + I->setName("anon_global"); + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + if (!I->hasName()) + I->setName("anon_fn"); +} + +/// ExtractLoops - Given a reduced list of functions that still exposed the bug, +/// check to see if we can extract the loops in the region without obscuring the +/// bug. If so, it reduces the amount of code identified. +/// +static bool ExtractLoops(BugDriver &BD, + bool (*TestFn)(BugDriver &, Module *, Module *, + std::string &), + std::vector &MiscompiledFunctions, + std::string &Error) { + bool MadeChange = false; + while (1) { + if (BugpointIsInterrupted) return MadeChange; + + ValueToValueMapTy VMap; + Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); + Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, + MiscompiledFunctions, + VMap); + Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); + if (!ToOptimizeLoopExtracted) { + // If the loop extractor crashed or if there were no extractible loops, + // then this chapter of our odyssey is over with. + delete ToNotOptimize; + delete ToOptimize; + return MadeChange; + } + + errs() << "Extracted a loop from the breaking portion of the program.\n"; + + // Bugpoint is intentionally not very trusting of LLVM transformations. In + // particular, we're not going to assume that the loop extractor works, so + // we're going to test the newly loop extracted program to make sure nothing + // has broken. If something broke, then we'll inform the user and stop + // extraction. + AbstractInterpreter *AI = BD.switchToSafeInterpreter(); + bool Failure; + Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, + false, Error, Failure); + if (!New) + return false; + // Delete the original and set the new program. + delete BD.swapProgramIn(New); + if (Failure) { + BD.switchToInterpreter(AI); + + // Merged program doesn't work anymore! + errs() << " *** ERROR: Loop extraction broke the program. :(" + << " Please report a bug!\n"; + errs() << " Continuing on with un-loop-extracted version.\n"; + + BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc", + ToNotOptimize); + BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc", + ToOptimize); + BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc", + ToOptimizeLoopExtracted); + + errs() << "Please submit the " + << OutputPrefix << "-loop-extract-fail-*.bc files.\n"; + delete ToOptimize; + delete ToNotOptimize; + delete ToOptimizeLoopExtracted; + return MadeChange; + } + delete ToOptimize; + BD.switchToInterpreter(AI); + + outs() << " Testing after loop extraction:\n"; + // Clone modules, the tester function will free them. + Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); + Module *TNOBackup = CloneModule(ToNotOptimize); + Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error); + if (!Error.empty()) + return false; + if (!Failure) { + outs() << "*** Loop extraction masked the problem. Undoing.\n"; + // If the program is not still broken, then loop extraction did something + // that masked the error. Stop loop extraction now. + delete TOLEBackup; + delete TNOBackup; + return MadeChange; + } + ToOptimizeLoopExtracted = TOLEBackup; + ToNotOptimize = TNOBackup; + + outs() << "*** Loop extraction successful!\n"; + + std::vector > MisCompFunctions; + for (Module::iterator I = ToOptimizeLoopExtracted->begin(), + E = ToOptimizeLoopExtracted->end(); I != E; ++I) + if (!I->isDeclaration()) + MisCompFunctions.push_back(std::make_pair(I->getName(), + I->getFunctionType())); + + // Okay, great! Now we know that we extracted a loop and that loop + // extraction both didn't break the program, and didn't mask the problem. + // Replace the current program with the loop extracted version, and try to + // extract another loop. + std::string ErrorMsg; + if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, + Linker::DestroySource, &ErrorMsg)){ + errs() << BD.getToolName() << ": Error linking modules together:" + << ErrorMsg << '\n'; + exit(1); + } + delete ToOptimizeLoopExtracted; + + // All of the Function*'s in the MiscompiledFunctions list are in the old + // module. Update this list to include all of the functions in the + // optimized and loop extracted module. + MiscompiledFunctions.clear(); + for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { + Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); + + assert(NewF && "Function not found??"); + MiscompiledFunctions.push_back(NewF); + } + + BD.setNewProgram(ToNotOptimize); + MadeChange = true; + } +} + +namespace { + class ReduceMiscompiledBlocks : public ListReducer { + BugDriver &BD; + bool (*TestFn)(BugDriver &, Module *, Module *, std::string &); + std::vector FunctionsBeingTested; + public: + ReduceMiscompiledBlocks(BugDriver &bd, + bool (*F)(BugDriver &, Module *, Module *, + std::string &), + const std::vector &Fns) + : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} + + virtual TestResult doTest(std::vector &Prefix, + std::vector &Suffix, + std::string &Error) { + if (!Suffix.empty()) { + bool Ret = TestFuncs(Suffix, Error); + if (!Error.empty()) + return InternalError; + if (Ret) + return KeepSuffix; + } + if (!Prefix.empty()) { + bool Ret = TestFuncs(Prefix, Error); + if (!Error.empty()) + return InternalError; + if (Ret) + return KeepPrefix; + } + return NoFailure; + } + + bool TestFuncs(const std::vector &BBs, std::string &Error); + }; +} + +/// TestFuncs - Extract all blocks for the miscompiled functions except for the +/// specified blocks. If the problem still exists, return true. +/// +bool ReduceMiscompiledBlocks::TestFuncs(const std::vector &BBs, + std::string &Error) { + // Test to see if the function is misoptimized if we ONLY run it on the + // functions listed in Funcs. + outs() << "Checking to see if the program is misoptimized when all "; + if (!BBs.empty()) { + outs() << "but these " << BBs.size() << " blocks are extracted: "; + for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) + outs() << BBs[i]->getName() << " "; + if (BBs.size() > 10) outs() << "..."; + } else { + outs() << "blocks are extracted."; + } + outs() << '\n'; + + // Split the module into the two halves of the program we want. + ValueToValueMapTy VMap; + Module *Clone = CloneModule(BD.getProgram(), VMap); + Module *Orig = BD.swapProgramIn(Clone); + std::vector FuncsOnClone; + std::vector BBsOnClone; + for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) { + Function *F = cast(VMap[FunctionsBeingTested[i]]); + FuncsOnClone.push_back(F); + } + for (unsigned i = 0, e = BBs.size(); i != e; ++i) { + BasicBlock *BB = cast(VMap[BBs[i]]); + BBsOnClone.push_back(BB); + } + VMap.clear(); + + Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); + Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, + FuncsOnClone, + VMap); + + // Try the extraction. If it doesn't work, then the block extractor crashed + // or something, in which case bugpoint can't chase down this possibility. + if (Module *New = BD.ExtractMappedBlocksFromModule(BBsOnClone, ToOptimize)) { + delete ToOptimize; + // Run the predicate, + // note that the predicate will delete both input modules. + bool Ret = TestFn(BD, New, ToNotOptimize, Error); + delete BD.swapProgramIn(Orig); + return Ret; + } + delete BD.swapProgramIn(Orig); + delete ToOptimize; + delete ToNotOptimize; + return false; +} + + +/// ExtractBlocks - Given a reduced list of functions that still expose the bug, +/// extract as many basic blocks from the region as possible without obscuring +/// the bug. +/// +static bool ExtractBlocks(BugDriver &BD, + bool (*TestFn)(BugDriver &, Module *, Module *, + std::string &), + std::vector &MiscompiledFunctions, + std::string &Error) { + if (BugpointIsInterrupted) return false; + + std::vector Blocks; + for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) + for (Function::iterator I = MiscompiledFunctions[i]->begin(), + E = MiscompiledFunctions[i]->end(); I != E; ++I) + Blocks.push_back(I); + + // Use the list reducer to identify blocks that can be extracted without + // obscuring the bug. The Blocks list will end up containing blocks that must + // be retained from the original program. + unsigned OldSize = Blocks.size(); + + // Check to see if all blocks are extractible first. + bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions) + .TestFuncs(std::vector(), Error); + if (!Error.empty()) + return false; + if (Ret) { + Blocks.clear(); + } else { + ReduceMiscompiledBlocks(BD, TestFn, + MiscompiledFunctions).reduceList(Blocks, Error); + if (!Error.empty()) + return false; + if (Blocks.size() == OldSize) + return false; + } + + ValueToValueMapTy VMap; + Module *ProgClone = CloneModule(BD.getProgram(), VMap); + Module *ToExtract = SplitFunctionsOutOfModule(ProgClone, + MiscompiledFunctions, + VMap); + Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract); + if (Extracted == 0) { + // Weird, extraction should have worked. + errs() << "Nondeterministic problem extracting blocks??\n"; + delete ProgClone; + delete ToExtract; + return false; + } + + // Otherwise, block extraction succeeded. Link the two program fragments back + // together. + delete ToExtract; + + std::vector > MisCompFunctions; + for (Module::iterator I = Extracted->begin(), E = Extracted->end(); + I != E; ++I) + if (!I->isDeclaration()) + MisCompFunctions.push_back(std::make_pair(I->getName(), + I->getFunctionType())); + + std::string ErrorMsg; + if (Linker::LinkModules(ProgClone, Extracted, Linker::DestroySource, + &ErrorMsg)) { + errs() << BD.getToolName() << ": Error linking modules together:" + << ErrorMsg << '\n'; + exit(1); + } + delete Extracted; + + // Set the new program and delete the old one. + BD.setNewProgram(ProgClone); + + // Update the list of miscompiled functions. + MiscompiledFunctions.clear(); + + for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { + Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); + assert(NewF && "Function not found??"); + MiscompiledFunctions.push_back(NewF); + } + + return true; +} + + +/// DebugAMiscompilation - This is a generic driver to narrow down +/// miscompilations, either in an optimization or a code generator. +/// +static std::vector +DebugAMiscompilation(BugDriver &BD, + bool (*TestFn)(BugDriver &, Module *, Module *, + std::string &), + std::string &Error) { + // Okay, now that we have reduced the list of passes which are causing the + // failure, see if we can pin down which functions are being + // miscompiled... first build a list of all of the non-external functions in + // the program. + std::vector MiscompiledFunctions; + Module *Prog = BD.getProgram(); + for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I) + if (!I->isDeclaration()) + MiscompiledFunctions.push_back(I); + + // Do the reduction... + if (!BugpointIsInterrupted) + ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, + Error); + if (!Error.empty()) { + errs() << "\n***Cannot reduce functions: "; + return MiscompiledFunctions; + } + outs() << "\n*** The following function" + << (MiscompiledFunctions.size() == 1 ? " is" : "s are") + << " being miscompiled: "; + PrintFunctionList(MiscompiledFunctions); + outs() << '\n'; + + // See if we can rip any loops out of the miscompiled functions and still + // trigger the problem. + + if (!BugpointIsInterrupted && !DisableLoopExtraction) { + bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error); + if (!Error.empty()) + return MiscompiledFunctions; + if (Ret) { + // Okay, we extracted some loops and the problem still appears. See if + // we can eliminate some of the created functions from being candidates. + DisambiguateGlobalSymbols(BD.getProgram()); + + // Do the reduction... + if (!BugpointIsInterrupted) + ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, + Error); + if (!Error.empty()) + return MiscompiledFunctions; + + outs() << "\n*** The following function" + << (MiscompiledFunctions.size() == 1 ? " is" : "s are") + << " being miscompiled: "; + PrintFunctionList(MiscompiledFunctions); + outs() << '\n'; + } + } + + if (!BugpointIsInterrupted && !DisableBlockExtraction) { + bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error); + if (!Error.empty()) + return MiscompiledFunctions; + if (Ret) { + // Okay, we extracted some blocks and the problem still appears. See if + // we can eliminate some of the created functions from being candidates. + DisambiguateGlobalSymbols(BD.getProgram()); + + // Do the reduction... + ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, + Error); + if (!Error.empty()) + return MiscompiledFunctions; + + outs() << "\n*** The following function" + << (MiscompiledFunctions.size() == 1 ? " is" : "s are") + << " being miscompiled: "; + PrintFunctionList(MiscompiledFunctions); + outs() << '\n'; + } + } + + return MiscompiledFunctions; +} + +/// TestOptimizer - This is the predicate function used to check to see if the +/// "Test" portion of the program is misoptimized. If so, return true. In any +/// case, both module arguments are deleted. +/// +static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe, + std::string &Error) { + // Run the optimization passes on ToOptimize, producing a transformed version + // of the functions being tested. + outs() << " Optimizing functions being tested: "; + Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(), + /*AutoDebugCrashes*/true); + outs() << "done.\n"; + delete Test; + + outs() << " Checking to see if the merged program executes correctly: "; + bool Broken; + Module *New = TestMergedProgram(BD, Optimized, Safe, true, Error, Broken); + if (New) { + outs() << (Broken ? " nope.\n" : " yup.\n"); + // Delete the original and set the new program. + delete BD.swapProgramIn(New); + } + return Broken; +} + + +/// debugMiscompilation - This method is used when the passes selected are not +/// crashing, but the generated output is semantically different from the +/// input. +/// +void BugDriver::debugMiscompilation(std::string *Error) { + // Make sure something was miscompiled... + if (!BugpointIsInterrupted) + if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) { + if (Error->empty()) + errs() << "*** Optimized program matches reference output! No problem" + << " detected...\nbugpoint can't help you with your problem!\n"; + return; + } + + outs() << "\n*** Found miscompiling pass" + << (getPassesToRun().size() == 1 ? "" : "es") << ": " + << getPassesString(getPassesToRun()) << '\n'; + EmitProgressBitcode(Program, "passinput"); + + std::vector MiscompiledFunctions = + DebugAMiscompilation(*this, TestOptimizer, *Error); + if (!Error->empty()) + return; + + // Output a bunch of bitcode files for the user... + outs() << "Outputting reduced bitcode files which expose the problem:\n"; + ValueToValueMapTy VMap; + Module *ToNotOptimize = CloneModule(getProgram(), VMap); + Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, + MiscompiledFunctions, + VMap); + + outs() << " Non-optimized portion: "; + EmitProgressBitcode(ToNotOptimize, "tonotoptimize", true); + delete ToNotOptimize; // Delete hacked module. + + outs() << " Portion that is input to optimizer: "; + EmitProgressBitcode(ToOptimize, "tooptimize"); + delete ToOptimize; // Delete hacked module. + + return; +} + +/// CleanupAndPrepareModules - Get the specified modules ready for code +/// generator testing. +/// +static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, + Module *Safe) { + // Clean up the modules, removing extra cruft that we don't need anymore... + Test = BD.performFinalCleanups(Test); + + // If we are executing the JIT, we have several nasty issues to take care of. + if (!BD.isExecutingJIT()) return; + + // First, if the main function is in the Safe module, we must add a stub to + // the Test module to call into it. Thus, we create a new function `main' + // which just calls the old one. + if (Function *oldMain = Safe->getFunction("main")) + if (!oldMain->isDeclaration()) { + // Rename it + oldMain->setName("llvm_bugpoint_old_main"); + // Create a NEW `main' function with same type in the test module. + Function *newMain = Function::Create(oldMain->getFunctionType(), + GlobalValue::ExternalLinkage, + "main", Test); + // Create an `oldmain' prototype in the test module, which will + // corresponds to the real main function in the same module. + Function *oldMainProto = Function::Create(oldMain->getFunctionType(), + GlobalValue::ExternalLinkage, + oldMain->getName(), Test); + // Set up and remember the argument list for the main function. + std::vector args; + for (Function::arg_iterator + I = newMain->arg_begin(), E = newMain->arg_end(), + OI = oldMain->arg_begin(); I != E; ++I, ++OI) { + I->setName(OI->getName()); // Copy argument names from oldMain + args.push_back(I); + } + + // Call the old main function and return its result + BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain); + CallInst *call = CallInst::Create(oldMainProto, args, "", BB); + + // If the type of old function wasn't void, return value of call + ReturnInst::Create(Safe->getContext(), call, BB); + } + + // The second nasty issue we must deal with in the JIT is that the Safe + // module cannot directly reference any functions defined in the test + // module. Instead, we use a JIT API call to dynamically resolve the + // symbol. + + // Add the resolver to the Safe module. + // Prototype: void *getPointerToNamedFunction(const char* Name) + Constant *resolverFunc = + Safe->getOrInsertFunction("getPointerToNamedFunction", + Type::getInt8PtrTy(Safe->getContext()), + Type::getInt8PtrTy(Safe->getContext()), + (Type *)0); + + // Use the function we just added to get addresses of functions we need. + for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { + if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && + !F->isIntrinsic() /* ignore intrinsics */) { + Function *TestFn = Test->getFunction(F->getName()); + + // Don't forward functions which are external in the test module too. + if (TestFn && !TestFn->isDeclaration()) { + // 1. Add a string constant with its name to the global file + Constant *InitArray = ConstantArray::get(F->getContext(), F->getName()); + GlobalVariable *funcName = + new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/, + GlobalValue::InternalLinkage, InitArray, + F->getName() + "_name"); + + // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an + // sbyte* so it matches the signature of the resolver function. + + // GetElementPtr *funcName, ulong 0, ulong 0 + std::vector GEPargs(2, + Constant::getNullValue(Type::getInt32Ty(F->getContext()))); + Value *GEP = ConstantExpr::getGetElementPtr(funcName, GEPargs); + std::vector ResolverArgs; + ResolverArgs.push_back(GEP); + + // Rewrite uses of F in global initializers, etc. to uses of a wrapper + // function that dynamically resolves the calls to F via our JIT API + if (!F->use_empty()) { + // Create a new global to hold the cached function pointer. + Constant *NullPtr = ConstantPointerNull::get(F->getType()); + GlobalVariable *Cache = + new GlobalVariable(*F->getParent(), F->getType(), + false, GlobalValue::InternalLinkage, + NullPtr,F->getName()+".fpcache"); + + // Construct a new stub function that will re-route calls to F + FunctionType *FuncTy = F->getFunctionType(); + Function *FuncWrapper = Function::Create(FuncTy, + GlobalValue::InternalLinkage, + F->getName() + "_wrapper", + F->getParent()); + BasicBlock *EntryBB = BasicBlock::Create(F->getContext(), + "entry", FuncWrapper); + BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(), + "usecache", FuncWrapper); + BasicBlock *LookupBB = BasicBlock::Create(F->getContext(), + "lookupfp", FuncWrapper); + + // Check to see if we already looked up the value. + Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); + Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal, + NullPtr, "isNull"); + BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB); + + // Resolve the call to function F via the JIT API: + // + // call resolver(GetElementPtr...) + CallInst *Resolver = + CallInst::Create(resolverFunc, ResolverArgs, "resolver", LookupBB); + + // Cast the result from the resolver to correctly-typed function. + CastInst *CastedResolver = + new BitCastInst(Resolver, + PointerType::getUnqual(F->getFunctionType()), + "resolverCast", LookupBB); + + // Save the value in our cache. + new StoreInst(CastedResolver, Cache, LookupBB); + BranchInst::Create(DoCallBB, LookupBB); + + PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 2, + "fp", DoCallBB); + FuncPtr->addIncoming(CastedResolver, LookupBB); + FuncPtr->addIncoming(CachedVal, EntryBB); + + // Save the argument list. + std::vector Args; + for (Function::arg_iterator i = FuncWrapper->arg_begin(), + e = FuncWrapper->arg_end(); i != e; ++i) + Args.push_back(i); + + // Pass on the arguments to the real function, return its result + if (F->getReturnType()->isVoidTy()) { + CallInst::Create(FuncPtr, Args, "", DoCallBB); + ReturnInst::Create(F->getContext(), DoCallBB); + } else { + CallInst *Call = CallInst::Create(FuncPtr, Args, + "retval", DoCallBB); + ReturnInst::Create(F->getContext(),Call, DoCallBB); + } + + // Use the wrapper function instead of the old function + F->replaceAllUsesWith(FuncWrapper); + } + } + } + } + + if (verifyModule(*Test) || verifyModule(*Safe)) { + errs() << "Bugpoint has a bug, which corrupted a module!!\n"; + abort(); + } +} + + + +/// TestCodeGenerator - This is the predicate function used to check to see if +/// the "Test" portion of the program is miscompiled by the code generator under +/// test. If so, return true. In any case, both module arguments are deleted. +/// +static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe, + std::string &Error) { + CleanupAndPrepareModules(BD, Test, Safe); + + sys::Path TestModuleBC("bugpoint.test.bc"); + std::string ErrMsg; + if (TestModuleBC.makeUnique(true, &ErrMsg)) { + errs() << BD.getToolName() << "Error making unique filename: " + << ErrMsg << "\n"; + exit(1); + } + if (BD.writeProgramToFile(TestModuleBC.str(), Test)) { + errs() << "Error writing bitcode to `" << TestModuleBC.str() + << "'\nExiting."; + exit(1); + } + delete Test; + + FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps); + + // Make the shared library + sys::Path SafeModuleBC("bugpoint.safe.bc"); + if (SafeModuleBC.makeUnique(true, &ErrMsg)) { + errs() << BD.getToolName() << "Error making unique filename: " + << ErrMsg << "\n"; + exit(1); + } + + if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) { + errs() << "Error writing bitcode to `" << SafeModuleBC.str() + << "'\nExiting."; + exit(1); + } + + FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps); + + std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error); + if (!Error.empty()) + return false; + delete Safe; + + FileRemover SharedObjectRemover(SharedObject, !SaveTemps); + + // Run the code generator on the `Test' code, loading the shared library. + // The function returns whether or not the new output differs from reference. + bool Result = BD.diffProgram(BD.getProgram(), TestModuleBC.str(), + SharedObject, false, &Error); + if (!Error.empty()) + return false; + + if (Result) + errs() << ": still failing!\n"; + else + errs() << ": didn't fail.\n"; + + return Result; +} + + +/// debugCodeGenerator - debug errors in LLC, LLI, or CBE. +/// +bool BugDriver::debugCodeGenerator(std::string *Error) { + if ((void*)SafeInterpreter == (void*)Interpreter) { + std::string Result = executeProgramSafely(Program, "bugpoint.safe.out", + Error); + if (Error->empty()) { + outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " + << "the reference diff. This may be due to a\n front-end " + << "bug or a bug in the original program, but this can also " + << "happen if bugpoint isn't running the program with the " + << "right flags or input.\n I left the result of executing " + << "the program with the \"safe\" backend in this file for " + << "you: '" + << Result << "'.\n"; + } + return true; + } + + DisambiguateGlobalSymbols(Program); + + std::vector Funcs = DebugAMiscompilation(*this, TestCodeGenerator, + *Error); + if (!Error->empty()) + return true; + + // Split the module into the two halves of the program we want. + ValueToValueMapTy VMap; + Module *ToNotCodeGen = CloneModule(getProgram(), VMap); + Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, VMap); + + // Condition the modules + CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen); + + sys::Path TestModuleBC("bugpoint.test.bc"); + std::string ErrMsg; + if (TestModuleBC.makeUnique(true, &ErrMsg)) { + errs() << getToolName() << "Error making unique filename: " + << ErrMsg << "\n"; + exit(1); + } + + if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) { + errs() << "Error writing bitcode to `" << TestModuleBC.str() + << "'\nExiting."; + exit(1); + } + delete ToCodeGen; + + // Make the shared library + sys::Path SafeModuleBC("bugpoint.safe.bc"); + if (SafeModuleBC.makeUnique(true, &ErrMsg)) { + errs() << getToolName() << "Error making unique filename: " + << ErrMsg << "\n"; + exit(1); + } + + if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) { + errs() << "Error writing bitcode to `" << SafeModuleBC.str() + << "'\nExiting."; + exit(1); + } + std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error); + if (!Error->empty()) + return true; + delete ToNotCodeGen; + + outs() << "You can reproduce the problem with the command line: \n"; + if (isExecutingJIT()) { + outs() << " lli -load " << SharedObject << " " << TestModuleBC.str(); + } else { + outs() << " llc " << TestModuleBC.str() << " -o " << TestModuleBC.str() + << ".s\n"; + outs() << " gcc " << SharedObject << " " << TestModuleBC.str() + << ".s -o " << TestModuleBC.str() << ".exe"; +#if defined (HAVE_LINK_R) + outs() << " -Wl,-R."; +#endif + outs() << "\n"; + outs() << " " << TestModuleBC.str() << ".exe"; + } + for (unsigned i = 0, e = InputArgv.size(); i != e; ++i) + outs() << " " << InputArgv[i]; + outs() << '\n'; + outs() << "The shared object was created with:\n llc -march=c " + << SafeModuleBC.str() << " -o temporary.c\n" + << " gcc -xc temporary.c -O2 -o " << SharedObject; + if (TargetTriple.getArch() == Triple::sparc) + outs() << " -G"; // Compile a shared library, `-G' for Sparc + else + outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others + + outs() << " -fno-strict-aliasing\n"; + + return false; +} diff --git a/contrib/llvm/tools/bugpoint/OptimizerDriver.cpp b/contrib/llvm/tools/bugpoint/OptimizerDriver.cpp new file mode 100644 index 000000000..336c83d7b --- /dev/null +++ b/contrib/llvm/tools/bugpoint/OptimizerDriver.cpp @@ -0,0 +1,265 @@ +//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines an interface that allows bugpoint to run various passes +// without the threat of a buggy pass corrupting bugpoint (of course, bugpoint +// may have its own bugs, but that's another story...). It achieves this by +// forking a copy of itself and having the child process do the optimizations. +// If this client dies, we can always fork a new one. :) +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Program.h" + +#define DONT_GET_PLUGIN_LOADER_OPTION +#include "llvm/Support/PluginLoader.h" + +#include +using namespace llvm; + +namespace llvm { + extern cl::opt OutputPrefix; +} + +namespace { + // ChildOutput - This option captures the name of the child output file that + // is set up by the parent bugpoint process + cl::opt ChildOutput("child-output", cl::ReallyHidden); +} + +/// writeProgramToFile - This writes the current "Program" to the named bitcode +/// file. If an error occurs, true is returned. +/// +bool BugDriver::writeProgramToFile(const std::string &Filename, + const Module *M) const { + std::string ErrInfo; + tool_output_file Out(Filename.c_str(), ErrInfo, + raw_fd_ostream::F_Binary); + if (ErrInfo.empty()) { + WriteBitcodeToFile(M, Out.os()); + Out.os().close(); + if (!Out.os().has_error()) { + Out.keep(); + return false; + } + } + Out.os().clear_error(); + return true; +} + + +/// EmitProgressBitcode - This function is used to output the current Program +/// to a file named "bugpoint-ID.bc". +/// +void BugDriver::EmitProgressBitcode(const Module *M, + const std::string &ID, + bool NoFlyer) const { + // Output the input to the current pass to a bitcode file, emit a message + // telling the user how to reproduce it: opt -foo blah.bc + // + std::string Filename = OutputPrefix + "-" + ID + ".bc"; + if (writeProgramToFile(Filename, M)) { + errs() << "Error opening file '" << Filename << "' for writing!\n"; + return; + } + + outs() << "Emitted bitcode to '" << Filename << "'\n"; + if (NoFlyer || PassesToRun.empty()) return; + outs() << "\n*** You can reproduce the problem with: "; + if (UseValgrind) outs() << "valgrind "; + outs() << "opt " << Filename << " "; + outs() << getPassesString(PassesToRun) << "\n"; +} + +cl::opt SilencePasses("silence-passes", + cl::desc("Suppress output of running passes (both stdout and stderr)")); + +static cl::list OptArgs("opt-args", cl::Positional, + cl::desc("..."), + cl::ZeroOrMore, cl::PositionalEatsArgs); + +/// runPasses - Run the specified passes on Program, outputting a bitcode file +/// and writing the filename into OutputFile if successful. If the +/// optimizations fail for some reason (optimizer crashes), return true, +/// otherwise return false. If DeleteOutput is set to true, the bitcode is +/// deleted on success, and the filename string is undefined. This prints to +/// outs() a single line message indicating whether compilation was successful +/// or failed. +/// +bool BugDriver::runPasses(Module *Program, + const std::vector &Passes, + std::string &OutputFilename, bool DeleteOutput, + bool Quiet, unsigned NumExtraArgs, + const char * const *ExtraArgs) const { + // setup the output file name + outs().flush(); + sys::Path uniqueFilename(OutputPrefix + "-output.bc"); + std::string ErrMsg; + if (uniqueFilename.makeUnique(true, &ErrMsg)) { + errs() << getToolName() << ": Error making unique filename: " + << ErrMsg << "\n"; + return(1); + } + OutputFilename = uniqueFilename.str(); + + // set up the input file name + sys::Path inputFilename(OutputPrefix + "-input.bc"); + if (inputFilename.makeUnique(true, &ErrMsg)) { + errs() << getToolName() << ": Error making unique filename: " + << ErrMsg << "\n"; + return(1); + } + + std::string ErrInfo; + tool_output_file InFile(inputFilename.c_str(), ErrInfo, + raw_fd_ostream::F_Binary); + + + if (!ErrInfo.empty()) { + errs() << "Error opening bitcode file: " << inputFilename.str() << "\n"; + return 1; + } + WriteBitcodeToFile(Program, InFile.os()); + InFile.os().close(); + if (InFile.os().has_error()) { + errs() << "Error writing bitcode file: " << inputFilename.str() << "\n"; + InFile.os().clear_error(); + return 1; + } + + sys::Path tool = PrependMainExecutablePath("opt", getToolName(), + (void*)"opt"); + if (tool.empty()) { + errs() << "Cannot find `opt' in executable directory!\n"; + return 1; + } + + // Ok, everything that could go wrong before running opt is done. + InFile.keep(); + + // setup the child process' arguments + SmallVector Args; + std::string Opt = tool.str(); + if (UseValgrind) { + Args.push_back("valgrind"); + Args.push_back("--error-exitcode=1"); + Args.push_back("-q"); + Args.push_back(tool.c_str()); + } else + Args.push_back(Opt.c_str()); + + Args.push_back("-o"); + Args.push_back(OutputFilename.c_str()); + for (unsigned i = 0, e = OptArgs.size(); i != e; ++i) + Args.push_back(OptArgs[i].c_str()); + std::vector pass_args; + for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { + pass_args.push_back( std::string("-load")); + pass_args.push_back( PluginLoader::getPlugin(i)); + } + for (std::vector::const_iterator I = Passes.begin(), + E = Passes.end(); I != E; ++I ) + pass_args.push_back( std::string("-") + (*I) ); + for (std::vector::const_iterator I = pass_args.begin(), + E = pass_args.end(); I != E; ++I ) + Args.push_back(I->c_str()); + Args.push_back(inputFilename.c_str()); + for (unsigned i = 0; i < NumExtraArgs; ++i) + Args.push_back(*ExtraArgs); + Args.push_back(0); + + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i = 0, e = Args.size()-1; i != e; ++i) + errs() << " " << Args[i]; + errs() << "\n"; + ); + + sys::Path prog; + if (UseValgrind) + prog = sys::Program::FindProgramByName("valgrind"); + else + prog = tool; + + // Redirect stdout and stderr to nowhere if SilencePasses is given + sys::Path Nowhere; + const sys::Path *Redirects[3] = {0, &Nowhere, &Nowhere}; + + int result = sys::Program::ExecuteAndWait(prog, Args.data(), 0, + (SilencePasses ? Redirects : 0), + Timeout, MemoryLimit, &ErrMsg); + + // If we are supposed to delete the bitcode file or if the passes crashed, + // remove it now. This may fail if the file was never created, but that's ok. + if (DeleteOutput || result != 0) + sys::Path(OutputFilename).eraseFromDisk(); + + // Remove the temporary input file as well + inputFilename.eraseFromDisk(); + + if (!Quiet) { + if (result == 0) + outs() << "Success!\n"; + else if (result > 0) + outs() << "Exited with error code '" << result << "'\n"; + else if (result < 0) { + if (result == -1) + outs() << "Execute failed: " << ErrMsg << "\n"; + else + outs() << "Crashed: " << ErrMsg << "\n"; + } + if (result & 0x01000000) + outs() << "Dumped core\n"; + } + + // Was the child successful? + return result != 0; +} + + +/// runPassesOn - Carefully run the specified set of pass on the specified +/// module, returning the transformed module on success, or a null pointer on +/// failure. +Module *BugDriver::runPassesOn(Module *M, + const std::vector &Passes, + bool AutoDebugCrashes, unsigned NumExtraArgs, + const char * const *ExtraArgs) { + std::string BitcodeResult; + if (runPasses(M, Passes, BitcodeResult, false/*delete*/, true/*quiet*/, + NumExtraArgs, ExtraArgs)) { + if (AutoDebugCrashes) { + errs() << " Error running this sequence of passes" + << " on the input program!\n"; + delete swapProgramIn(M); + EmitProgressBitcode(M, "pass-error", false); + exit(debugOptimizerCrash()); + } + return 0; + } + + Module *Ret = ParseInputFile(BitcodeResult, Context); + if (Ret == 0) { + errs() << getToolName() << ": Error reading bitcode file '" + << BitcodeResult << "'!\n"; + exit(1); + } + sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk + return Ret; +} diff --git a/contrib/llvm/tools/bugpoint/ToolRunner.cpp b/contrib/llvm/tools/bugpoint/ToolRunner.cpp new file mode 100644 index 000000000..0d98262b4 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/ToolRunner.cpp @@ -0,0 +1,977 @@ +//===-- ToolRunner.cpp ----------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the interfaces described in the ToolRunner.h file. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "toolrunner" +#include "ToolRunner.h" +#include "llvm/Support/Program.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Config/config.h" // for HAVE_LINK_R +#include +#include +using namespace llvm; + +namespace llvm { + cl::opt + SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files")); +} + +namespace { + cl::opt + RemoteClient("remote-client", + cl::desc("Remote execution client (rsh/ssh)")); + + cl::opt + RemoteHost("remote-host", + cl::desc("Remote execution (rsh/ssh) host")); + + cl::opt + RemotePort("remote-port", + cl::desc("Remote execution (rsh/ssh) port")); + + cl::opt + RemoteUser("remote-user", + cl::desc("Remote execution (rsh/ssh) user id")); + + cl::opt + RemoteExtra("remote-extra-options", + cl::desc("Remote execution (rsh/ssh) extra options")); +} + +/// RunProgramWithTimeout - This function provides an alternate interface +/// to the sys::Program::ExecuteAndWait interface. +/// @see sys::Program::ExecuteAndWait +static int RunProgramWithTimeout(const sys::Path &ProgramPath, + const char **Args, + const sys::Path &StdInFile, + const sys::Path &StdOutFile, + const sys::Path &StdErrFile, + unsigned NumSeconds = 0, + unsigned MemoryLimit = 0, + std::string *ErrMsg = 0) { + const sys::Path* redirects[3]; + redirects[0] = &StdInFile; + redirects[1] = &StdOutFile; + redirects[2] = &StdErrFile; + +#if 0 // For debug purposes + { + errs() << "RUN:"; + for (unsigned i = 0; Args[i]; ++i) + errs() << " " << Args[i]; + errs() << "\n"; + } +#endif + + return + sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, + NumSeconds, MemoryLimit, ErrMsg); +} + +/// RunProgramRemotelyWithTimeout - This function runs the given program +/// remotely using the given remote client and the sys::Program::ExecuteAndWait. +/// Returns the remote program exit code or reports a remote client error if it +/// fails. Remote client is required to return 255 if it failed or program exit +/// code otherwise. +/// @see sys::Program::ExecuteAndWait +static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath, + const char **Args, + const sys::Path &StdInFile, + const sys::Path &StdOutFile, + const sys::Path &StdErrFile, + unsigned NumSeconds = 0, + unsigned MemoryLimit = 0) { + const sys::Path* redirects[3]; + redirects[0] = &StdInFile; + redirects[1] = &StdOutFile; + redirects[2] = &StdErrFile; + +#if 0 // For debug purposes + { + errs() << "RUN:"; + for (unsigned i = 0; Args[i]; ++i) + errs() << " " << Args[i]; + errs() << "\n"; + } +#endif + + // Run the program remotely with the remote client + int ReturnCode = sys::Program::ExecuteAndWait(RemoteClientPath, Args, + 0, redirects, NumSeconds, MemoryLimit); + + // Has the remote client fail? + if (255 == ReturnCode) { + std::ostringstream OS; + OS << "\nError running remote client:\n "; + for (const char **Arg = Args; *Arg; ++Arg) + OS << " " << *Arg; + OS << "\n"; + + // The error message is in the output file, let's print it out from there. + std::ifstream ErrorFile(StdOutFile.c_str()); + if (ErrorFile) { + std::copy(std::istreambuf_iterator(ErrorFile), + std::istreambuf_iterator(), + std::ostreambuf_iterator(OS)); + ErrorFile.close(); + } + + errs() << OS; + } + + return ReturnCode; +} + +static std::string ProcessFailure(sys::Path ProgPath, const char** Args, + unsigned Timeout = 0, + unsigned MemoryLimit = 0) { + std::ostringstream OS; + OS << "\nError running tool:\n "; + for (const char **Arg = Args; *Arg; ++Arg) + OS << " " << *Arg; + OS << "\n"; + + // Rerun the compiler, capturing any error messages to print them. + sys::Path ErrorFilename("bugpoint.program_error_messages"); + std::string ErrMsg; + if (ErrorFilename.makeUnique(true, &ErrMsg)) { + errs() << "Error making unique filename: " << ErrMsg << "\n"; + exit(1); + } + RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename, + ErrorFilename, Timeout, MemoryLimit); + // FIXME: check return code ? + + // Print out the error messages generated by GCC if possible... + std::ifstream ErrorFile(ErrorFilename.c_str()); + if (ErrorFile) { + std::copy(std::istreambuf_iterator(ErrorFile), + std::istreambuf_iterator(), + std::ostreambuf_iterator(OS)); + ErrorFile.close(); + } + + ErrorFilename.eraseFromDisk(); + return OS.str(); +} + +//===---------------------------------------------------------------------===// +// LLI Implementation of AbstractIntepreter interface +// +namespace { + class LLI : public AbstractInterpreter { + std::string LLIPath; // The path to the LLI executable + std::vector ToolArgs; // Args to pass to LLI + public: + LLI(const std::string &Path, const std::vector *Args) + : LLIPath(Path) { + ToolArgs.clear (); + if (Args) { ToolArgs = *Args; } + } + + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs, + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + }; +} + +int LLI::ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs, + const std::vector &SharedLibs, + unsigned Timeout, + unsigned MemoryLimit) { + std::vector LLIArgs; + LLIArgs.push_back(LLIPath.c_str()); + LLIArgs.push_back("-force-interpreter=true"); + + for (std::vector::const_iterator i = SharedLibs.begin(), + e = SharedLibs.end(); i != e; ++i) { + LLIArgs.push_back("-load"); + LLIArgs.push_back((*i).c_str()); + } + + // Add any extra LLI args. + for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) + LLIArgs.push_back(ToolArgs[i].c_str()); + + LLIArgs.push_back(Bitcode.c_str()); + // Add optional parameters to the running program from Argv + for (unsigned i=0, e = Args.size(); i != e; ++i) + LLIArgs.push_back(Args[i].c_str()); + LLIArgs.push_back(0); + + outs() << ""; outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) + errs() << " " << LLIArgs[i]; + errs() << "\n"; + ); + return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0], + sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), + Timeout, MemoryLimit, Error); +} + +// LLI create method - Try to find the LLI executable +AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0, + std::string &Message, + const std::vector *ToolArgs) { + std::string LLIPath = + PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str(); + if (!LLIPath.empty()) { + Message = "Found lli: " + LLIPath + "\n"; + return new LLI(LLIPath, ToolArgs); + } + + Message = "Cannot find `lli' in executable directory!\n"; + return 0; +} + +//===---------------------------------------------------------------------===// +// Custom compiler command implementation of AbstractIntepreter interface +// +// Allows using a custom command for compiling the bitcode, thus allows, for +// example, to compile a bitcode fragment without linking or executing, then +// using a custom wrapper script to check for compiler errors. +namespace { + class CustomCompiler : public AbstractInterpreter { + std::string CompilerCommand; + std::vector CompilerArgs; + public: + CustomCompiler( + const std::string &CompilerCmd, std::vector CompArgs) : + CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {} + + virtual void compileProgram(const std::string &Bitcode, + std::string *Error, + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs = + std::vector(), + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0) { + *Error = "Execution not supported with -compile-custom"; + return -1; + } + }; +} + +void CustomCompiler::compileProgram(const std::string &Bitcode, + std::string *Error, + unsigned Timeout, + unsigned MemoryLimit) { + + std::vector ProgramArgs; + ProgramArgs.push_back(CompilerCommand.c_str()); + + for (std::size_t i = 0; i < CompilerArgs.size(); ++i) + ProgramArgs.push_back(CompilerArgs.at(i).c_str()); + ProgramArgs.push_back(Bitcode.c_str()); + ProgramArgs.push_back(0); + + // Add optional parameters to the running program from Argv + for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i) + ProgramArgs.push_back(CompilerArgs[i].c_str()); + + if (RunProgramWithTimeout( sys::Path(CompilerCommand), &ProgramArgs[0], + sys::Path(), sys::Path(), sys::Path(), + Timeout, MemoryLimit, Error)) + *Error = ProcessFailure(sys::Path(CompilerCommand), &ProgramArgs[0], + Timeout, MemoryLimit); +} + +//===---------------------------------------------------------------------===// +// Custom execution command implementation of AbstractIntepreter interface +// +// Allows using a custom command for executing the bitcode, thus allows, +// for example, to invoke a cross compiler for code generation followed by +// a simulator that executes the generated binary. +namespace { + class CustomExecutor : public AbstractInterpreter { + std::string ExecutionCommand; + std::vector ExecutorArgs; + public: + CustomExecutor( + const std::string &ExecutionCmd, std::vector ExecArgs) : + ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {} + + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs, + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + }; +} + +int CustomExecutor::ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs, + const std::vector &SharedLibs, + unsigned Timeout, + unsigned MemoryLimit) { + + std::vector ProgramArgs; + ProgramArgs.push_back(ExecutionCommand.c_str()); + + for (std::size_t i = 0; i < ExecutorArgs.size(); ++i) + ProgramArgs.push_back(ExecutorArgs.at(i).c_str()); + ProgramArgs.push_back(Bitcode.c_str()); + ProgramArgs.push_back(0); + + // Add optional parameters to the running program from Argv + for (unsigned i = 0, e = Args.size(); i != e; ++i) + ProgramArgs.push_back(Args[i].c_str()); + + return RunProgramWithTimeout( + sys::Path(ExecutionCommand), + &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile), + sys::Path(OutputFile), Timeout, MemoryLimit, Error); +} + +// Tokenize the CommandLine to the command and the args to allow +// defining a full command line as the command instead of just the +// executed program. We cannot just pass the whole string after the command +// as a single argument because then program sees only a single +// command line argument (with spaces in it: "foo bar" instead +// of "foo" and "bar"). +// +// code borrowed from: +// http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html +static void lexCommand(std::string &Message, const std::string &CommandLine, + std::string &CmdPath, std::vector Args) { + + std::string Command = ""; + std::string delimiters = " "; + + std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0); + std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) { + std::string token = CommandLine.substr(lastPos, pos - lastPos); + if (Command == "") + Command = token; + else + Args.push_back(token); + // Skip delimiters. Note the "not_of" + lastPos = CommandLine.find_first_not_of(delimiters, pos); + // Find next "non-delimiter" + pos = CommandLine.find_first_of(delimiters, lastPos); + } + + CmdPath = sys::Program::FindProgramByName(Command).str(); + if (CmdPath.empty()) { + Message = + std::string("Cannot find '") + Command + + "' in PATH!\n"; + return; + } + + Message = "Found command in: " + CmdPath + "\n"; +} + +// Custom execution environment create method, takes the execution command +// as arguments +AbstractInterpreter *AbstractInterpreter::createCustomCompiler( + std::string &Message, + const std::string &CompileCommandLine) { + + std::string CmdPath; + std::vector Args; + lexCommand(Message, CompileCommandLine, CmdPath, Args); + if (CmdPath.empty()) + return 0; + + return new CustomCompiler(CmdPath, Args); +} + +// Custom execution environment create method, takes the execution command +// as arguments +AbstractInterpreter *AbstractInterpreter::createCustomExecutor( + std::string &Message, + const std::string &ExecCommandLine) { + + + std::string CmdPath; + std::vector Args; + lexCommand(Message, ExecCommandLine, CmdPath, Args); + if (CmdPath.empty()) + return 0; + + return new CustomExecutor(CmdPath, Args); +} + +//===----------------------------------------------------------------------===// +// LLC Implementation of AbstractIntepreter interface +// +GCC::FileType LLC::OutputCode(const std::string &Bitcode, + sys::Path &OutputAsmFile, std::string &Error, + unsigned Timeout, unsigned MemoryLimit) { + const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s"); + sys::Path uniqueFile(Bitcode + Suffix); + std::string ErrMsg; + if (uniqueFile.makeUnique(true, &ErrMsg)) { + errs() << "Error making unique filename: " << ErrMsg << "\n"; + exit(1); + } + OutputAsmFile = uniqueFile; + std::vector LLCArgs; + LLCArgs.push_back(LLCPath.c_str()); + + // Add any extra LLC args. + for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) + LLCArgs.push_back(ToolArgs[i].c_str()); + + LLCArgs.push_back("-o"); + LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file + LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode + + if (UseIntegratedAssembler) + LLCArgs.push_back("-filetype=obj"); + + LLCArgs.push_back (0); + + outs() << (UseIntegratedAssembler ? "" : ""); + outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i) + errs() << " " << LLCArgs[i]; + errs() << "\n"; + ); + if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0], + sys::Path(), sys::Path(), sys::Path(), + Timeout, MemoryLimit)) + Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0], + Timeout, MemoryLimit); + return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile; +} + +void LLC::compileProgram(const std::string &Bitcode, std::string *Error, + unsigned Timeout, unsigned MemoryLimit) { + sys::Path OutputAsmFile; + OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit); + OutputAsmFile.eraseFromDisk(); +} + +int LLC::ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &ArgsForGCC, + const std::vector &SharedLibs, + unsigned Timeout, + unsigned MemoryLimit) { + + sys::Path OutputAsmFile; + GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, + MemoryLimit); + FileRemover OutFileRemover(OutputAsmFile.str(), !SaveTemps); + + std::vector GCCArgs(ArgsForGCC); + GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end()); + + // Assuming LLC worked, compile the result with GCC and run it. + return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind, + InputFile, OutputFile, Error, GCCArgs, + Timeout, MemoryLimit); +} + +/// createLLC - Try to find the LLC executable +/// +LLC *AbstractInterpreter::createLLC(const char *Argv0, + std::string &Message, + const std::string &GCCBinary, + const std::vector *Args, + const std::vector *GCCArgs, + bool UseIntegratedAssembler) { + std::string LLCPath = + PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str(); + if (LLCPath.empty()) { + Message = "Cannot find `llc' in executable directory!\n"; + return 0; + } + + Message = "Found llc: " + LLCPath + "\n"; + GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs); + if (!gcc) { + errs() << Message << "\n"; + exit(1); + } + return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler); +} + +//===---------------------------------------------------------------------===// +// JIT Implementation of AbstractIntepreter interface +// +namespace { + class JIT : public AbstractInterpreter { + std::string LLIPath; // The path to the LLI executable + std::vector ToolArgs; // Args to pass to LLI + public: + JIT(const std::string &Path, const std::vector *Args) + : LLIPath(Path) { + ToolArgs.clear (); + if (Args) { ToolArgs = *Args; } + } + + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs = + std::vector(), + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + }; +} + +int JIT::ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs, + const std::vector &SharedLibs, + unsigned Timeout, + unsigned MemoryLimit) { + // Construct a vector of parameters, incorporating those from the command-line + std::vector JITArgs; + JITArgs.push_back(LLIPath.c_str()); + JITArgs.push_back("-force-interpreter=false"); + + // Add any extra LLI args. + for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) + JITArgs.push_back(ToolArgs[i].c_str()); + + for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { + JITArgs.push_back("-load"); + JITArgs.push_back(SharedLibs[i].c_str()); + } + JITArgs.push_back(Bitcode.c_str()); + // Add optional parameters to the running program from Argv + for (unsigned i=0, e = Args.size(); i != e; ++i) + JITArgs.push_back(Args[i].c_str()); + JITArgs.push_back(0); + + outs() << ""; outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) + errs() << " " << JITArgs[i]; + errs() << "\n"; + ); + DEBUG(errs() << "\nSending output to " << OutputFile << "\n"); + return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0], + sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), + Timeout, MemoryLimit, Error); +} + +/// createJIT - Try to find the LLI executable +/// +AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0, + std::string &Message, const std::vector *Args) { + std::string LLIPath = + PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str(); + if (!LLIPath.empty()) { + Message = "Found lli: " + LLIPath + "\n"; + return new JIT(LLIPath, Args); + } + + Message = "Cannot find `lli' in executable directory!\n"; + return 0; +} + +GCC::FileType CBE::OutputCode(const std::string &Bitcode, + sys::Path &OutputCFile, std::string &Error, + unsigned Timeout, unsigned MemoryLimit) { + sys::Path uniqueFile(Bitcode+".cbe.c"); + std::string ErrMsg; + if (uniqueFile.makeUnique(true, &ErrMsg)) { + errs() << "Error making unique filename: " << ErrMsg << "\n"; + exit(1); + } + OutputCFile = uniqueFile; + std::vector LLCArgs; + LLCArgs.push_back(LLCPath.c_str()); + + // Add any extra LLC args. + for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) + LLCArgs.push_back(ToolArgs[i].c_str()); + + LLCArgs.push_back("-o"); + LLCArgs.push_back(OutputCFile.c_str()); // Output to the C file + LLCArgs.push_back("-march=c"); // Output C language + LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode + LLCArgs.push_back(0); + + outs() << ""; outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i) + errs() << " " << LLCArgs[i]; + errs() << "\n"; + ); + if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(), + sys::Path(), Timeout, MemoryLimit)) + Error = ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit); + return GCC::CFile; +} + +void CBE::compileProgram(const std::string &Bitcode, std::string *Error, + unsigned Timeout, unsigned MemoryLimit) { + sys::Path OutputCFile; + OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit); + OutputCFile.eraseFromDisk(); +} + +int CBE::ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &ArgsForGCC, + const std::vector &SharedLibs, + unsigned Timeout, + unsigned MemoryLimit) { + sys::Path OutputCFile; + OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit); + + FileRemover CFileRemove(OutputCFile.str(), !SaveTemps); + + std::vector GCCArgs(ArgsForGCC); + GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end()); + + return gcc->ExecuteProgram(OutputCFile.str(), Args, GCC::CFile, + InputFile, OutputFile, Error, GCCArgs, + Timeout, MemoryLimit); +} + +/// createCBE - Try to find the 'llc' executable +/// +CBE *AbstractInterpreter::createCBE(const char *Argv0, + std::string &Message, + const std::string &GCCBinary, + const std::vector *Args, + const std::vector *GCCArgs) { + sys::Path LLCPath = + PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createCBE); + if (LLCPath.isEmpty()) { + Message = + "Cannot find `llc' in executable directory!\n"; + return 0; + } + + Message = "Found llc: " + LLCPath.str() + "\n"; + GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs); + if (!gcc) { + errs() << Message << "\n"; + exit(1); + } + return new CBE(LLCPath, gcc, Args); +} + +//===---------------------------------------------------------------------===// +// GCC abstraction +// + +static bool IsARMArchitecture(std::vector Args) { + for (std::vector::const_iterator + I = Args.begin(), E = Args.end(); I != E; ++I) { + if (StringRef(*I).equals_lower("-arch")) { + ++I; + if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm")) + return true; + } + } + + return false; +} + +int GCC::ExecuteProgram(const std::string &ProgramFile, + const std::vector &Args, + FileType fileType, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &ArgsForGCC, + unsigned Timeout, + unsigned MemoryLimit) { + std::vector GCCArgs; + + GCCArgs.push_back(GCCPath.c_str()); + + if (TargetTriple.getArch() == Triple::x86) + GCCArgs.push_back("-m32"); + + for (std::vector::const_iterator + I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I) + GCCArgs.push_back(I->c_str()); + + // Specify -x explicitly in case the extension is wonky + if (fileType != ObjectFile) { + GCCArgs.push_back("-x"); + if (fileType == CFile) { + GCCArgs.push_back("c"); + GCCArgs.push_back("-fno-strict-aliasing"); + } else { + GCCArgs.push_back("assembler"); + + // For ARM architectures we don't want this flag. bugpoint isn't + // explicitly told what architecture it is working on, so we get + // it from gcc flags + if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs)) + GCCArgs.push_back("-force_cpusubtype_ALL"); + } + } + + GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename. + + GCCArgs.push_back("-x"); + GCCArgs.push_back("none"); + GCCArgs.push_back("-o"); + sys::Path OutputBinary (ProgramFile+".gcc.exe"); + std::string ErrMsg; + if (OutputBinary.makeUnique(true, &ErrMsg)) { + errs() << "Error making unique filename: " << ErrMsg << "\n"; + exit(1); + } + GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file... + + // Add any arguments intended for GCC. We locate them here because this is + // most likely -L and -l options that need to come before other libraries but + // after the source. Other options won't be sensitive to placement on the + // command line, so this should be safe. + for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) + GCCArgs.push_back(ArgsForGCC[i].c_str()); + + GCCArgs.push_back("-lm"); // Hard-code the math library... + GCCArgs.push_back("-O2"); // Optimize the program a bit... +#if defined (HAVE_LINK_R) + GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files +#endif + if (TargetTriple.getArch() == Triple::sparc) + GCCArgs.push_back("-mcpu=v9"); + GCCArgs.push_back(0); // NULL terminator + + outs() << ""; outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i) + errs() << " " << GCCArgs[i]; + errs() << "\n"; + ); + if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), + sys::Path())) { + *Error = ProcessFailure(GCCPath, &GCCArgs[0]); + return -1; + } + + std::vector ProgramArgs; + + // Declared here so that the destructor only runs after + // ProgramArgs is used. + std::string Exec; + + if (RemoteClientPath.isEmpty()) + ProgramArgs.push_back(OutputBinary.c_str()); + else { + ProgramArgs.push_back(RemoteClientPath.c_str()); + ProgramArgs.push_back(RemoteHost.c_str()); + if (!RemoteUser.empty()) { + ProgramArgs.push_back("-l"); + ProgramArgs.push_back(RemoteUser.c_str()); + } + if (!RemotePort.empty()) { + ProgramArgs.push_back("-p"); + ProgramArgs.push_back(RemotePort.c_str()); + } + if (!RemoteExtra.empty()) { + ProgramArgs.push_back(RemoteExtra.c_str()); + } + + // Full path to the binary. We need to cd to the exec directory because + // there is a dylib there that the exec expects to find in the CWD + char* env_pwd = getenv("PWD"); + Exec = "cd "; + Exec += env_pwd; + Exec += "; ./"; + Exec += OutputBinary.c_str(); + ProgramArgs.push_back(Exec.c_str()); + } + + // Add optional parameters to the running program from Argv + for (unsigned i = 0, e = Args.size(); i != e; ++i) + ProgramArgs.push_back(Args[i].c_str()); + ProgramArgs.push_back(0); // NULL terminator + + // Now that we have a binary, run it! + outs() << ""; outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i) + errs() << " " << ProgramArgs[i]; + errs() << "\n"; + ); + + FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps); + + if (RemoteClientPath.isEmpty()) { + DEBUG(errs() << ""); + int ExitCode = RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], + sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), + Timeout, MemoryLimit, Error); + // Treat a signal (usually SIGSEGV) or timeout as part of the program output + // so that crash-causing miscompilation is handled seamlessly. + if (ExitCode < -1) { + std::ofstream outFile(OutputFile.c_str(), std::ios_base::app); + outFile << *Error << '\n'; + outFile.close(); + Error->clear(); + } + return ExitCode; + } else { + outs() << ""; outs().flush(); + return RunProgramRemotelyWithTimeout(sys::Path(RemoteClientPath), + &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile), + sys::Path(OutputFile), Timeout, MemoryLimit); + } +} + +int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, + std::string &OutputFile, + const std::vector &ArgsForGCC, + std::string &Error) { + sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); + std::string ErrMsg; + if (uniqueFilename.makeUnique(true, &ErrMsg)) { + errs() << "Error making unique filename: " << ErrMsg << "\n"; + exit(1); + } + OutputFile = uniqueFilename.str(); + + std::vector GCCArgs; + + GCCArgs.push_back(GCCPath.c_str()); + + if (TargetTriple.getArch() == Triple::x86) + GCCArgs.push_back("-m32"); + + for (std::vector::const_iterator + I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I) + GCCArgs.push_back(I->c_str()); + + // Compile the C/asm file into a shared object + if (fileType != ObjectFile) { + GCCArgs.push_back("-x"); + GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c"); + } + GCCArgs.push_back("-fno-strict-aliasing"); + GCCArgs.push_back(InputFile.c_str()); // Specify the input filename. + GCCArgs.push_back("-x"); + GCCArgs.push_back("none"); + if (TargetTriple.getArch() == Triple::sparc) + GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc + else if (TargetTriple.isOSDarwin()) { + // link all source files into a single module in data segment, rather than + // generating blocks. dynamic_lookup requires that you set + // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for + // bugpoint to just pass that in the environment of GCC. + GCCArgs.push_back("-single_module"); + GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC + GCCArgs.push_back("-undefined"); + GCCArgs.push_back("dynamic_lookup"); + } else + GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others + + if ((TargetTriple.getArch() == Triple::alpha) || + (TargetTriple.getArch() == Triple::x86_64)) + GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC + + if (TargetTriple.getArch() == Triple::sparc) + GCCArgs.push_back("-mcpu=v9"); + + GCCArgs.push_back("-o"); + GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename. + GCCArgs.push_back("-O2"); // Optimize the program a bit. + + + + // Add any arguments intended for GCC. We locate them here because this is + // most likely -L and -l options that need to come before other libraries but + // after the source. Other options won't be sensitive to placement on the + // command line, so this should be safe. + for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) + GCCArgs.push_back(ArgsForGCC[i].c_str()); + GCCArgs.push_back(0); // NULL terminator + + + + outs() << ""; outs().flush(); + DEBUG(errs() << "\nAbout to run:\t"; + for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i) + errs() << " " << GCCArgs[i]; + errs() << "\n"; + ); + if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), + sys::Path())) { + Error = ProcessFailure(GCCPath, &GCCArgs[0]); + return 1; + } + return 0; +} + +/// create - Try to find the `gcc' executable +/// +GCC *GCC::create(std::string &Message, + const std::string &GCCBinary, + const std::vector *Args) { + sys::Path GCCPath = sys::Program::FindProgramByName(GCCBinary); + if (GCCPath.isEmpty()) { + Message = "Cannot find `"+ GCCBinary +"' in PATH!\n"; + return 0; + } + + sys::Path RemoteClientPath; + if (!RemoteClient.empty()) + RemoteClientPath = sys::Program::FindProgramByName(RemoteClient); + + Message = "Found gcc: " + GCCPath.str() + "\n"; + return new GCC(GCCPath, RemoteClientPath, Args); +} diff --git a/contrib/llvm/tools/bugpoint/ToolRunner.h b/contrib/llvm/tools/bugpoint/ToolRunner.h new file mode 100644 index 000000000..cfa8acf6b --- /dev/null +++ b/contrib/llvm/tools/bugpoint/ToolRunner.h @@ -0,0 +1,247 @@ +//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file exposes an abstraction around a platform C compiler, used to +// compile C and assembly code. It also exposes an "AbstractIntepreter" +// interface, which is used to execute code using one of the LLVM execution +// engines. +// +//===----------------------------------------------------------------------===// + +#ifndef BUGPOINT_TOOLRUNNER_H +#define BUGPOINT_TOOLRUNNER_H + +#include "llvm/ADT/Triple.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/Path.h" +#include +#include + +namespace llvm { + +extern cl::opt SaveTemps; +extern Triple TargetTriple; + +class CBE; +class LLC; + +//===---------------------------------------------------------------------===// +// GCC abstraction +// +class GCC { + sys::Path GCCPath; // The path to the gcc executable. + sys::Path RemoteClientPath; // The path to the rsh / ssh executable. + std::vector gccArgs; // GCC-specific arguments. + GCC(const sys::Path &gccPath, const sys::Path &RemotePath, + const std::vector *GCCArgs) + : GCCPath(gccPath), RemoteClientPath(RemotePath) { + if (GCCArgs) gccArgs = *GCCArgs; + } +public: + enum FileType { AsmFile, ObjectFile, CFile }; + + static GCC *create(std::string &Message, + const std::string &GCCBinary, + const std::vector *Args); + + /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is + /// either a .s file, or a .c file, specified by FileType), with the specified + /// arguments. Standard input is specified with InputFile, and standard + /// Output is captured to the specified OutputFile location. The SharedLibs + /// option specifies optional native shared objects that can be loaded into + /// the program for execution. + /// + int ExecuteProgram(const std::string &ProgramFile, + const std::vector &Args, + FileType fileType, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error = 0, + const std::vector &GCCArgs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + + /// MakeSharedObject - This compiles the specified file (which is either a .c + /// file or a .s file) into a shared object. + /// + int MakeSharedObject(const std::string &InputFile, FileType fileType, + std::string &OutputFile, + const std::vector &ArgsForGCC, + std::string &Error); +}; + + +//===---------------------------------------------------------------------===// +/// AbstractInterpreter Class - Subclasses of this class are used to execute +/// LLVM bitcode in a variety of ways. This abstract interface hides this +/// complexity behind a simple interface. +/// +class AbstractInterpreter { +public: + static CBE *createCBE(const char *Argv0, std::string &Message, + const std::string &GCCBinary, + const std::vector *Args = 0, + const std::vector *GCCArgs = 0); + static LLC *createLLC(const char *Argv0, std::string &Message, + const std::string &GCCBinary, + const std::vector *Args = 0, + const std::vector *GCCArgs = 0, + bool UseIntegratedAssembler = false); + + static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message, + const std::vector *Args=0); + + static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message, + const std::vector *Args=0); + + static AbstractInterpreter* + createCustomCompiler(std::string &Message, + const std::string &CompileCommandLine); + + static AbstractInterpreter* + createCustomExecutor(std::string &Message, + const std::string &ExecCommandLine); + + + virtual ~AbstractInterpreter() {} + + /// compileProgram - Compile the specified program from bitcode to executable + /// code. This does not produce any output, it is only used when debugging + /// the code generator. It returns false if the code generator fails. + virtual void compileProgram(const std::string &Bitcode, std::string *Error, + unsigned Timeout = 0, unsigned MemoryLimit = 0) {} + + /// OutputCode - Compile the specified program from bitcode to code + /// understood by the GCC driver (either C or asm). If the code generator + /// fails, it sets Error, otherwise, this function returns the type of code + /// emitted. + virtual GCC::FileType OutputCode(const std::string &Bitcode, + sys::Path &OutFile, std::string &Error, + unsigned Timeout = 0, + unsigned MemoryLimit = 0) { + Error = "OutputCode not supported by this AbstractInterpreter!"; + return GCC::AsmFile; + } + + /// ExecuteProgram - Run the specified bitcode file, emitting output to the + /// specified filename. This sets RetVal to the exit code of the program or + /// returns false if a problem was encountered that prevented execution of + /// the program. + /// + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs = + std::vector(), + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0) = 0; +}; + +//===---------------------------------------------------------------------===// +// CBE Implementation of AbstractIntepreter interface +// +class CBE : public AbstractInterpreter { + sys::Path LLCPath; // The path to the `llc' executable. + std::vector ToolArgs; // Extra args to pass to LLC. + GCC *gcc; +public: + CBE(const sys::Path &llcPath, GCC *Gcc, + const std::vector *Args) + : LLCPath(llcPath), gcc(Gcc) { + ToolArgs.clear (); + if (Args) ToolArgs = *Args; + } + ~CBE() { delete gcc; } + + /// compileProgram - Compile the specified program from bitcode to executable + /// code. This does not produce any output, it is only used when debugging + /// the code generator. Returns false if the code generator fails. + virtual void compileProgram(const std::string &Bitcode, std::string *Error, + unsigned Timeout = 0, unsigned MemoryLimit = 0); + + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs = + std::vector(), + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + + /// OutputCode - Compile the specified program from bitcode to code + /// understood by the GCC driver (either C or asm). If the code generator + /// fails, it sets Error, otherwise, this function returns the type of code + /// emitted. + virtual GCC::FileType OutputCode(const std::string &Bitcode, + sys::Path &OutFile, std::string &Error, + unsigned Timeout = 0, + unsigned MemoryLimit = 0); +}; + + +//===---------------------------------------------------------------------===// +// LLC Implementation of AbstractIntepreter interface +// +class LLC : public AbstractInterpreter { + std::string LLCPath; // The path to the LLC executable. + std::vector ToolArgs; // Extra args to pass to LLC. + GCC *gcc; + bool UseIntegratedAssembler; +public: + LLC(const std::string &llcPath, GCC *Gcc, + const std::vector *Args, + bool useIntegratedAssembler) + : LLCPath(llcPath), gcc(Gcc), + UseIntegratedAssembler(useIntegratedAssembler) { + ToolArgs.clear(); + if (Args) ToolArgs = *Args; + } + ~LLC() { delete gcc; } + + /// compileProgram - Compile the specified program from bitcode to executable + /// code. This does not produce any output, it is only used when debugging + /// the code generator. Returns false if the code generator fails. + virtual void compileProgram(const std::string &Bitcode, std::string *Error, + unsigned Timeout = 0, unsigned MemoryLimit = 0); + + virtual int ExecuteProgram(const std::string &Bitcode, + const std::vector &Args, + const std::string &InputFile, + const std::string &OutputFile, + std::string *Error, + const std::vector &GCCArgs = + std::vector(), + const std::vector &SharedLibs = + std::vector(), + unsigned Timeout = 0, + unsigned MemoryLimit = 0); + + /// OutputCode - Compile the specified program from bitcode to code + /// understood by the GCC driver (either C or asm). If the code generator + /// fails, it sets Error, otherwise, this function returns the type of code + /// emitted. + virtual GCC::FileType OutputCode(const std::string &Bitcode, + sys::Path &OutFile, std::string &Error, + unsigned Timeout = 0, + unsigned MemoryLimit = 0); +}; + +} // End llvm namespace + +#endif diff --git a/contrib/llvm/tools/bugpoint/bugpoint.cpp b/contrib/llvm/tools/bugpoint/bugpoint.cpp new file mode 100644 index 000000000..6a87521a1 --- /dev/null +++ b/contrib/llvm/tools/bugpoint/bugpoint.cpp @@ -0,0 +1,209 @@ +//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is an automated compiler debugger tool. It is used to narrow +// down miscompilations and crash problems to a specific pass in the compiler, +// and the specific Module or Function input that is causing the problem. +// +//===----------------------------------------------------------------------===// + +#include "BugDriver.h" +#include "ToolRunner.h" +#include "llvm/LinkAllPasses.h" +#include "llvm/LLVMContext.h" +#include "llvm/PassManager.h" +#include "llvm/Support/PassNameParser.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PluginLoader.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Process.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/Valgrind.h" +#include "llvm/LinkAllVMCore.h" +#include "llvm/Transforms/IPO/PassManagerBuilder.h" + +//Enable this macro to debug bugpoint itself. +//#define DEBUG_BUGPOINT 1 + +using namespace llvm; + +static cl::opt +FindBugs("find-bugs", cl::desc("Run many different optimization sequences " + "on program to find bugs"), cl::init(false)); + +static cl::list +InputFilenames(cl::Positional, cl::OneOrMore, + cl::desc("")); + +static cl::opt +TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"), + cl::desc("Number of seconds program is allowed to run before it " + "is killed (default is 300s), 0 disables timeout")); + +static cl::opt +MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"), + cl::desc("Maximum amount of memory to use. 0 disables check." + " Defaults to 100MB (800MB under valgrind).")); + +static cl::opt +UseValgrind("enable-valgrind", + cl::desc("Run optimizations through valgrind")); + +// The AnalysesList is automatically populated with registered Passes by the +// PassNameParser. +// +static cl::list +PassList(cl::desc("Passes available:"), cl::ZeroOrMore); + +static cl::opt +StandardCompileOpts("std-compile-opts", + cl::desc("Include the standard compile time optimizations")); + +static cl::opt +StandardLinkOpts("std-link-opts", + cl::desc("Include the standard link time optimizations")); + +static cl::opt +OptLevelO1("O1", + cl::desc("Optimization level 1. Similar to llvm-gcc -O1")); + +static cl::opt +OptLevelO2("O2", + cl::desc("Optimization level 2. Similar to llvm-gcc -O2")); + +static cl::opt +OptLevelO3("O3", + cl::desc("Optimization level 3. Similar to llvm-gcc -O3")); + +static cl::opt +OverrideTriple("mtriple", cl::desc("Override target triple for module")); + +/// BugpointIsInterrupted - Set to true when the user presses ctrl-c. +bool llvm::BugpointIsInterrupted = false; + +#ifndef DEBUG_BUGPOINT +static void BugpointInterruptFunction() { + BugpointIsInterrupted = true; +} +#endif + +// Hack to capture a pass list. +namespace { + class AddToDriver : public FunctionPassManager { + BugDriver &D; + public: + AddToDriver(BugDriver &_D) : FunctionPassManager(0), D(_D) {} + + virtual void add(Pass *P) { + const void *ID = P->getPassID(); + const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); + D.addPass(PI->getPassArgument()); + } + }; +} + +int main(int argc, char **argv) { +#ifndef DEBUG_BUGPOINT + llvm::sys::PrintStackTraceOnErrorSignal(); + llvm::PrettyStackTraceProgram X(argc, argv); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. +#endif + + // Initialize passes + PassRegistry &Registry = *PassRegistry::getPassRegistry(); + initializeCore(Registry); + initializeScalarOpts(Registry); + initializeIPO(Registry); + initializeAnalysis(Registry); + initializeIPA(Registry); + initializeTransformUtils(Registry); + initializeInstCombine(Registry); + initializeInstrumentation(Registry); + initializeTarget(Registry); + + cl::ParseCommandLineOptions(argc, argv, + "LLVM automatic testcase reducer. See\nhttp://" + "llvm.org/cmds/bugpoint.html" + " for more information.\n"); +#ifndef DEBUG_BUGPOINT + sys::SetInterruptFunction(BugpointInterruptFunction); +#endif + + LLVMContext& Context = getGlobalContext(); + // If we have an override, set it and then track the triple we want Modules + // to use. + if (!OverrideTriple.empty()) { + TargetTriple.setTriple(Triple::normalize(OverrideTriple)); + outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; + } + + if (MemoryLimit < 0) { + // Set the default MemoryLimit. Be sure to update the flag's description if + // you change this. + if (sys::RunningOnValgrind() || UseValgrind) + MemoryLimit = 800; + else + MemoryLimit = 100; + } + + BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, + UseValgrind, Context); + if (D.addSources(InputFilenames)) return 1; + + AddToDriver PM(D); + if (StandardCompileOpts) { + PassManagerBuilder Builder; + Builder.OptLevel = 3; + Builder.Inliner = createFunctionInliningPass(); + Builder.populateModulePassManager(PM); + } + + if (StandardLinkOpts) { + PassManagerBuilder Builder; + Builder.populateLTOPassManager(PM, /*Internalize=*/true, + /*RunInliner=*/true); + } + + if (OptLevelO1 || OptLevelO2 || OptLevelO3) { + PassManagerBuilder Builder; + if (OptLevelO1) + Builder.Inliner = createAlwaysInlinerPass(); + else if (OptLevelO2) + Builder.Inliner = createFunctionInliningPass(225); + else + Builder.Inliner = createFunctionInliningPass(275); + + // Note that although clang/llvm-gcc use two separate passmanagers + // here, it shouldn't normally make a difference. + Builder.populateFunctionPassManager(PM); + Builder.populateModulePassManager(PM); + } + + for (std::vector::iterator I = PassList.begin(), + E = PassList.end(); + I != E; ++I) { + const PassInfo* PI = *I; + D.addPass(PI->getPassArgument()); + } + + // Bugpoint has the ability of generating a plethora of core files, so to + // avoid filling up the disk, we prevent it +#ifndef DEBUG_BUGPOINT + sys::Process::PreventCoreFiles(); +#endif + + std::string Error; + bool Failure = D.run(Error); + if (!Error.empty()) { + errs() << Error; + return 1; + } + return Failure; +} diff --git a/contrib/llvm/tools/llc/CMakeLists.txt b/contrib/llvm/tools/llc/CMakeLists.txt new file mode 100644 index 000000000..683f29862 --- /dev/null +++ b/contrib/llvm/tools/llc/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} bitreader asmparser) + +add_llvm_tool(llc + llc.cpp + ) diff --git a/contrib/llvm/tools/llc/Makefile b/contrib/llvm/tools/llc/Makefile new file mode 100644 index 000000000..7319aada4 --- /dev/null +++ b/contrib/llvm/tools/llc/Makefile @@ -0,0 +1,21 @@ +#===- tools/llc/Makefile -----------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llc + +# Include this here so we can get the configuration of the targets +# that have been configured for construction. We have to do this +# early so we can set up LINK_COMPONENTS before including Makefile.rules +include $(LEVEL)/Makefile.config + +LINK_COMPONENTS := $(TARGETS_TO_BUILD) bitreader asmparser + +include $(LLVM_SRC_ROOT)/Makefile.rules + diff --git a/contrib/llvm/tools/llc/llc.cpp b/contrib/llvm/tools/llc/llc.cpp new file mode 100644 index 000000000..d29bd9bc6 --- /dev/null +++ b/contrib/llvm/tools/llc/llc.cpp @@ -0,0 +1,381 @@ +//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This is the llc code generator driver. It provides a convenient +// command-line interface for generating native assembly-language code +// or C code, given LLVM bitcode. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Pass.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Support/IRReader.h" +#include "llvm/CodeGen/LinkAllAsmWriterComponents.h" +#include "llvm/CodeGen/LinkAllCodegenComponents.h" +#include "llvm/Config/config.h" +#include "llvm/MC/SubtargetFeature.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PluginLoader.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" +#include +using namespace llvm; + +// General options for llc. Other pass-specific options are specified +// within the corresponding llc passes, and target-specific options +// and back-end code generation options are specified with the target machine. +// +static cl::opt +InputFilename(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt +OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); + +// Determine optimization level. +static cl::opt +OptLevel("O", + cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " + "(default = '-O2')"), + cl::Prefix, + cl::ZeroOrMore, + cl::init(' ')); + +static cl::opt +TargetTriple("mtriple", cl::desc("Override target triple for module")); + +static cl::opt +MArch("march", cl::desc("Architecture to generate code for (see --version)")); + +static cl::opt +MCPU("mcpu", + cl::desc("Target a specific cpu type (-mcpu=help for details)"), + cl::value_desc("cpu-name"), + cl::init("")); + +static cl::list +MAttrs("mattr", + cl::CommaSeparated, + cl::desc("Target specific attributes (-mattr=help for details)"), + cl::value_desc("a1,+a2,-a3,...")); + +static cl::opt +RelocModel("relocation-model", + cl::desc("Choose relocation model"), + cl::init(Reloc::Default), + cl::values( + clEnumValN(Reloc::Default, "default", + "Target default relocation model"), + clEnumValN(Reloc::Static, "static", + "Non-relocatable code"), + clEnumValN(Reloc::PIC_, "pic", + "Fully relocatable, position independent code"), + clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", + "Relocatable external references, non-relocatable code"), + clEnumValEnd)); + +static cl::opt +CMModel("code-model", + cl::desc("Choose code model"), + cl::init(CodeModel::Default), + cl::values(clEnumValN(CodeModel::Default, "default", + "Target default code model"), + clEnumValN(CodeModel::Small, "small", + "Small code model"), + clEnumValN(CodeModel::Kernel, "kernel", + "Kernel code model"), + clEnumValN(CodeModel::Medium, "medium", + "Medium code model"), + clEnumValN(CodeModel::Large, "large", + "Large code model"), + clEnumValEnd)); + +static cl::opt +RelaxAll("mc-relax-all", + cl::desc("When used with filetype=obj, " + "relax all fixups in the emitted object file")); + +cl::opt +FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile), + cl::desc("Choose a file type (not all types are supported by all targets):"), + cl::values( + clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm", + "Emit an assembly ('.s') file"), + clEnumValN(TargetMachine::CGFT_ObjectFile, "obj", + "Emit a native object ('.o') file [experimental]"), + clEnumValN(TargetMachine::CGFT_Null, "null", + "Emit nothing, for performance testing"), + clEnumValEnd)); + +cl::opt NoVerify("disable-verify", cl::Hidden, + cl::desc("Do not verify input module")); + +cl::opt DisableDotLoc("disable-dot-loc", cl::Hidden, + cl::desc("Do not use .loc entries")); + +cl::opt DisableCFI("disable-cfi", cl::Hidden, + cl::desc("Do not use .cfi_* directives")); + +static cl::opt +DisableRedZone("disable-red-zone", + cl::desc("Do not emit code that uses the red zone."), + cl::init(false)); + +// GetFileNameRoot - Helper function to get the basename of a filename. +static inline std::string +GetFileNameRoot(const std::string &InputFilename) { + std::string IFN = InputFilename; + std::string outputFilename; + int Len = IFN.length(); + if ((Len > 2) && + IFN[Len-3] == '.' && + ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') || + (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) { + outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ + } else { + outputFilename = IFN; + } + return outputFilename; +} + +static tool_output_file *GetOutputStream(const char *TargetName, + Triple::OSType OS, + const char *ProgName) { + // If we don't yet have an output filename, make one. + if (OutputFilename.empty()) { + if (InputFilename == "-") + OutputFilename = "-"; + else { + OutputFilename = GetFileNameRoot(InputFilename); + + switch (FileType) { + default: assert(0 && "Unknown file type"); + case TargetMachine::CGFT_AssemblyFile: + if (TargetName[0] == 'c') { + if (TargetName[1] == 0) + OutputFilename += ".cbe.c"; + else if (TargetName[1] == 'p' && TargetName[2] == 'p') + OutputFilename += ".cpp"; + else + OutputFilename += ".s"; + } else + OutputFilename += ".s"; + break; + case TargetMachine::CGFT_ObjectFile: + if (OS == Triple::Win32) + OutputFilename += ".obj"; + else + OutputFilename += ".o"; + break; + case TargetMachine::CGFT_Null: + OutputFilename += ".null"; + break; + } + } + } + + // Decide if we need "binary" output. + bool Binary = false; + switch (FileType) { + default: assert(0 && "Unknown file type"); + case TargetMachine::CGFT_AssemblyFile: + break; + case TargetMachine::CGFT_ObjectFile: + case TargetMachine::CGFT_Null: + Binary = true; + break; + } + + // Open the file. + std::string error; + unsigned OpenFlags = 0; + if (Binary) OpenFlags |= raw_fd_ostream::F_Binary; + tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error, + OpenFlags); + if (!error.empty()) { + errs() << error << '\n'; + delete FDOut; + return 0; + } + + return FDOut; +} + +// main - Entry point for the llc compiler. +// +int main(int argc, char **argv) { + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + // Enable debug stream buffering. + EnableDebugBuffering = true; + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + // Initialize targets first, so that --version shows registered targets. + InitializeAllTargets(); + InitializeAllTargetMCs(); + InitializeAllAsmPrinters(); + InitializeAllAsmParsers(); + + // Register the target printer for --version. + cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); + + cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); + + // Load the module to be compiled... + SMDiagnostic Err; + std::auto_ptr M; + + M.reset(ParseIRFile(InputFilename, Err, Context)); + if (M.get() == 0) { + Err.Print(argv[0], errs()); + return 1; + } + Module &mod = *M.get(); + + // If we are supposed to override the target triple, do so now. + if (!TargetTriple.empty()) + mod.setTargetTriple(Triple::normalize(TargetTriple)); + + Triple TheTriple(mod.getTargetTriple()); + if (TheTriple.getTriple().empty()) + TheTriple.setTriple(sys::getHostTriple()); + + // Allocate target machine. First, check whether the user has explicitly + // specified an architecture to compile for. If so we have to look it up by + // name, because it might be a backend that has no mapping to a target triple. + const Target *TheTarget = 0; + if (!MArch.empty()) { + for (TargetRegistry::iterator it = TargetRegistry::begin(), + ie = TargetRegistry::end(); it != ie; ++it) { + if (MArch == it->getName()) { + TheTarget = &*it; + break; + } + } + + if (!TheTarget) { + errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; + return 1; + } + + // Adjust the triple to match (if known), otherwise stick with the + // module/host triple. + Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); + if (Type != Triple::UnknownArch) + TheTriple.setArch(Type); + } else { + std::string Err; + TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); + if (TheTarget == 0) { + errs() << argv[0] << ": error auto-selecting target for module '" + << Err << "'. Please use the -march option to explicitly " + << "pick a target.\n"; + return 1; + } + } + + // Package up features to be passed to target/subtarget + std::string FeaturesStr; + if (MAttrs.size()) { + SubtargetFeatures Features; + for (unsigned i = 0; i != MAttrs.size(); ++i) + Features.AddFeature(MAttrs[i]); + FeaturesStr = Features.getString(); + } + + std::auto_ptr + target(TheTarget->createTargetMachine(TheTriple.getTriple(), + MCPU, FeaturesStr, + RelocModel, CMModel)); + assert(target.get() && "Could not allocate target machine!"); + TargetMachine &Target = *target.get(); + + if (DisableDotLoc) + Target.setMCUseLoc(false); + + if (DisableCFI) + Target.setMCUseCFI(false); + + // Disable .loc support for older OS X versions. + if (TheTriple.isMacOSX() && + TheTriple.isMacOSXVersionLT(10, 6)) + Target.setMCUseLoc(false); + + // Figure out where we are going to send the output... + OwningPtr Out + (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0])); + if (!Out) return 1; + + CodeGenOpt::Level OLvl = CodeGenOpt::Default; + switch (OptLevel) { + default: + errs() << argv[0] << ": invalid optimization level.\n"; + return 1; + case ' ': break; + case '0': OLvl = CodeGenOpt::None; break; + case '1': OLvl = CodeGenOpt::Less; break; + case '2': OLvl = CodeGenOpt::Default; break; + case '3': OLvl = CodeGenOpt::Aggressive; break; + } + + // Build up all of the passes that we want to do to the module. + PassManager PM; + + // Add the target data from the target machine, if it exists, or the module. + if (const TargetData *TD = Target.getTargetData()) + PM.add(new TargetData(*TD)); + else + PM.add(new TargetData(&mod)); + + // Override default to generate verbose assembly. + Target.setAsmVerbosityDefault(true); + + if (RelaxAll) { + if (FileType != TargetMachine::CGFT_ObjectFile) + errs() << argv[0] + << ": warning: ignoring -mc-relax-all because filetype != obj"; + else + Target.setMCRelaxAll(true); + } + + { + formatted_raw_ostream FOS(Out->os()); + + // Ask the target to add backend passes as necessary. + if (Target.addPassesToEmitFile(PM, FOS, FileType, OLvl, NoVerify)) { + errs() << argv[0] << ": target does not support generation of this" + << " file type!\n"; + return 1; + } + + // Before executing passes, print the final values of the LLVM options. + cl::PrintOptionValues(); + + PM.run(mod); + } + + // Declare success. + Out->keep(); + + return 0; +} diff --git a/contrib/llvm/tools/lli/CMakeLists.txt b/contrib/llvm/tools/lli/CMakeLists.txt new file mode 100644 index 000000000..9378ef255 --- /dev/null +++ b/contrib/llvm/tools/lli/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS mcjit jit interpreter nativecodegen bitreader asmparser selectiondag) + +add_llvm_tool(lli + lli.cpp + ) diff --git a/contrib/llvm/tools/lli/Makefile b/contrib/llvm/tools/lli/Makefile new file mode 100644 index 000000000..80aa82b4d --- /dev/null +++ b/contrib/llvm/tools/lli/Makefile @@ -0,0 +1,15 @@ +##===- tools/lli/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL := ../.. +TOOLNAME := lli +LINK_COMPONENTS := mcjit jit interpreter nativecodegen bitreader asmparser selectiondag + +# Enable JIT support +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/lli/lli.cpp b/contrib/llvm/tools/lli/lli.cpp new file mode 100644 index 000000000..50c7a498f --- /dev/null +++ b/contrib/llvm/tools/lli/lli.cpp @@ -0,0 +1,305 @@ +//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility provides a simple wrapper around the LLVM Execution Engines, +// which allow the direct execution of LLVM programs through a Just-In-Time +// compiler, or through an interpreter if no JIT is available for this platform. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Type.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/CodeGen/LinkAllCodegenComponents.h" +#include "llvm/ExecutionEngine/GenericValue.h" +#include "llvm/ExecutionEngine/Interpreter.h" +#include "llvm/ExecutionEngine/JIT.h" +#include "llvm/ExecutionEngine/JITEventListener.h" +#include "llvm/ExecutionEngine/MCJIT.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PluginLoader.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Process.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/TargetSelect.h" +#include + +#ifdef __CYGWIN__ +#include +#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007 +#define DO_NOTHING_ATEXIT 1 +#endif +#endif + +using namespace llvm; + +namespace { + cl::opt + InputFile(cl::desc(""), cl::Positional, cl::init("-")); + + cl::list + InputArgv(cl::ConsumeAfter, cl::desc("...")); + + cl::opt ForceInterpreter("force-interpreter", + cl::desc("Force interpretation: disable JIT"), + cl::init(false)); + + cl::opt UseMCJIT( + "use-mcjit", cl::desc("Enable use of the MC-based JIT (if available)"), + cl::init(false)); + + // Determine optimization level. + cl::opt + OptLevel("O", + cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " + "(default = '-O2')"), + cl::Prefix, + cl::ZeroOrMore, + cl::init(' ')); + + cl::opt + TargetTriple("mtriple", cl::desc("Override target triple for module")); + + cl::opt + MArch("march", + cl::desc("Architecture to generate assembly for (see --version)")); + + cl::opt + MCPU("mcpu", + cl::desc("Target a specific cpu type (-mcpu=help for details)"), + cl::value_desc("cpu-name"), + cl::init("")); + + cl::list + MAttrs("mattr", + cl::CommaSeparated, + cl::desc("Target specific attributes (-mattr=help for details)"), + cl::value_desc("a1,+a2,-a3,...")); + + cl::opt + EntryFunc("entry-function", + cl::desc("Specify the entry function (default = 'main') " + "of the executable"), + cl::value_desc("function"), + cl::init("main")); + + cl::opt + FakeArgv0("fake-argv0", + cl::desc("Override the 'argv[0]' value passed into the executing" + " program"), cl::value_desc("executable")); + + cl::opt + DisableCoreFiles("disable-core-files", cl::Hidden, + cl::desc("Disable emission of core files if possible")); + + cl::opt + NoLazyCompilation("disable-lazy-compilation", + cl::desc("Disable JIT lazy compilation"), + cl::init(false)); + + cl::opt + RelocModel("relocation-model", + cl::desc("Choose relocation model"), + cl::init(Reloc::Default), + cl::values( + clEnumValN(Reloc::Default, "default", + "Target default relocation model"), + clEnumValN(Reloc::Static, "static", + "Non-relocatable code"), + clEnumValN(Reloc::PIC_, "pic", + "Fully relocatable, position independent code"), + clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", + "Relocatable external references, non-relocatable code"), + clEnumValEnd)); + + cl::opt + CMModel("code-model", + cl::desc("Choose code model"), + cl::init(CodeModel::JITDefault), + cl::values(clEnumValN(CodeModel::JITDefault, "default", + "Target default JIT code model"), + clEnumValN(CodeModel::Small, "small", + "Small code model"), + clEnumValN(CodeModel::Kernel, "kernel", + "Kernel code model"), + clEnumValN(CodeModel::Medium, "medium", + "Medium code model"), + clEnumValN(CodeModel::Large, "large", + "Large code model"), + clEnumValEnd)); + +} + +static ExecutionEngine *EE = 0; + +static void do_shutdown() { + // Cygwin-1.5 invokes DLL's dtors before atexit handler. +#ifndef DO_NOTHING_ATEXIT + delete EE; + llvm_shutdown(); +#endif +} + +//===----------------------------------------------------------------------===// +// main Driver function +// +int main(int argc, char **argv, char * const *envp) { + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + atexit(do_shutdown); // Call llvm_shutdown() on exit. + + // If we have a native target, initialize it to ensure it is linked in and + // usable by the JIT. + InitializeNativeTarget(); + InitializeNativeTargetAsmPrinter(); + + cl::ParseCommandLineOptions(argc, argv, + "llvm interpreter & dynamic compiler\n"); + + // If the user doesn't want core files, disable them. + if (DisableCoreFiles) + sys::Process::PreventCoreFiles(); + + // Load the bitcode... + SMDiagnostic Err; + Module *Mod = ParseIRFile(InputFile, Err, Context); + if (!Mod) { + Err.Print(argv[0], errs()); + return 1; + } + + // If not jitting lazily, load the whole bitcode file eagerly too. + std::string ErrorMsg; + if (NoLazyCompilation) { + if (Mod->MaterializeAllPermanently(&ErrorMsg)) { + errs() << argv[0] << ": bitcode didn't read correctly.\n"; + errs() << "Reason: " << ErrorMsg << "\n"; + exit(1); + } + } + + EngineBuilder builder(Mod); + builder.setMArch(MArch); + builder.setMCPU(MCPU); + builder.setMAttrs(MAttrs); + builder.setRelocationModel(RelocModel); + builder.setCodeModel(CMModel); + builder.setErrorStr(&ErrorMsg); + builder.setEngineKind(ForceInterpreter + ? EngineKind::Interpreter + : EngineKind::JIT); + + // If we are supposed to override the target triple, do so now. + if (!TargetTriple.empty()) + Mod->setTargetTriple(Triple::normalize(TargetTriple)); + + // Enable MCJIT, if desired. + if (UseMCJIT) + builder.setUseMCJIT(true); + + CodeGenOpt::Level OLvl = CodeGenOpt::Default; + switch (OptLevel) { + default: + errs() << argv[0] << ": invalid optimization level.\n"; + return 1; + case ' ': break; + case '0': OLvl = CodeGenOpt::None; break; + case '1': OLvl = CodeGenOpt::Less; break; + case '2': OLvl = CodeGenOpt::Default; break; + case '3': OLvl = CodeGenOpt::Aggressive; break; + } + builder.setOptLevel(OLvl); + + EE = builder.create(); + if (!EE) { + if (!ErrorMsg.empty()) + errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n"; + else + errs() << argv[0] << ": unknown error creating EE!\n"; + exit(1); + } + + EE->RegisterJITEventListener(createOProfileJITEventListener()); + + EE->DisableLazyCompilation(NoLazyCompilation); + + // If the user specifically requested an argv[0] to pass into the program, + // do it now. + if (!FakeArgv0.empty()) { + InputFile = FakeArgv0; + } else { + // Otherwise, if there is a .bc suffix on the executable strip it off, it + // might confuse the program. + if (StringRef(InputFile).endswith(".bc")) + InputFile.erase(InputFile.length() - 3); + } + + // Add the module's name to the start of the vector of arguments to main(). + InputArgv.insert(InputArgv.begin(), InputFile); + + // Call the main function from M as if its signature were: + // int main (int argc, char **argv, const char **envp) + // using the contents of Args to determine argc & argv, and the contents of + // EnvVars to determine envp. + // + Function *EntryFn = Mod->getFunction(EntryFunc); + if (!EntryFn) { + errs() << '\'' << EntryFunc << "\' function not found in module.\n"; + return -1; + } + + // If the program doesn't explicitly call exit, we will need the Exit + // function later on to make an explicit call, so get the function now. + Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), + Type::getInt32Ty(Context), + NULL); + + // Reset errno to zero on entry to main. + errno = 0; + + // Run static constructors. + EE->runStaticConstructorsDestructors(false); + + if (NoLazyCompilation) { + for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { + Function *Fn = &*I; + if (Fn != EntryFn && !Fn->isDeclaration()) + EE->getPointerToFunction(Fn); + } + } + + // Run main. + int Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); + + // Run static destructors. + EE->runStaticConstructorsDestructors(true); + + // If the program didn't call exit explicitly, we should call it now. + // This ensures that any atexit handlers get called correctly. + if (Function *ExitF = dyn_cast(Exit)) { + std::vector Args; + GenericValue ResultGV; + ResultGV.IntVal = APInt(32, Result); + Args.push_back(ResultGV); + EE->runFunction(ExitF, Args); + errs() << "ERROR: exit(" << Result << ") returned!\n"; + abort(); + } else { + errs() << "ERROR: exit defined with wrong prototype!\n"; + abort(); + } +} diff --git a/contrib/llvm/tools/llvm-ar/CMakeLists.txt b/contrib/llvm/tools/llvm-ar/CMakeLists.txt new file mode 100644 index 000000000..c8b0b725d --- /dev/null +++ b/contrib/llvm/tools/llvm-ar/CMakeLists.txt @@ -0,0 +1,8 @@ +set(LLVM_LINK_COMPONENTS archive) +set(LLVM_REQUIRES_EH 1) + +add_llvm_tool(llvm-ar + llvm-ar.cpp + ) + +# TODO: Support check-local. diff --git a/contrib/llvm/tools/llvm-ar/Makefile b/contrib/llvm/tools/llvm-ar/Makefile new file mode 100644 index 000000000..e4fe4e8ca --- /dev/null +++ b/contrib/llvm/tools/llvm-ar/Makefile @@ -0,0 +1,25 @@ +##===- tools/llvm-ar/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-ar +LINK_COMPONENTS = archive +REQUIRES_EH := 1 + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common + +check-local:: + $(Echo) Checking llvm-ar + $(Verb) $(ToolDir)/llvm-ar zRrS nada.a . + $(Verb) $(ToolDir)/llvm-ar tv nada.a | \ + grep Debug/llvm-ar.d >/dev/null 2>&1 + $(Verb) $(RM) -f nada.a diff --git a/contrib/llvm/tools/llvm-ar/llvm-ar.cpp b/contrib/llvm/tools/llvm-ar/llvm-ar.cpp new file mode 100644 index 000000000..c1c8b2474 --- /dev/null +++ b/contrib/llvm/tools/llvm-ar/llvm-ar.cpp @@ -0,0 +1,781 @@ +//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Builds up (relatively) standard unix archive files (.a) containing LLVM +// bitcode or other files. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Bitcode/Archive.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" +#include +#include +#include +using namespace llvm; + +// Option for compatibility with AIX, not used but must allow it to be present. +static cl::opt +X32Option ("X32_64", cl::Hidden, + cl::desc("Ignored option for compatibility with AIX")); + +// llvm-ar operation code and modifier flags. This must come first. +static cl::opt +Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]...")); + +// llvm-ar remaining positional arguments. +static cl::list +RestOfArgs(cl::Positional, cl::OneOrMore, + cl::desc("[relpos] [count] [members]...")); + +// MoreHelp - Provide additional help output explaining the operations and +// modifiers of llvm-ar. This object instructs the CommandLine library +// to print the text of the constructor when the --help option is given. +static cl::extrahelp MoreHelp( + "\nOPERATIONS:\n" + " d[NsS] - delete file(s) from the archive\n" + " m[abiSs] - move file(s) in the archive\n" + " p[kN] - print file(s) found in the archive\n" + " q[ufsS] - quick append file(s) to the archive\n" + " r[abfiuzRsS] - replace or insert file(s) into the archive\n" + " t - display contents of archive\n" + " x[No] - extract file(s) from the archive\n" + "\nMODIFIERS (operation specific):\n" + " [a] - put file(s) after [relpos]\n" + " [b] - put file(s) before [relpos] (same as [i])\n" + " [f] - truncate inserted file names\n" + " [i] - put file(s) before [relpos] (same as [b])\n" + " [k] - always print bitcode files (default is to skip them)\n" + " [N] - use instance [count] of name\n" + " [o] - preserve original dates\n" + " [P] - use full path names when matching\n" + " [R] - recurse through directories when inserting\n" + " [s] - create an archive index (cf. ranlib)\n" + " [S] - do not build a symbol table\n" + " [u] - update only files newer than archive contents\n" + " [z] - compress files before inserting/extracting\n" + "\nMODIFIERS (generic):\n" + " [c] - do not warn if the library had to be created\n" + " [v] - be verbose about actions taken\n" + " [V] - be *really* verbose about actions taken\n" +); + +// This enumeration delineates the kinds of operations on an archive +// that are permitted. +enum ArchiveOperation { + NoOperation, ///< An operation hasn't been specified + Print, ///< Print the contents of the archive + Delete, ///< Delete the specified members + Move, ///< Move members to end or as given by {a,b,i} modifiers + QuickAppend, ///< Quickly append to end of archive + ReplaceOrInsert, ///< Replace or Insert members + DisplayTable, ///< Display the table of contents + Extract ///< Extract files back to file system +}; + +// Modifiers to follow operation to vary behavior +bool AddAfter = false; ///< 'a' modifier +bool AddBefore = false; ///< 'b' modifier +bool Create = false; ///< 'c' modifier +bool TruncateNames = false; ///< 'f' modifier +bool InsertBefore = false; ///< 'i' modifier +bool DontSkipBitcode = false; ///< 'k' modifier +bool UseCount = false; ///< 'N' modifier +bool OriginalDates = false; ///< 'o' modifier +bool FullPath = false; ///< 'P' modifier +bool RecurseDirectories = false; ///< 'R' modifier +bool SymTable = true; ///< 's' & 'S' modifiers +bool OnlyUpdate = false; ///< 'u' modifier +bool Verbose = false; ///< 'v' modifier +bool ReallyVerbose = false; ///< 'V' modifier +bool Compression = false; ///< 'z' modifier + +// Relative Positional Argument (for insert/move). This variable holds +// the name of the archive member to which the 'a', 'b' or 'i' modifier +// refers. Only one of 'a', 'b' or 'i' can be specified so we only need +// one variable. +std::string RelPos; + +// Select which of multiple entries in the archive with the same name should be +// used (specified with -N) for the delete and extract operations. +int Count = 1; + +// This variable holds the name of the archive file as given on the +// command line. +std::string ArchiveName; + +// This variable holds the list of member files to proecess, as given +// on the command line. +std::vector Members; + +// This variable holds the (possibly expanded) list of path objects that +// correspond to files we will +std::set Paths; + +// The Archive object to which all the editing operations will be sent. +Archive* TheArchive = 0; + +// getRelPos - Extract the member filename from the command line for +// the [relpos] argument associated with a, b, and i modifiers +void getRelPos() { + if(RestOfArgs.size() > 0) { + RelPos = RestOfArgs[0]; + RestOfArgs.erase(RestOfArgs.begin()); + } + else + throw "Expected [relpos] for a, b, or i modifier"; +} + +// getCount - Extract the [count] argument associated with the N modifier +// from the command line and check its value. +void getCount() { + if(RestOfArgs.size() > 0) { + Count = atoi(RestOfArgs[0].c_str()); + RestOfArgs.erase(RestOfArgs.begin()); + } + else + throw "Expected [count] value with N modifier"; + + // Non-positive counts are not allowed + if (Count < 1) + throw "Invalid [count] value (not a positive integer)"; +} + +// getArchive - Get the archive file name from the command line +void getArchive() { + if(RestOfArgs.size() > 0) { + ArchiveName = RestOfArgs[0]; + RestOfArgs.erase(RestOfArgs.begin()); + } + else + throw "An archive name must be specified."; +} + +// getMembers - Copy over remaining items in RestOfArgs to our Members vector +// This is just for clarity. +void getMembers() { + if(RestOfArgs.size() > 0) + Members = std::vector(RestOfArgs); +} + +// parseCommandLine - Parse the command line options as presented and return the +// operation specified. Process all modifiers and check to make sure that +// constraints on modifier/operation pairs have not been violated. +ArchiveOperation parseCommandLine() { + + // Keep track of number of operations. We can only specify one + // per execution. + unsigned NumOperations = 0; + + // Keep track of the number of positional modifiers (a,b,i). Only + // one can be specified. + unsigned NumPositional = 0; + + // Keep track of which operation was requested + ArchiveOperation Operation = NoOperation; + + for(unsigned i=0; i 1) + throw "Only one operation may be specified"; + if (NumPositional > 1) + throw "You may only specify one of a, b, and i modifiers"; + if (AddAfter || AddBefore || InsertBefore) + if (Operation != Move && Operation != ReplaceOrInsert) + throw "The 'a', 'b' and 'i' modifiers can only be specified with " + "the 'm' or 'r' operations"; + if (RecurseDirectories && Operation != ReplaceOrInsert) + throw "The 'R' modifiers is only applicabe to the 'r' operation"; + if (OriginalDates && Operation != Extract) + throw "The 'o' modifier is only applicable to the 'x' operation"; + if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert) + throw "The 'f' modifier is only applicable to the 'q' and 'r' operations"; + if (OnlyUpdate && Operation != ReplaceOrInsert) + throw "The 'u' modifier is only applicable to the 'r' operation"; + if (Compression && Operation!=ReplaceOrInsert && Operation!=Extract) + throw "The 'z' modifier is only applicable to the 'r' and 'x' operations"; + if (Count > 1 && Members.size() > 1) + throw "Only one member name may be specified with the 'N' modifier"; + + // Return the parsed operation to the caller + return Operation; +} + +// recurseDirectories - Implements the "R" modifier. This function scans through +// the Paths vector (built by buildPaths, below) and replaces any directories it +// finds with all the files in that directory (recursively). It uses the +// sys::Path::getDirectoryContent method to perform the actual directory scans. +bool +recurseDirectories(const sys::Path& path, + std::set& result, std::string* ErrMsg) { + result.clear(); + if (RecurseDirectories) { + std::set content; + if (path.getDirectoryContents(content, ErrMsg)) + return true; + + for (std::set::iterator I = content.begin(), E = content.end(); + I != E; ++I) { + // Make sure it exists and is a directory + sys::PathWithStatus PwS(*I); + const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg); + if (!Status) + return true; + if (Status->isDir) { + std::set moreResults; + if (recurseDirectories(*I, moreResults, ErrMsg)) + return true; + result.insert(moreResults.begin(), moreResults.end()); + } else { + result.insert(*I); + } + } + } + return false; +} + +// buildPaths - Convert the strings in the Members vector to sys::Path objects +// and make sure they are valid and exist exist. This check is only needed for +// the operations that add/replace files to the archive ('q' and 'r') +bool buildPaths(bool checkExistence, std::string* ErrMsg) { + for (unsigned i = 0; i < Members.size(); i++) { + sys::Path aPath; + if (!aPath.set(Members[i])) + throw std::string("File member name invalid: ") + Members[i]; + if (checkExistence) { + bool Exists; + if (sys::fs::exists(aPath.str(), Exists) || !Exists) + throw std::string("File does not exist: ") + Members[i]; + std::string Err; + sys::PathWithStatus PwS(aPath); + const sys::FileStatus *si = PwS.getFileStatus(false, &Err); + if (!si) + throw Err; + if (si->isDir) { + std::set dirpaths; + if (recurseDirectories(aPath, dirpaths, ErrMsg)) + return true; + Paths.insert(dirpaths.begin(),dirpaths.end()); + } else { + Paths.insert(aPath); + } + } else { + Paths.insert(aPath); + } + } + return false; +} + +// printSymbolTable - print out the archive's symbol table. +void printSymbolTable() { + outs() << "\nArchive Symbol Table:\n"; + const Archive::SymTabType& symtab = TheArchive->getSymbolTable(); + for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); + I != E; ++I ) { + unsigned offset = TheArchive->getFirstFileOffset() + I->second; + outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n"; + } +} + +// doPrint - Implements the 'p' operation. This function traverses the archive +// looking for members that match the path list. It is careful to uncompress +// things that should be and to skip bitcode files unless the 'k' modifier was +// given. +bool doPrint(std::string* ErrMsg) { + if (buildPaths(false, ErrMsg)) + return true; + unsigned countDown = Count; + for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); + I != E; ++I ) { + if (Paths.empty() || + (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { + if (countDown == 1) { + const char* data = reinterpret_cast(I->getData()); + + // Skip things that don't make sense to print + if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() || + I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode())) + continue; + + if (Verbose) + outs() << "Printing " << I->getPath().str() << "\n"; + + unsigned len = I->getSize(); + outs().write(data, len); + } else { + countDown--; + } + } + } + return false; +} + +// putMode - utility function for printing out the file mode when the 't' +// operation is in verbose mode. +void +printMode(unsigned mode) { + if (mode & 004) + outs() << "r"; + else + outs() << "-"; + if (mode & 002) + outs() << "w"; + else + outs() << "-"; + if (mode & 001) + outs() << "x"; + else + outs() << "-"; +} + +// doDisplayTable - Implement the 't' operation. This function prints out just +// the file names of each of the members. However, if verbose mode is requested +// ('v' modifier) then the file type, permission mode, user, group, size, and +// modification time are also printed. +bool +doDisplayTable(std::string* ErrMsg) { + if (buildPaths(false, ErrMsg)) + return true; + for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); + I != E; ++I ) { + if (Paths.empty() || + (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { + if (Verbose) { + // FIXME: Output should be this format: + // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile + if (I->isBitcode()) + outs() << "b"; + else if (I->isCompressed()) + outs() << "Z"; + else + outs() << " "; + unsigned mode = I->getMode(); + printMode((mode >> 6) & 007); + printMode((mode >> 3) & 007); + printMode(mode & 007); + outs() << " " << format("%4u", I->getUser()); + outs() << "/" << format("%4u", I->getGroup()); + outs() << " " << format("%8u", I->getSize()); + outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str()); + outs() << " " << I->getPath().str() << "\n"; + } else { + outs() << I->getPath().str() << "\n"; + } + } + } + if (ReallyVerbose) + printSymbolTable(); + return false; +} + +// doExtract - Implement the 'x' operation. This function extracts files back to +// the file system, making sure to uncompress any that were compressed +bool +doExtract(std::string* ErrMsg) { + if (buildPaths(false, ErrMsg)) + return true; + for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); + I != E; ++I ) { + if (Paths.empty() || + (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { + + // Make sure the intervening directories are created + if (I->hasPath()) { + sys::Path dirs(I->getPath()); + dirs.eraseComponent(); + if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg)) + return true; + } + + // Open up a file stream for writing + std::ios::openmode io_mode = std::ios::out | std::ios::trunc | + std::ios::binary; + std::ofstream file(I->getPath().c_str(), io_mode); + + // Get the data and its length + const char* data = reinterpret_cast(I->getData()); + unsigned len = I->getSize(); + + // Write the data. + file.write(data,len); + file.close(); + + // If we're supposed to retain the original modification times, etc. do so + // now. + if (OriginalDates) + I->getPath().setStatusInfoOnDisk(I->getFileStatus()); + } + } + return false; +} + +// doDelete - Implement the delete operation. This function deletes zero or more +// members from the archive. Note that if the count is specified, there should +// be no more than one path in the Paths list or else this algorithm breaks. +// That check is enforced in parseCommandLine (above). +bool +doDelete(std::string* ErrMsg) { + if (buildPaths(false, ErrMsg)) + return true; + if (Paths.empty()) + return false; + unsigned countDown = Count; + for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); + I != E; ) { + if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) { + if (countDown == 1) { + Archive::iterator J = I; + ++I; + TheArchive->erase(J); + } else + countDown--; + } else { + ++I; + } + } + + // We're done editting, reconstruct the archive. + if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg)) + return true; + if (ReallyVerbose) + printSymbolTable(); + return false; +} + +// doMore - Implement the move operation. This function re-arranges just the +// order of the archive members so that when the archive is written the move +// of the members is accomplished. Note the use of the RelPos variable to +// determine where the items should be moved to. +bool +doMove(std::string* ErrMsg) { + if (buildPaths(false, ErrMsg)) + return true; + + // By default and convention the place to move members to is the end of the + // archive. + Archive::iterator moveto_spot = TheArchive->end(); + + // However, if the relative positioning modifiers were used, we need to scan + // the archive to find the member in question. If we don't find it, its no + // crime, we just move to the end. + if (AddBefore || InsertBefore || AddAfter) { + for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end(); + I != E; ++I ) { + if (RelPos == I->getPath().str()) { + if (AddAfter) { + moveto_spot = I; + moveto_spot++; + } else { + moveto_spot = I; + } + break; + } + } + } + + // Keep a list of the paths remaining to be moved + std::set remaining(Paths); + + // Scan the archive again, this time looking for the members to move to the + // moveto_spot. + for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end(); + I != E && !remaining.empty(); ++I ) { + std::set::iterator found = + std::find(remaining.begin(),remaining.end(),I->getPath()); + if (found != remaining.end()) { + if (I != moveto_spot) + TheArchive->splice(moveto_spot,*TheArchive,I); + remaining.erase(found); + } + } + + // We're done editting, reconstruct the archive. + if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg)) + return true; + if (ReallyVerbose) + printSymbolTable(); + return false; +} + +// doQuickAppend - Implements the 'q' operation. This function just +// indiscriminantly adds the members to the archive and rebuilds it. +bool +doQuickAppend(std::string* ErrMsg) { + // Get the list of paths to append. + if (buildPaths(true, ErrMsg)) + return true; + if (Paths.empty()) + return false; + + // Append them quickly. + for (std::set::iterator PI = Paths.begin(), PE = Paths.end(); + PI != PE; ++PI) { + if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg)) + return true; + } + + // We're done editting, reconstruct the archive. + if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg)) + return true; + if (ReallyVerbose) + printSymbolTable(); + return false; +} + +// doReplaceOrInsert - Implements the 'r' operation. This function will replace +// any existing files or insert new ones into the archive. +bool +doReplaceOrInsert(std::string* ErrMsg) { + + // Build the list of files to be added/replaced. + if (buildPaths(true, ErrMsg)) + return true; + if (Paths.empty()) + return false; + + // Keep track of the paths that remain to be inserted. + std::set remaining(Paths); + + // Default the insertion spot to the end of the archive + Archive::iterator insert_spot = TheArchive->end(); + + // Iterate over the archive contents + for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); + I != E && !remaining.empty(); ++I ) { + + // Determine if this archive member matches one of the paths we're trying + // to replace. + + std::set::iterator found = remaining.end(); + for (std::set::iterator RI = remaining.begin(), + RE = remaining.end(); RI != RE; ++RI ) { + std::string compare(RI->str()); + if (TruncateNames && compare.length() > 15) { + const char* nm = compare.c_str(); + unsigned len = compare.length(); + size_t slashpos = compare.rfind('/'); + if (slashpos != std::string::npos) { + nm += slashpos + 1; + len -= slashpos +1; + } + if (len > 15) + len = 15; + compare.assign(nm,len); + } + if (compare == I->getPath().str()) { + found = RI; + break; + } + } + + if (found != remaining.end()) { + std::string Err; + sys::PathWithStatus PwS(*found); + const sys::FileStatus *si = PwS.getFileStatus(false, &Err); + if (!si) + return true; + if (!si->isDir) { + if (OnlyUpdate) { + // Replace the item only if it is newer. + if (si->modTime > I->getModTime()) + if (I->replaceWith(*found, ErrMsg)) + return true; + } else { + // Replace the item regardless of time stamp + if (I->replaceWith(*found, ErrMsg)) + return true; + } + } else { + // We purposefully ignore directories. + } + + // Remove it from our "to do" list + remaining.erase(found); + } + + // Determine if this is the place where we should insert + if ((AddBefore || InsertBefore) && RelPos == I->getPath().str()) + insert_spot = I; + else if (AddAfter && RelPos == I->getPath().str()) { + insert_spot = I; + insert_spot++; + } + } + + // If we didn't replace all the members, some will remain and need to be + // inserted at the previously computed insert-spot. + if (!remaining.empty()) { + for (std::set::iterator PI = remaining.begin(), + PE = remaining.end(); PI != PE; ++PI) { + if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg)) + return true; + } + } + + // We're done editting, reconstruct the archive. + if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg)) + return true; + if (ReallyVerbose) + printSymbolTable(); + return false; +} + +// main - main program for llvm-ar .. see comments in the code +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + // Have the command line options parsed and handle things + // like --help and --version. + cl::ParseCommandLineOptions(argc, argv, + "LLVM Archiver (llvm-ar)\n\n" + " This program archives bitcode files into single libraries\n" + ); + + int exitCode = 0; + + // Make sure we don't exit with "unhandled exception". + try { + // Do our own parsing of the command line because the CommandLine utility + // can't handle the grouped positional parameters without a dash. + ArchiveOperation Operation = parseCommandLine(); + + // Check the path name of the archive + sys::Path ArchivePath; + if (!ArchivePath.set(ArchiveName)) + throw std::string("Archive name invalid: ") + ArchiveName; + + // Create or open the archive object. + bool Exists; + if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) { + // Produce a warning if we should and we're creating the archive + if (!Create) + errs() << argv[0] << ": creating " << ArchivePath.str() << "\n"; + TheArchive = Archive::CreateEmpty(ArchivePath, Context); + TheArchive->writeToDisk(); + } else { + std::string Error; + TheArchive = Archive::OpenAndLoad(ArchivePath, Context, &Error); + if (TheArchive == 0) { + errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': " + << Error << "!\n"; + return 1; + } + } + + // Make sure we're not fooling ourselves. + assert(TheArchive && "Unable to instantiate the archive"); + + // Make sure we clean up the archive even on failure. + std::auto_ptr AutoArchive(TheArchive); + + // Perform the operation + std::string ErrMsg; + bool haveError = false; + switch (Operation) { + case Print: haveError = doPrint(&ErrMsg); break; + case Delete: haveError = doDelete(&ErrMsg); break; + case Move: haveError = doMove(&ErrMsg); break; + case QuickAppend: haveError = doQuickAppend(&ErrMsg); break; + case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break; + case DisplayTable: haveError = doDisplayTable(&ErrMsg); break; + case Extract: haveError = doExtract(&ErrMsg); break; + case NoOperation: + errs() << argv[0] << ": No operation was selected.\n"; + break; + } + if (haveError) { + errs() << argv[0] << ": " << ErrMsg << "\n"; + return 1; + } + } catch (const char*msg) { + // These errors are usage errors, thrown only by the various checks in the + // code above. + errs() << argv[0] << ": " << msg << "\n\n"; + cl::PrintHelpMessage(); + exitCode = 1; + } catch (const std::string& msg) { + // These errors are thrown by LLVM libraries (e.g. lib System) and represent + // a more serious error so we bump the exitCode and don't print the usage. + errs() << argv[0] << ": " << msg << "\n"; + exitCode = 2; + } catch (...) { + // This really shouldn't happen, but just in case .... + errs() << argv[0] << ": An unexpected unknown exception occurred.\n"; + exitCode = 3; + } + + // Return result code back to operating system. + return exitCode; +} diff --git a/contrib/llvm/tools/llvm-as/CMakeLists.txt b/contrib/llvm/tools/llvm-as/CMakeLists.txt new file mode 100644 index 000000000..eef4a13e2 --- /dev/null +++ b/contrib/llvm/tools/llvm-as/CMakeLists.txt @@ -0,0 +1,6 @@ +set(LLVM_LINK_COMPONENTS asmparser bitwriter) +set(LLVM_REQUIRES_EH 1) + +add_llvm_tool(llvm-as + llvm-as.cpp + ) diff --git a/contrib/llvm/tools/llvm-as/Makefile b/contrib/llvm/tools/llvm-as/Makefile new file mode 100644 index 000000000..e1e5853a7 --- /dev/null +++ b/contrib/llvm/tools/llvm-as/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-as/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llvm-as +LINK_COMPONENTS := asmparser bitwriter + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-as/llvm-as.cpp b/contrib/llvm/tools/llvm-as/llvm-as.cpp new file mode 100644 index 000000000..c1661cdcb --- /dev/null +++ b/contrib/llvm/tools/llvm-as/llvm-as.cpp @@ -0,0 +1,119 @@ +//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility may be invoked in the following manner: +// llvm-as --help - Output information about command line switches +// llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout +// llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode +// to the x.bc file. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Assembly/Parser.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Signals.h" +#include +using namespace llvm; + +static cl::opt +InputFilename(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt +OutputFilename("o", cl::desc("Override output filename"), + cl::value_desc("filename")); + +static cl::opt +Force("f", cl::desc("Enable binary output on terminals")); + +static cl::opt +DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false)); + +static cl::opt +DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); + +static cl::opt +DisableVerify("disable-verify", cl::Hidden, + cl::desc("Do not run verifier on input LLVM (dangerous!)")); + +static void WriteOutputFile(const Module *M) { + // Infer the output filename if needed. + if (OutputFilename.empty()) { + if (InputFilename == "-") { + OutputFilename = "-"; + } else { + std::string IFN = InputFilename; + int Len = IFN.length(); + if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { + // Source ends in .ll + OutputFilename = std::string(IFN.begin(), IFN.end()-3); + } else { + OutputFilename = IFN; // Append a .bc to it + } + OutputFilename += ".bc"; + } + } + + std::string ErrorInfo; + OwningPtr Out + (new tool_output_file(OutputFilename.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary)); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + exit(1); + } + + if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) + WriteBitcodeToFile(M, Out->os()); + + // Declare success. + Out->keep(); +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); + + // Parse the file now... + SMDiagnostic Err; + std::auto_ptr M(ParseAssemblyFile(InputFilename, Err, Context)); + if (M.get() == 0) { + Err.Print(argv[0], errs()); + return 1; + } + + if (!DisableVerify) { + std::string Err; + if (verifyModule(*M.get(), ReturnStatusAction, &Err)) { + errs() << argv[0] + << ": assembly parsed, but does not verify as correct!\n"; + errs() << Err; + return 1; + } + } + + if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get(); + + if (!DisableOutput) + WriteOutputFile(M.get()); + + return 0; +} diff --git a/contrib/llvm/tools/llvm-bcanalyzer/CMakeLists.txt b/contrib/llvm/tools/llvm-bcanalyzer/CMakeLists.txt new file mode 100644 index 000000000..732bc3296 --- /dev/null +++ b/contrib/llvm/tools/llvm-bcanalyzer/CMakeLists.txt @@ -0,0 +1,6 @@ +set(LLVM_LINK_COMPONENTS bitreader) +set(LLVM_REQUIRES_EH 1) + +add_llvm_tool(llvm-bcanalyzer + llvm-bcanalyzer.cpp + ) diff --git a/contrib/llvm/tools/llvm-bcanalyzer/Makefile b/contrib/llvm/tools/llvm-bcanalyzer/Makefile new file mode 100644 index 000000000..488387d5d --- /dev/null +++ b/contrib/llvm/tools/llvm-bcanalyzer/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-bcanalyzer/Makefile ----------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-bcanalyzer +LINK_COMPONENTS := bitreader + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/contrib/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp new file mode 100644 index 000000000..4ada64a52 --- /dev/null +++ b/contrib/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -0,0 +1,629 @@ +//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This tool may be invoked in the following manner: +// llvm-bcanalyzer [options] - Read LLVM bitcode from stdin +// llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file +// +// Options: +// --help - Output information about command line switches +// --dump - Dump low-level bitcode structure in readable format +// +// This tool provides analytical information about a bitcode file. It is +// intended as an aid to developers of bitcode reading and writing software. It +// produces on std::out a summary of the bitcode file that shows various +// statistics about the contents of the file. By default this information is +// detailed and contains information about individual bitcode blocks and the +// functions in the module. +// The tool is also able to print a bitcode file in a straight forward text +// format that shows the containment and relationships of the information in +// the bitcode file (-dump option). +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Bitcode/BitstreamReader.h" +#include "llvm/Bitcode/LLVMBitCodes.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/system_error.h" +#include +#include +#include +using namespace llvm; + +static cl::opt + InputFilename(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt Dump("dump", cl::desc("Dump low level bitcode trace")); + +//===----------------------------------------------------------------------===// +// Bitcode specific analysis. +//===----------------------------------------------------------------------===// + +static cl::opt NoHistogram("disable-histogram", + cl::desc("Do not print per-code histogram")); + +static cl::opt +NonSymbolic("non-symbolic", + cl::desc("Emit numeric info in dump even if" + " symbolic info is available")); + +namespace { + +/// CurStreamTypeType - A type for CurStreamType +enum CurStreamTypeType { + UnknownBitstream, + LLVMIRBitstream +}; + +} + +/// CurStreamType - If we can sniff the flavor of this stream, we can produce +/// better dump info. +static CurStreamTypeType CurStreamType; + + +/// GetBlockName - Return a symbolic block name if known, otherwise return +/// null. +static const char *GetBlockName(unsigned BlockID, + const BitstreamReader &StreamFile) { + // Standard blocks for all bitcode files. + if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { + if (BlockID == bitc::BLOCKINFO_BLOCK_ID) + return "BLOCKINFO_BLOCK"; + return 0; + } + + // Check to see if we have a blockinfo record for this block, with a name. + if (const BitstreamReader::BlockInfo *Info = + StreamFile.getBlockInfo(BlockID)) { + if (!Info->Name.empty()) + return Info->Name.c_str(); + } + + + if (CurStreamType != LLVMIRBitstream) return 0; + + switch (BlockID) { + default: return 0; + case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK"; + case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK"; + case bitc::TYPE_BLOCK_ID_OLD: return "TYPE_BLOCK_ID_OLD"; + case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID"; + case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK"; + case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK"; + case bitc::TYPE_SYMTAB_BLOCK_ID_OLD: return "TYPE_SYMTAB_OLD"; + case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB"; + case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK"; + case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK"; + } +} + +/// GetCodeName - Return a symbolic code name if known, otherwise return +/// null. +static const char *GetCodeName(unsigned CodeID, unsigned BlockID, + const BitstreamReader &StreamFile) { + // Standard blocks for all bitcode files. + if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { + if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { + switch (CodeID) { + default: return 0; + case bitc::BLOCKINFO_CODE_SETBID: return "SETBID"; + case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME"; + case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME"; + } + } + return 0; + } + + // Check to see if we have a blockinfo record for this record, with a name. + if (const BitstreamReader::BlockInfo *Info = + StreamFile.getBlockInfo(BlockID)) { + for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i) + if (Info->RecordNames[i].first == CodeID) + return Info->RecordNames[i].second.c_str(); + } + + + if (CurStreamType != LLVMIRBitstream) return 0; + + switch (BlockID) { + default: return 0; + case bitc::MODULE_BLOCK_ID: + switch (CodeID) { + default: return 0; + case bitc::MODULE_CODE_VERSION: return "VERSION"; + case bitc::MODULE_CODE_TRIPLE: return "TRIPLE"; + case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT"; + case bitc::MODULE_CODE_ASM: return "ASM"; + case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME"; + case bitc::MODULE_CODE_DEPLIB: return "DEPLIB"; + case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR"; + case bitc::MODULE_CODE_FUNCTION: return "FUNCTION"; + case bitc::MODULE_CODE_ALIAS: return "ALIAS"; + case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS"; + case bitc::MODULE_CODE_GCNAME: return "GCNAME"; + } + case bitc::PARAMATTR_BLOCK_ID: + switch (CodeID) { + default: return 0; + case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY"; + } + case bitc::TYPE_BLOCK_ID_OLD: + case bitc::TYPE_BLOCK_ID_NEW: + switch (CodeID) { + default: return 0; + case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY"; + case bitc::TYPE_CODE_VOID: return "VOID"; + case bitc::TYPE_CODE_FLOAT: return "FLOAT"; + case bitc::TYPE_CODE_DOUBLE: return "DOUBLE"; + case bitc::TYPE_CODE_LABEL: return "LABEL"; + case bitc::TYPE_CODE_OPAQUE: return "OPAQUE"; + case bitc::TYPE_CODE_INTEGER: return "INTEGER"; + case bitc::TYPE_CODE_POINTER: return "POINTER"; + case bitc::TYPE_CODE_FUNCTION: return "FUNCTION"; + case bitc::TYPE_CODE_STRUCT_OLD: return "STRUCT_OLD"; + case bitc::TYPE_CODE_ARRAY: return "ARRAY"; + case bitc::TYPE_CODE_VECTOR: return "VECTOR"; + case bitc::TYPE_CODE_X86_FP80: return "X86_FP80"; + case bitc::TYPE_CODE_FP128: return "FP128"; + case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128"; + case bitc::TYPE_CODE_METADATA: return "METADATA"; + case bitc::TYPE_CODE_STRUCT_ANON: return "STRUCT_ANON"; + case bitc::TYPE_CODE_STRUCT_NAME: return "STRUCT_NAME"; + case bitc::TYPE_CODE_STRUCT_NAMED: return "STRUCT_NAMED"; + } + + case bitc::CONSTANTS_BLOCK_ID: + switch (CodeID) { + default: return 0; + case bitc::CST_CODE_SETTYPE: return "SETTYPE"; + case bitc::CST_CODE_NULL: return "NULL"; + case bitc::CST_CODE_UNDEF: return "UNDEF"; + case bitc::CST_CODE_INTEGER: return "INTEGER"; + case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER"; + case bitc::CST_CODE_FLOAT: return "FLOAT"; + case bitc::CST_CODE_AGGREGATE: return "AGGREGATE"; + case bitc::CST_CODE_STRING: return "STRING"; + case bitc::CST_CODE_CSTRING: return "CSTRING"; + case bitc::CST_CODE_CE_BINOP: return "CE_BINOP"; + case bitc::CST_CODE_CE_CAST: return "CE_CAST"; + case bitc::CST_CODE_CE_GEP: return "CE_GEP"; + case bitc::CST_CODE_CE_INBOUNDS_GEP: return "CE_INBOUNDS_GEP"; + case bitc::CST_CODE_CE_SELECT: return "CE_SELECT"; + case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT"; + case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT"; + case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC"; + case bitc::CST_CODE_CE_CMP: return "CE_CMP"; + case bitc::CST_CODE_INLINEASM: return "INLINEASM"; + case bitc::CST_CODE_CE_SHUFVEC_EX: return "CE_SHUFVEC_EX"; + } + case bitc::FUNCTION_BLOCK_ID: + switch (CodeID) { + default: return 0; + case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS"; + + case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP"; + case bitc::FUNC_CODE_INST_CAST: return "INST_CAST"; + case bitc::FUNC_CODE_INST_GEP: return "INST_GEP"; + case bitc::FUNC_CODE_INST_INBOUNDS_GEP: return "INST_INBOUNDS_GEP"; + case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT"; + case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT"; + case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT"; + case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC"; + case bitc::FUNC_CODE_INST_CMP: return "INST_CMP"; + + case bitc::FUNC_CODE_INST_RET: return "INST_RET"; + case bitc::FUNC_CODE_INST_BR: return "INST_BR"; + case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH"; + case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE"; + case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND"; + case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE"; + + case bitc::FUNC_CODE_INST_PHI: return "INST_PHI"; + case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA"; + case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD"; + case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG"; + case bitc::FUNC_CODE_INST_STORE: return "INST_STORE"; + case bitc::FUNC_CODE_INST_EXTRACTVAL: return "INST_EXTRACTVAL"; + case bitc::FUNC_CODE_INST_INSERTVAL: return "INST_INSERTVAL"; + case bitc::FUNC_CODE_INST_CMP2: return "INST_CMP2"; + case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT"; + case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: return "DEBUG_LOC_AGAIN"; + case bitc::FUNC_CODE_INST_CALL: return "INST_CALL"; + case bitc::FUNC_CODE_DEBUG_LOC: return "DEBUG_LOC"; + } + case bitc::TYPE_SYMTAB_BLOCK_ID_OLD: + switch (CodeID) { + default: return 0; + case bitc::TST_CODE_ENTRY: return "ENTRY"; + } + case bitc::VALUE_SYMTAB_BLOCK_ID: + switch (CodeID) { + default: return 0; + case bitc::VST_CODE_ENTRY: return "ENTRY"; + case bitc::VST_CODE_BBENTRY: return "BBENTRY"; + } + case bitc::METADATA_ATTACHMENT_ID: + switch(CodeID) { + default:return 0; + case bitc::METADATA_ATTACHMENT: return "METADATA_ATTACHMENT"; + } + case bitc::METADATA_BLOCK_ID: + switch(CodeID) { + default:return 0; + case bitc::METADATA_STRING: return "METADATA_STRING"; + case bitc::METADATA_NAME: return "METADATA_NAME"; + case bitc::METADATA_KIND: return "METADATA_KIND"; + case bitc::METADATA_NODE: return "METADATA_NODE"; + case bitc::METADATA_FN_NODE: return "METADATA_FN_NODE"; + case bitc::METADATA_NAMED_NODE: return "METADATA_NAMED_NODE"; + } + } +} + +struct PerRecordStats { + unsigned NumInstances; + unsigned NumAbbrev; + uint64_t TotalBits; + + PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {} +}; + +struct PerBlockIDStats { + /// NumInstances - This the number of times this block ID has been seen. + unsigned NumInstances; + + /// NumBits - The total size in bits of all of these blocks. + uint64_t NumBits; + + /// NumSubBlocks - The total number of blocks these blocks contain. + unsigned NumSubBlocks; + + /// NumAbbrevs - The total number of abbreviations. + unsigned NumAbbrevs; + + /// NumRecords - The total number of records these blocks contain, and the + /// number that are abbreviated. + unsigned NumRecords, NumAbbreviatedRecords; + + /// CodeFreq - Keep track of the number of times we see each code. + std::vector CodeFreq; + + PerBlockIDStats() + : NumInstances(0), NumBits(0), + NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {} +}; + +static std::map BlockIDStats; + + + +/// Error - All bitcode analysis errors go through this function, making this a +/// good place to breakpoint if debugging. +static bool Error(const std::string &Err) { + errs() << Err << "\n"; + return true; +} + +/// ParseBlock - Read a block, updating statistics, etc. +static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) { + std::string Indent(IndentLevel*2, ' '); + uint64_t BlockBitStart = Stream.GetCurrentBitNo(); + unsigned BlockID = Stream.ReadSubBlockID(); + + // Get the statistics for this BlockID. + PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; + + BlockStats.NumInstances++; + + // BLOCKINFO is a special part of the stream. + if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { + if (Dump) errs() << Indent << "\n"; + if (Stream.ReadBlockInfoBlock()) + return Error("Malformed BlockInfoBlock"); + uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); + BlockStats.NumBits += BlockBitEnd-BlockBitStart; + return false; + } + + unsigned NumWords = 0; + if (Stream.EnterSubBlock(BlockID, &NumWords)) + return Error("Malformed block record"); + + const char *BlockName = 0; + if (Dump) { + errs() << Indent << "<"; + if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader()))) + errs() << BlockName; + else + errs() << "UnknownBlock" << BlockID; + + if (NonSymbolic && BlockName) + errs() << " BlockID=" << BlockID; + + errs() << " NumWords=" << NumWords + << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n"; + } + + SmallVector Record; + + // Read all the records for this block. + while (1) { + if (Stream.AtEndOfStream()) + return Error("Premature end of bitstream"); + + uint64_t RecordStartBit = Stream.GetCurrentBitNo(); + + // Read the code for this record. + unsigned AbbrevID = Stream.ReadCode(); + switch (AbbrevID) { + case bitc::END_BLOCK: { + if (Stream.ReadBlockEnd()) + return Error("Error at end of block"); + uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); + BlockStats.NumBits += BlockBitEnd-BlockBitStart; + if (Dump) { + errs() << Indent << "\n"; + else + errs() << "UnknownBlock" << BlockID << ">\n"; + } + return false; + } + case bitc::ENTER_SUBBLOCK: { + uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); + if (ParseBlock(Stream, IndentLevel+1)) + return true; + ++BlockStats.NumSubBlocks; + uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); + + // Don't include subblock sizes in the size of this block. + BlockBitStart += SubBlockBitEnd-SubBlockBitStart; + break; + } + case bitc::DEFINE_ABBREV: + Stream.ReadAbbrevRecord(); + ++BlockStats.NumAbbrevs; + break; + default: + Record.clear(); + + ++BlockStats.NumRecords; + if (AbbrevID != bitc::UNABBREV_RECORD) + ++BlockStats.NumAbbreviatedRecords; + + const char *BlobStart = 0; + unsigned BlobLen = 0; + unsigned Code = Stream.ReadRecord(AbbrevID, Record, BlobStart, BlobLen); + + + + // Increment the # occurrences of this code. + if (BlockStats.CodeFreq.size() <= Code) + BlockStats.CodeFreq.resize(Code+1); + BlockStats.CodeFreq[Code].NumInstances++; + BlockStats.CodeFreq[Code].TotalBits += + Stream.GetCurrentBitNo()-RecordStartBit; + if (AbbrevID != bitc::UNABBREV_RECORD) + BlockStats.CodeFreq[Code].NumAbbrev++; + + if (Dump) { + errs() << Indent << " <"; + if (const char *CodeName = + GetCodeName(Code, BlockID, *Stream.getBitStreamReader())) + errs() << CodeName; + else + errs() << "UnknownCode" << Code; + if (NonSymbolic && + GetCodeName(Code, BlockID, *Stream.getBitStreamReader())) + errs() << " codeid=" << Code; + if (AbbrevID != bitc::UNABBREV_RECORD) + errs() << " abbrevid=" << AbbrevID; + + for (unsigned i = 0, e = Record.size(); i != e; ++i) + errs() << " op" << i << "=" << (int64_t)Record[i]; + + errs() << "/>"; + + if (BlobStart) { + errs() << " blob data = "; + bool BlobIsPrintable = true; + for (unsigned i = 0; i != BlobLen; ++i) + if (!isprint(BlobStart[i])) { + BlobIsPrintable = false; + break; + } + + if (BlobIsPrintable) + errs() << "'" << std::string(BlobStart, BlobStart+BlobLen) <<"'"; + else + errs() << "unprintable, " << BlobLen << " bytes."; + } + + errs() << "\n"; + } + + break; + } + } +} + +static void PrintSize(double Bits) { + fprintf(stderr, "%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32)); +} +static void PrintSize(uint64_t Bits) { + fprintf(stderr, "%lub/%.2fB/%luW", (unsigned long)Bits, + (double)Bits/8, (unsigned long)(Bits/32)); +} + + +/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. +static int AnalyzeBitcode() { + // Read the input file. + OwningPtr MemBuf; + + if (error_code ec = + MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf)) + return Error("Error reading '" + InputFilename + "': " + ec.message()); + + if (MemBuf->getBufferSize() & 3) + return Error("Bitcode stream should be a multiple of 4 bytes in length"); + + unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart(); + unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize(); + + // If we have a wrapper header, parse it and ignore the non-bc file contents. + // The magic number is 0x0B17C0DE stored in little endian. + if (isBitcodeWrapper(BufPtr, EndBufPtr)) + if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr)) + return Error("Invalid bitcode wrapper header"); + + BitstreamReader StreamFile(BufPtr, EndBufPtr); + BitstreamCursor Stream(StreamFile); + StreamFile.CollectBlockInfoNames(); + + // Read the stream signature. + char Signature[6]; + Signature[0] = Stream.Read(8); + Signature[1] = Stream.Read(8); + Signature[2] = Stream.Read(4); + Signature[3] = Stream.Read(4); + Signature[4] = Stream.Read(4); + Signature[5] = Stream.Read(4); + + // Autodetect the file contents, if it is one we know. + CurStreamType = UnknownBitstream; + if (Signature[0] == 'B' && Signature[1] == 'C' && + Signature[2] == 0x0 && Signature[3] == 0xC && + Signature[4] == 0xE && Signature[5] == 0xD) + CurStreamType = LLVMIRBitstream; + + unsigned NumTopBlocks = 0; + + // Parse the top-level structure. We only allow blocks at the top-level. + while (!Stream.AtEndOfStream()) { + unsigned Code = Stream.ReadCode(); + if (Code != bitc::ENTER_SUBBLOCK) + return Error("Invalid record at top-level"); + + if (ParseBlock(Stream, 0)) + return true; + ++NumTopBlocks; + } + + if (Dump) errs() << "\n\n"; + + uint64_t BufferSizeBits = (EndBufPtr-BufPtr)*CHAR_BIT; + // Print a summary of the read file. + errs() << "Summary of " << InputFilename << ":\n"; + errs() << " Total size: "; + PrintSize(BufferSizeBits); + errs() << "\n"; + errs() << " Stream type: "; + switch (CurStreamType) { + default: assert(0 && "Unknown bitstream type"); + case UnknownBitstream: errs() << "unknown\n"; break; + case LLVMIRBitstream: errs() << "LLVM IR\n"; break; + } + errs() << " # Toplevel Blocks: " << NumTopBlocks << "\n"; + errs() << "\n"; + + // Emit per-block stats. + errs() << "Per-block Summary:\n"; + for (std::map::iterator I = BlockIDStats.begin(), + E = BlockIDStats.end(); I != E; ++I) { + errs() << " Block ID #" << I->first; + if (const char *BlockName = GetBlockName(I->first, StreamFile)) + errs() << " (" << BlockName << ")"; + errs() << ":\n"; + + const PerBlockIDStats &Stats = I->second; + errs() << " Num Instances: " << Stats.NumInstances << "\n"; + errs() << " Total Size: "; + PrintSize(Stats.NumBits); + errs() << "\n"; + double pct = (Stats.NumBits * 100.0) / BufferSizeBits; + errs() << " Percent of file: " << format("%2.4f%%", pct) << "\n"; + if (Stats.NumInstances > 1) { + errs() << " Average Size: "; + PrintSize(Stats.NumBits/(double)Stats.NumInstances); + errs() << "\n"; + errs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/" + << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n"; + errs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/" + << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n"; + errs() << " Tot/Avg Records: " << Stats.NumRecords << "/" + << Stats.NumRecords/(double)Stats.NumInstances << "\n"; + } else { + errs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n"; + errs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n"; + errs() << " Num Records: " << Stats.NumRecords << "\n"; + } + if (Stats.NumRecords) { + double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords; + errs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n"; + } + errs() << "\n"; + + // Print a histogram of the codes we see. + if (!NoHistogram && !Stats.CodeFreq.empty()) { + std::vector > FreqPairs; // + for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i) + if (unsigned Freq = Stats.CodeFreq[i].NumInstances) + FreqPairs.push_back(std::make_pair(Freq, i)); + std::stable_sort(FreqPairs.begin(), FreqPairs.end()); + std::reverse(FreqPairs.begin(), FreqPairs.end()); + + errs() << "\tRecord Histogram:\n"; + fprintf(stderr, "\t\t Count # Bits %% Abv Record Kind\n"); + for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) { + const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second]; + + fprintf(stderr, "\t\t%7d %9lu ", RecStats.NumInstances, + (unsigned long)RecStats.TotalBits); + + if (RecStats.NumAbbrev) + fprintf(stderr, "%7.2f ", + (double)RecStats.NumAbbrev/RecStats.NumInstances*100); + else + fprintf(stderr, " "); + + if (const char *CodeName = + GetCodeName(FreqPairs[i].second, I->first, StreamFile)) + fprintf(stderr, "%s\n", CodeName); + else + fprintf(stderr, "UnknownCode%d\n", FreqPairs[i].second); + } + errs() << "\n"; + + } + } + return 0; +} + + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n"); + + return AnalyzeBitcode(); +} diff --git a/contrib/llvm/tools/llvm-diff/CMakeLists.txt b/contrib/llvm/tools/llvm-diff/CMakeLists.txt new file mode 100644 index 000000000..c59d69ea0 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/CMakeLists.txt @@ -0,0 +1,8 @@ +set(LLVM_LINK_COMPONENTS support asmparser bitreader) + +add_llvm_tool(llvm-diff + llvm-diff.cpp + DiffConsumer.cpp + DiffLog.cpp + DifferenceEngine.cpp + ) diff --git a/contrib/llvm/tools/llvm-diff/DiffConsumer.cpp b/contrib/llvm/tools/llvm-diff/DiffConsumer.cpp new file mode 100644 index 000000000..c23e8fb91 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/DiffConsumer.cpp @@ -0,0 +1,209 @@ +//===-- DiffConsumer.cpp - Difference Consumer ------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This files implements the the LLVM difference Consumer +// +//===----------------------------------------------------------------------===// + +#include "DiffConsumer.h" + +#include "llvm/Module.h" +#include "llvm/Instructions.h" +#include "llvm/Support/ErrorHandling.h" + +using namespace llvm; + +static void ComputeNumbering(Function *F, DenseMap &Numbering){ + unsigned IN = 0; + + // Arguments get the first numbers. + for (Function::arg_iterator + AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI) + if (!AI->hasName()) + Numbering[&*AI] = IN++; + + // Walk the basic blocks in order. + for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) { + if (!FI->hasName()) + Numbering[&*FI] = IN++; + + // Walk the instructions in order. + for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) + // void instructions don't get numbers. + if (!BI->hasName() && !BI->getType()->isVoidTy()) + Numbering[&*BI] = IN++; + } + + assert(!Numbering.empty() && "asked for numbering but numbering was no-op"); +} + + +void DiffConsumer::printValue(Value *V, bool isL) { + if (V->hasName()) { + out << (isa(V) ? '@' : '%') << V->getName(); + return; + } + if (V->getType()->isVoidTy()) { + if (isa(V)) { + out << "store to "; + printValue(cast(V)->getPointerOperand(), isL); + } else if (isa(V)) { + out << "call to "; + printValue(cast(V)->getCalledValue(), isL); + } else if (isa(V)) { + out << "invoke to "; + printValue(cast(V)->getCalledValue(), isL); + } else { + out << *V; + } + return; + } + + unsigned N = contexts.size(); + while (N > 0) { + --N; + DiffContext &ctxt = contexts[N]; + if (!ctxt.IsFunction) continue; + if (isL) { + if (ctxt.LNumbering.empty()) + ComputeNumbering(cast(ctxt.L), ctxt.LNumbering); + out << '%' << ctxt.LNumbering[V]; + return; + } else { + if (ctxt.RNumbering.empty()) + ComputeNumbering(cast(ctxt.R), ctxt.RNumbering); + out << '%' << ctxt.RNumbering[V]; + return; + } + } + + out << ""; +} + +void DiffConsumer::header() { + if (contexts.empty()) return; + for (SmallVectorImpl::iterator + I = contexts.begin(), E = contexts.end(); I != E; ++I) { + if (I->Differences) continue; + if (isa(I->L)) { + // Extra newline between functions. + if (Differences) out << "\n"; + + Function *L = cast(I->L); + Function *R = cast(I->R); + if (L->getName() != R->getName()) + out << "in function " << L->getName() + << " / " << R->getName() << ":\n"; + else + out << "in function " << L->getName() << ":\n"; + } else if (isa(I->L)) { + BasicBlock *L = cast(I->L); + BasicBlock *R = cast(I->R); + if (L->hasName() && R->hasName() && L->getName() == R->getName()) + out << " in block %" << L->getName() << ":\n"; + else { + out << " in block "; + printValue(L, true); + out << " / "; + printValue(R, false); + out << ":\n"; + } + } else if (isa(I->L)) { + out << " in instruction "; + printValue(I->L, true); + out << " / "; + printValue(I->R, false); + out << ":\n"; + } + + I->Differences = true; + } +} + +void DiffConsumer::indent() { + unsigned N = Indent; + while (N--) out << ' '; +} + +bool DiffConsumer::hadDifferences() const { + return Differences; +} + +void DiffConsumer::enterContext(Value *L, Value *R) { + contexts.push_back(DiffContext(L, R)); + Indent += 2; +} + +void DiffConsumer::exitContext() { + Differences |= contexts.back().Differences; + contexts.pop_back(); + Indent -= 2; +} + +void DiffConsumer::log(StringRef text) { + header(); + indent(); + out << text << '\n'; +} + +void DiffConsumer::logf(const LogBuilder &Log) { + header(); + indent(); + + unsigned arg = 0; + + StringRef format = Log.getFormat(); + while (true) { + size_t percent = format.find('%'); + if (percent == StringRef::npos) { + out << format; + break; + } + assert(format[percent] == '%'); + + if (percent > 0) out << format.substr(0, percent); + + switch (format[percent+1]) { + case '%': out << '%'; break; + case 'l': printValue(Log.getArgument(arg++), true); break; + case 'r': printValue(Log.getArgument(arg++), false); break; + default: llvm_unreachable("unknown format character"); + } + + format = format.substr(percent+2); + } + + out << '\n'; +} + +void DiffConsumer::logd(const DiffLogBuilder &Log) { + header(); + + for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) { + indent(); + switch (Log.getLineKind(I)) { + case DC_match: + out << " "; + Log.getLeft(I)->dump(); + //printValue(Log.getLeft(I), true); + break; + case DC_left: + out << "< "; + Log.getLeft(I)->dump(); + //printValue(Log.getLeft(I), true); + break; + case DC_right: + out << "> "; + Log.getRight(I)->dump(); + //printValue(Log.getRight(I), false); + break; + } + //out << "\n"; + } +} diff --git a/contrib/llvm/tools/llvm-diff/DiffConsumer.h b/contrib/llvm/tools/llvm-diff/DiffConsumer.h new file mode 100644 index 000000000..b95d42713 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/DiffConsumer.h @@ -0,0 +1,92 @@ +//===-- DiffConsumer.h - Difference Consumer --------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header defines the interface to the LLVM difference Consumer +// +//===----------------------------------------------------------------------===// + +#ifndef _LLVM_DIFFCONSUMER_H_ +#define _LLVM_DIFFCONSUMER_H_ + +#include "DiffLog.h" + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Casting.h" + +namespace llvm { + class Module; + class Value; + class Function; + + /// The interface for consumers of difference data. + class Consumer { + public: + /// Record that a local context has been entered. Left and + /// Right are IR "containers" of some sort which are being + /// considered for structural equivalence: global variables, + /// functions, blocks, instructions, etc. + virtual void enterContext(Value *Left, Value *Right) = 0; + + /// Record that a local context has been exited. + virtual void exitContext() = 0; + + /// Record a difference within the current context. + virtual void log(StringRef Text) = 0; + + /// Record a formatted difference within the current context. + virtual void logf(const LogBuilder &Log) = 0; + + /// Record a line-by-line instruction diff. + virtual void logd(const DiffLogBuilder &Log) = 0; + + protected: + virtual ~Consumer() {} + }; + + class DiffConsumer : public Consumer { + private: + struct DiffContext { + DiffContext(Value *L, Value *R) + : L(L), R(R), Differences(false), IsFunction(isa(L)) {} + Value *L; + Value *R; + bool Differences; + bool IsFunction; + DenseMap LNumbering; + DenseMap RNumbering; + }; + + raw_ostream &out; + Module *LModule; + Module *RModule; + SmallVector contexts; + bool Differences; + unsigned Indent; + + void printValue(Value *V, bool isL); + void header(); + void indent(); + + public: + DiffConsumer(Module *L, Module *R) + : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {} + + bool hadDifferences() const; + void enterContext(Value *L, Value *R); + void exitContext(); + void log(StringRef text); + void logf(const LogBuilder &Log); + void logd(const DiffLogBuilder &Log); + }; +} + +#endif diff --git a/contrib/llvm/tools/llvm-diff/DiffLog.cpp b/contrib/llvm/tools/llvm-diff/DiffLog.cpp new file mode 100644 index 000000000..9cc0c889f --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/DiffLog.cpp @@ -0,0 +1,53 @@ +//===-- DiffLog.h - Difference Log Builder and accessories ------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header defines the interface to the LLVM difference log builder. +// +//===----------------------------------------------------------------------===// + +#include "DiffLog.h" +#include "DiffConsumer.h" + +#include "llvm/Instructions.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" + +using namespace llvm; + +LogBuilder::~LogBuilder() { + consumer.logf(*this); +} + +StringRef LogBuilder::getFormat() const { return Format; } + +unsigned LogBuilder::getNumArguments() const { return Arguments.size(); } +Value *LogBuilder::getArgument(unsigned I) const { return Arguments[I]; } + +DiffLogBuilder::~DiffLogBuilder() { consumer.logd(*this); } + +void DiffLogBuilder::addMatch(Instruction *L, Instruction *R) { + Diff.push_back(DiffRecord(L, R)); +} +void DiffLogBuilder::addLeft(Instruction *L) { + // HACK: VS 2010 has a bug in the stdlib that requires this. + Diff.push_back(DiffRecord(L, DiffRecord::second_type(0))); +} +void DiffLogBuilder::addRight(Instruction *R) { + // HACK: VS 2010 has a bug in the stdlib that requires this. + Diff.push_back(DiffRecord(DiffRecord::first_type(0), R)); +} + +unsigned DiffLogBuilder::getNumLines() const { return Diff.size(); } + +DiffChange DiffLogBuilder::getLineKind(unsigned I) const { + return (Diff[I].first ? (Diff[I].second ? DC_match : DC_left) + : DC_right); +} +Instruction *DiffLogBuilder::getLeft(unsigned I) const { return Diff[I].first; } +Instruction *DiffLogBuilder::getRight(unsigned I) const { return Diff[I].second; } diff --git a/contrib/llvm/tools/llvm-diff/DiffLog.h b/contrib/llvm/tools/llvm-diff/DiffLog.h new file mode 100644 index 000000000..43e318ac4 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/DiffLog.h @@ -0,0 +1,80 @@ +//===-- DiffLog.h - Difference Log Builder and accessories ------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header defines the interface to the LLVM difference log builder. +// +//===----------------------------------------------------------------------===// + +#ifndef _LLVM_DIFFLOG_H_ +#define _LLVM_DIFFLOG_H_ + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" + +namespace llvm { + class Instruction; + class Value; + class Consumer; + + /// Trichotomy assumption + enum DiffChange { DC_match, DC_left, DC_right }; + + /// A temporary-object class for building up log messages. + class LogBuilder { + Consumer &consumer; + + /// The use of a stored StringRef here is okay because + /// LogBuilder should be used only as a temporary, and as a + /// temporary it will be destructed before whatever temporary + /// might be initializing this format. + StringRef Format; + + SmallVector Arguments; + + public: + LogBuilder(Consumer &c, StringRef Format) + : consumer(c), Format(Format) {} + + LogBuilder &operator<<(Value *V) { + Arguments.push_back(V); + return *this; + } + + ~LogBuilder(); + + StringRef getFormat() const; + unsigned getNumArguments() const; + Value *getArgument(unsigned I) const; + }; + + /// A temporary-object class for building up diff messages. + class DiffLogBuilder { + typedef std::pair DiffRecord; + SmallVector Diff; + + Consumer &consumer; + + public: + DiffLogBuilder(Consumer &c) : consumer(c) {} + ~DiffLogBuilder(); + + void addMatch(Instruction *L, Instruction *R); + // HACK: VS 2010 has a bug in the stdlib that requires this. + void addLeft(Instruction *L); + void addRight(Instruction *R); + + unsigned getNumLines() const; + DiffChange getLineKind(unsigned I) const; + Instruction *getLeft(unsigned I) const; + Instruction *getRight(unsigned I) const; + }; + +} + +#endif diff --git a/contrib/llvm/tools/llvm-diff/DifferenceEngine.cpp b/contrib/llvm/tools/llvm-diff/DifferenceEngine.cpp new file mode 100644 index 000000000..b240d8c5d --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/DifferenceEngine.cpp @@ -0,0 +1,677 @@ +//===-- DifferenceEngine.cpp - Structural function/module comparison ------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header defines the implementation of the LLVM difference +// engine, which structurally compares global values within a module. +// +//===----------------------------------------------------------------------===// + +#include "DifferenceEngine.h" + +#include "llvm/Constants.h" +#include "llvm/Function.h" +#include "llvm/Instructions.h" +#include "llvm/Module.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/Support/CallSite.h" +#include "llvm/Support/CFG.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/type_traits.h" + +#include + +using namespace llvm; + +namespace { + +/// A priority queue, implemented as a heap. +template +class PriorityQueue { + Sorter Precedes; + llvm::SmallVector Storage; + +public: + PriorityQueue(const Sorter &Precedes) : Precedes(Precedes) {} + + /// Checks whether the heap is empty. + bool empty() const { return Storage.empty(); } + + /// Insert a new value on the heap. + void insert(const T &V) { + unsigned Index = Storage.size(); + Storage.push_back(V); + if (Index == 0) return; + + T *data = Storage.data(); + while (true) { + unsigned Target = (Index + 1) / 2 - 1; + if (!Precedes(data[Index], data[Target])) return; + std::swap(data[Index], data[Target]); + if (Target == 0) return; + Index = Target; + } + } + + /// Remove the minimum value in the heap. Only valid on a non-empty heap. + T remove_min() { + assert(!empty()); + T tmp = Storage[0]; + + unsigned NewSize = Storage.size() - 1; + if (NewSize) { + // Move the slot at the end to the beginning. + if (isPodLike::value) + Storage[0] = Storage[NewSize]; + else + std::swap(Storage[0], Storage[NewSize]); + + // Bubble the root up as necessary. + unsigned Index = 0; + while (true) { + // With a 1-based index, the children would be Index*2 and Index*2+1. + unsigned R = (Index + 1) * 2; + unsigned L = R - 1; + + // If R is out of bounds, we're done after this in any case. + if (R >= NewSize) { + // If L is also out of bounds, we're done immediately. + if (L >= NewSize) break; + + // Otherwise, test whether we should swap L and Index. + if (Precedes(Storage[L], Storage[Index])) + std::swap(Storage[L], Storage[Index]); + break; + } + + // Otherwise, we need to compare with the smaller of L and R. + // Prefer R because it's closer to the end of the array. + unsigned IndexToTest = (Precedes(Storage[L], Storage[R]) ? L : R); + + // If Index is >= the min of L and R, then heap ordering is restored. + if (!Precedes(Storage[IndexToTest], Storage[Index])) + break; + + // Otherwise, keep bubbling up. + std::swap(Storage[IndexToTest], Storage[Index]); + Index = IndexToTest; + } + } + Storage.pop_back(); + + return tmp; + } +}; + +/// A function-scope difference engine. +class FunctionDifferenceEngine { + DifferenceEngine &Engine; + + /// The current mapping from old local values to new local values. + DenseMap Values; + + /// The current mapping from old blocks to new blocks. + DenseMap Blocks; + + DenseSet > TentativeValues; + + unsigned getUnprocPredCount(BasicBlock *Block) const { + unsigned Count = 0; + for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I) + if (!Blocks.count(*I)) Count++; + return Count; + } + + typedef std::pair BlockPair; + + /// A type which sorts a priority queue by the number of unprocessed + /// predecessor blocks it has remaining. + /// + /// This is actually really expensive to calculate. + struct QueueSorter { + const FunctionDifferenceEngine &fde; + explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {} + + bool operator()(const BlockPair &Old, const BlockPair &New) { + return fde.getUnprocPredCount(Old.first) + < fde.getUnprocPredCount(New.first); + } + }; + + /// A queue of unified blocks to process. + PriorityQueue Queue; + + /// Try to unify the given two blocks. Enqueues them for processing + /// if they haven't already been processed. + /// + /// Returns true if there was a problem unifying them. + bool tryUnify(BasicBlock *L, BasicBlock *R) { + BasicBlock *&Ref = Blocks[L]; + + if (Ref) { + if (Ref == R) return false; + + Engine.logf("successor %l cannot be equivalent to %r; " + "it's already equivalent to %r") + << L << R << Ref; + return true; + } + + Ref = R; + Queue.insert(BlockPair(L, R)); + return false; + } + + /// Unifies two instructions, given that they're known not to have + /// structural differences. + void unify(Instruction *L, Instruction *R) { + DifferenceEngine::Context C(Engine, L, R); + + bool Result = diff(L, R, true, true); + assert(!Result && "structural differences second time around?"); + (void) Result; + if (!L->use_empty()) + Values[L] = R; + } + + void processQueue() { + while (!Queue.empty()) { + BlockPair Pair = Queue.remove_min(); + diff(Pair.first, Pair.second); + } + } + + void diff(BasicBlock *L, BasicBlock *R) { + DifferenceEngine::Context C(Engine, L, R); + + BasicBlock::iterator LI = L->begin(), LE = L->end(); + BasicBlock::iterator RI = R->begin(); + + llvm::SmallVector, 20> TentativePairs; + + do { + assert(LI != LE && RI != R->end()); + Instruction *LeftI = &*LI, *RightI = &*RI; + + // If the instructions differ, start the more sophisticated diff + // algorithm at the start of the block. + if (diff(LeftI, RightI, false, false)) { + TentativeValues.clear(); + return runBlockDiff(L->begin(), R->begin()); + } + + // Otherwise, tentatively unify them. + if (!LeftI->use_empty()) + TentativeValues.insert(std::make_pair(LeftI, RightI)); + + ++LI, ++RI; + } while (LI != LE); // This is sufficient: we can't get equality of + // terminators if there are residual instructions. + + // Unify everything in the block, non-tentatively this time. + TentativeValues.clear(); + for (LI = L->begin(), RI = R->begin(); LI != LE; ++LI, ++RI) + unify(&*LI, &*RI); + } + + bool matchForBlockDiff(Instruction *L, Instruction *R); + void runBlockDiff(BasicBlock::iterator LI, BasicBlock::iterator RI); + + bool diffCallSites(CallSite L, CallSite R, bool Complain) { + // FIXME: call attributes + if (!equivalentAsOperands(L.getCalledValue(), R.getCalledValue())) { + if (Complain) Engine.log("called functions differ"); + return true; + } + if (L.arg_size() != R.arg_size()) { + if (Complain) Engine.log("argument counts differ"); + return true; + } + for (unsigned I = 0, E = L.arg_size(); I != E; ++I) + if (!equivalentAsOperands(L.getArgument(I), R.getArgument(I))) { + if (Complain) + Engine.logf("arguments %l and %r differ") + << L.getArgument(I) << R.getArgument(I); + return true; + } + return false; + } + + bool diff(Instruction *L, Instruction *R, bool Complain, bool TryUnify) { + // FIXME: metadata (if Complain is set) + + // Different opcodes always imply different operations. + if (L->getOpcode() != R->getOpcode()) { + if (Complain) Engine.log("different instruction types"); + return true; + } + + if (isa(L)) { + if (cast(L)->getPredicate() + != cast(R)->getPredicate()) { + if (Complain) Engine.log("different predicates"); + return true; + } + } else if (isa(L)) { + return diffCallSites(CallSite(L), CallSite(R), Complain); + } else if (isa(L)) { + // FIXME: implement. + + // This is really weird; type uniquing is broken? + if (L->getType() != R->getType()) { + if (!L->getType()->isPointerTy() || !R->getType()->isPointerTy()) { + if (Complain) Engine.log("different phi types"); + return true; + } + } + return false; + + // Terminators. + } else if (isa(L)) { + InvokeInst *LI = cast(L); + InvokeInst *RI = cast(R); + if (diffCallSites(CallSite(LI), CallSite(RI), Complain)) + return true; + + if (TryUnify) { + tryUnify(LI->getNormalDest(), RI->getNormalDest()); + tryUnify(LI->getUnwindDest(), RI->getUnwindDest()); + } + return false; + + } else if (isa(L)) { + BranchInst *LI = cast(L); + BranchInst *RI = cast(R); + if (LI->isConditional() != RI->isConditional()) { + if (Complain) Engine.log("branch conditionality differs"); + return true; + } + + if (LI->isConditional()) { + if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { + if (Complain) Engine.log("branch conditions differ"); + return true; + } + if (TryUnify) tryUnify(LI->getSuccessor(1), RI->getSuccessor(1)); + } + if (TryUnify) tryUnify(LI->getSuccessor(0), RI->getSuccessor(0)); + return false; + + } else if (isa(L)) { + SwitchInst *LI = cast(L); + SwitchInst *RI = cast(R); + if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { + if (Complain) Engine.log("switch conditions differ"); + return true; + } + if (TryUnify) tryUnify(LI->getDefaultDest(), RI->getDefaultDest()); + + bool Difference = false; + + DenseMap LCases; + for (unsigned I = 1, E = LI->getNumCases(); I != E; ++I) + LCases[LI->getCaseValue(I)] = LI->getSuccessor(I); + for (unsigned I = 1, E = RI->getNumCases(); I != E; ++I) { + ConstantInt *CaseValue = RI->getCaseValue(I); + BasicBlock *LCase = LCases[CaseValue]; + if (LCase) { + if (TryUnify) tryUnify(LCase, RI->getSuccessor(I)); + LCases.erase(CaseValue); + } else if (!Difference) { + if (Complain) + Engine.logf("right switch has extra case %r") << CaseValue; + Difference = true; + } + } + if (!Difference) + for (DenseMap::iterator + I = LCases.begin(), E = LCases.end(); I != E; ++I) { + if (Complain) + Engine.logf("left switch has extra case %l") << I->first; + Difference = true; + } + return Difference; + } else if (isa(L)) { + return false; + } + + if (L->getNumOperands() != R->getNumOperands()) { + if (Complain) Engine.log("instructions have different operand counts"); + return true; + } + + for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) { + Value *LO = L->getOperand(I), *RO = R->getOperand(I); + if (!equivalentAsOperands(LO, RO)) { + if (Complain) Engine.logf("operands %l and %r differ") << LO << RO; + return true; + } + } + + return false; + } + + bool equivalentAsOperands(Constant *L, Constant *R) { + // Use equality as a preliminary filter. + if (L == R) + return true; + + if (L->getValueID() != R->getValueID()) + return false; + + // Ask the engine about global values. + if (isa(L)) + return Engine.equivalentAsOperands(cast(L), + cast(R)); + + // Compare constant expressions structurally. + if (isa(L)) + return equivalentAsOperands(cast(L), + cast(R)); + + // Nulls of the "same type" don't always actually have the same + // type; I don't know why. Just white-list them. + if (isa(L)) + return true; + + // Block addresses only match if we've already encountered the + // block. FIXME: tentative matches? + if (isa(L)) + return Blocks[cast(L)->getBasicBlock()] + == cast(R)->getBasicBlock(); + + return false; + } + + bool equivalentAsOperands(ConstantExpr *L, ConstantExpr *R) { + if (L == R) + return true; + if (L->getOpcode() != R->getOpcode()) + return false; + + switch (L->getOpcode()) { + case Instruction::ICmp: + case Instruction::FCmp: + if (L->getPredicate() != R->getPredicate()) + return false; + break; + + case Instruction::GetElementPtr: + // FIXME: inbounds? + break; + + default: + break; + } + + if (L->getNumOperands() != R->getNumOperands()) + return false; + + for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) + if (!equivalentAsOperands(L->getOperand(I), R->getOperand(I))) + return false; + + return true; + } + + bool equivalentAsOperands(Value *L, Value *R) { + // Fall out if the values have different kind. + // This possibly shouldn't take priority over oracles. + if (L->getValueID() != R->getValueID()) + return false; + + // Value subtypes: Argument, Constant, Instruction, BasicBlock, + // InlineAsm, MDNode, MDString, PseudoSourceValue + + if (isa(L)) + return equivalentAsOperands(cast(L), cast(R)); + + if (isa(L)) + return Values[L] == R || TentativeValues.count(std::make_pair(L, R)); + + if (isa(L)) + return Values[L] == R; + + if (isa(L)) + return Blocks[cast(L)] != R; + + // Pretend everything else is identical. + return true; + } + + // Avoid a gcc warning about accessing 'this' in an initializer. + FunctionDifferenceEngine *this_() { return this; } + +public: + FunctionDifferenceEngine(DifferenceEngine &Engine) : + Engine(Engine), Queue(QueueSorter(*this_())) {} + + void diff(Function *L, Function *R) { + if (L->arg_size() != R->arg_size()) + Engine.log("different argument counts"); + + // Map the arguments. + for (Function::arg_iterator + LI = L->arg_begin(), LE = L->arg_end(), + RI = R->arg_begin(), RE = R->arg_end(); + LI != LE && RI != RE; ++LI, ++RI) + Values[&*LI] = &*RI; + + tryUnify(&*L->begin(), &*R->begin()); + processQueue(); + } +}; + +struct DiffEntry { + DiffEntry() : Cost(0) {} + + unsigned Cost; + llvm::SmallVector Path; // actually of DifferenceEngine::DiffChange +}; + +bool FunctionDifferenceEngine::matchForBlockDiff(Instruction *L, + Instruction *R) { + return !diff(L, R, false, false); +} + +void FunctionDifferenceEngine::runBlockDiff(BasicBlock::iterator LStart, + BasicBlock::iterator RStart) { + BasicBlock::iterator LE = LStart->getParent()->end(); + BasicBlock::iterator RE = RStart->getParent()->end(); + + unsigned NL = std::distance(LStart, LE); + + SmallVector Paths1(NL+1); + SmallVector Paths2(NL+1); + + DiffEntry *Cur = Paths1.data(); + DiffEntry *Next = Paths2.data(); + + const unsigned LeftCost = 2; + const unsigned RightCost = 2; + const unsigned MatchCost = 0; + + assert(TentativeValues.empty()); + + // Initialize the first column. + for (unsigned I = 0; I != NL+1; ++I) { + Cur[I].Cost = I * LeftCost; + for (unsigned J = 0; J != I; ++J) + Cur[I].Path.push_back(DC_left); + } + + for (BasicBlock::iterator RI = RStart; RI != RE; ++RI) { + // Initialize the first row. + Next[0] = Cur[0]; + Next[0].Cost += RightCost; + Next[0].Path.push_back(DC_right); + + unsigned Index = 1; + for (BasicBlock::iterator LI = LStart; LI != LE; ++LI, ++Index) { + if (matchForBlockDiff(&*LI, &*RI)) { + Next[Index] = Cur[Index-1]; + Next[Index].Cost += MatchCost; + Next[Index].Path.push_back(DC_match); + TentativeValues.insert(std::make_pair(&*LI, &*RI)); + } else if (Next[Index-1].Cost <= Cur[Index].Cost) { + Next[Index] = Next[Index-1]; + Next[Index].Cost += LeftCost; + Next[Index].Path.push_back(DC_left); + } else { + Next[Index] = Cur[Index]; + Next[Index].Cost += RightCost; + Next[Index].Path.push_back(DC_right); + } + } + + std::swap(Cur, Next); + } + + // We don't need the tentative values anymore; everything from here + // on out should be non-tentative. + TentativeValues.clear(); + + SmallVectorImpl &Path = Cur[NL].Path; + BasicBlock::iterator LI = LStart, RI = RStart; + + DiffLogBuilder Diff(Engine.getConsumer()); + + // Drop trailing matches. + while (Path.back() == DC_match) + Path.pop_back(); + + // Skip leading matches. + SmallVectorImpl::iterator + PI = Path.begin(), PE = Path.end(); + while (PI != PE && *PI == DC_match) { + unify(&*LI, &*RI); + ++PI, ++LI, ++RI; + } + + for (; PI != PE; ++PI) { + switch (static_cast(*PI)) { + case DC_match: + assert(LI != LE && RI != RE); + { + Instruction *L = &*LI, *R = &*RI; + unify(L, R); + Diff.addMatch(L, R); + } + ++LI; ++RI; + break; + + case DC_left: + assert(LI != LE); + Diff.addLeft(&*LI); + ++LI; + break; + + case DC_right: + assert(RI != RE); + Diff.addRight(&*RI); + ++RI; + break; + } + } + + // Finishing unifying and complaining about the tails of the block, + // which should be matches all the way through. + while (LI != LE) { + assert(RI != RE); + unify(&*LI, &*RI); + ++LI, ++RI; + } + + // If the terminators have different kinds, but one is an invoke and the + // other is an unconditional branch immediately following a call, unify + // the results and the destinations. + TerminatorInst *LTerm = LStart->getParent()->getTerminator(); + TerminatorInst *RTerm = RStart->getParent()->getTerminator(); + if (isa(LTerm) && isa(RTerm)) { + if (cast(LTerm)->isConditional()) return; + BasicBlock::iterator I = LTerm; + if (I == LStart->getParent()->begin()) return; + --I; + if (!isa(*I)) return; + CallInst *LCall = cast(&*I); + InvokeInst *RInvoke = cast(RTerm); + if (!equivalentAsOperands(LCall->getCalledValue(), RInvoke->getCalledValue())) + return; + if (!LCall->use_empty()) + Values[LCall] = RInvoke; + tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest()); + } else if (isa(LTerm) && isa(RTerm)) { + if (cast(RTerm)->isConditional()) return; + BasicBlock::iterator I = RTerm; + if (I == RStart->getParent()->begin()) return; + --I; + if (!isa(*I)) return; + CallInst *RCall = cast(I); + InvokeInst *LInvoke = cast(LTerm); + if (!equivalentAsOperands(LInvoke->getCalledValue(), RCall->getCalledValue())) + return; + if (!LInvoke->use_empty()) + Values[LInvoke] = RCall; + tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0)); + } +} + +} + +void DifferenceEngine::diff(Function *L, Function *R) { + Context C(*this, L, R); + + // FIXME: types + // FIXME: attributes and CC + // FIXME: parameter attributes + + // If both are declarations, we're done. + if (L->empty() && R->empty()) + return; + else if (L->empty()) + log("left function is declaration, right function is definition"); + else if (R->empty()) + log("right function is declaration, left function is definition"); + else + FunctionDifferenceEngine(*this).diff(L, R); +} + +void DifferenceEngine::diff(Module *L, Module *R) { + StringSet<> LNames; + SmallVector, 20> Queue; + + for (Module::iterator I = L->begin(), E = L->end(); I != E; ++I) { + Function *LFn = &*I; + LNames.insert(LFn->getName()); + + if (Function *RFn = R->getFunction(LFn->getName())) + Queue.push_back(std::make_pair(LFn, RFn)); + else + logf("function %l exists only in left module") << LFn; + } + + for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) { + Function *RFn = &*I; + if (!LNames.count(RFn->getName())) + logf("function %r exists only in right module") << RFn; + } + + for (SmallVectorImpl >::iterator + I = Queue.begin(), E = Queue.end(); I != E; ++I) + diff(I->first, I->second); +} + +bool DifferenceEngine::equivalentAsOperands(GlobalValue *L, GlobalValue *R) { + if (globalValueOracle) return (*globalValueOracle)(L, R); + return L->getName() == R->getName(); +} diff --git a/contrib/llvm/tools/llvm-diff/DifferenceEngine.h b/contrib/llvm/tools/llvm-diff/DifferenceEngine.h new file mode 100644 index 000000000..5b4f80b99 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/DifferenceEngine.h @@ -0,0 +1,91 @@ +//===-- DifferenceEngine.h - Module comparator ------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header defines the interface to the LLVM difference engine, +// which structurally compares functions within a module. +// +//===----------------------------------------------------------------------===// + +#ifndef _LLVM_DIFFERENCE_ENGINE_H_ +#define _LLVM_DIFFERENCE_ENGINE_H_ + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "DiffLog.h" +#include "DiffConsumer.h" + +#include + +namespace llvm { + class Function; + class GlobalValue; + class Instruction; + class LLVMContext; + class Module; + class Twine; + class Value; + + /// A class for performing structural comparisons of LLVM assembly. + class DifferenceEngine { + public: + /// A RAII object for recording the current context. + struct Context { + Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) { + Engine.consumer.enterContext(L, R); + } + + ~Context() { + Engine.consumer.exitContext(); + } + + private: + DifferenceEngine &Engine; + }; + + /// An oracle for answering whether two values are equivalent as + /// operands. + struct Oracle { + virtual bool operator()(Value *L, Value *R) = 0; + + protected: + virtual ~Oracle() {} + }; + + DifferenceEngine(LLVMContext &context, Consumer &consumer) + : context(context), consumer(consumer), globalValueOracle(0) {} + + void diff(Module *L, Module *R); + void diff(Function *L, Function *R); + void log(StringRef text) { + consumer.log(text); + } + LogBuilder logf(StringRef text) { + return LogBuilder(consumer, text); + } + Consumer& getConsumer() const { return consumer; } + + /// Installs an oracle to decide whether two global values are + /// equivalent as operands. Without an oracle, global values are + /// considered equivalent as operands precisely when they have the + /// same name. + void setGlobalValueOracle(Oracle *oracle) { + globalValueOracle = oracle; + } + + /// Determines whether two global values are equivalent. + bool equivalentAsOperands(GlobalValue *L, GlobalValue *R); + + private: + LLVMContext &context; + Consumer &consumer; + Oracle *globalValueOracle; + }; +} + +#endif diff --git a/contrib/llvm/tools/llvm-diff/Makefile b/contrib/llvm/tools/llvm-diff/Makefile new file mode 100644 index 000000000..58e49fa95 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-diff/Makefile ----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llvm-diff +LINK_COMPONENTS := asmparser bitreader + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-diff/llvm-diff.cpp b/contrib/llvm/tools/llvm-diff/llvm-diff.cpp new file mode 100644 index 000000000..76853f1e4 --- /dev/null +++ b/contrib/llvm/tools/llvm-diff/llvm-diff.cpp @@ -0,0 +1,98 @@ +//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the command-line driver for the difference engine. +// +//===----------------------------------------------------------------------===// + +#include "DiffLog.h" +#include "DifferenceEngine.h" + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Type.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/SourceMgr.h" + +#include +#include + + +using namespace llvm; + +/// Reads a module from a file. On error, messages are written to stderr +/// and null is returned. +static Module *ReadModule(LLVMContext &Context, StringRef Name) { + SMDiagnostic Diag; + Module *M = ParseIRFile(Name, Diag, Context); + if (!M) + Diag.Print("llvmdiff", errs()); + return M; +} + +static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R, + StringRef Name) { + // Drop leading sigils from the global name. + if (Name.startswith("@")) Name = Name.substr(1); + + Function *LFn = L->getFunction(Name); + Function *RFn = R->getFunction(Name); + if (LFn && RFn) + Engine.diff(LFn, RFn); + else if (!LFn && !RFn) + errs() << "No function named @" << Name << " in either module\n"; + else if (!LFn) + errs() << "No function named @" << Name << " in left module\n"; + else + errs() << "No function named @" << Name << " in right module\n"; +} + +static cl::opt LeftFilename(cl::Positional, + cl::desc(""), + cl::Required); +static cl::opt RightFilename(cl::Positional, + cl::desc(""), + cl::Required); +static cl::list GlobalsToCompare(cl::Positional, + cl::desc("")); + +int main(int argc, char **argv) { + cl::ParseCommandLineOptions(argc, argv); + + LLVMContext Context; + + // Load both modules. Die if that fails. + Module *LModule = ReadModule(Context, LeftFilename); + Module *RModule = ReadModule(Context, RightFilename); + if (!LModule || !RModule) return 1; + + DiffConsumer Consumer(LModule, RModule); + DifferenceEngine Engine(Context, Consumer); + + // If any global names were given, just diff those. + if (!GlobalsToCompare.empty()) { + for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I) + diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]); + + // Otherwise, diff everything in the module. + } else { + Engine.diff(LModule, RModule); + } + + delete LModule; + delete RModule; + + return Consumer.hadDifferences(); +} diff --git a/contrib/llvm/tools/llvm-dis/CMakeLists.txt b/contrib/llvm/tools/llvm-dis/CMakeLists.txt new file mode 100644 index 000000000..3125f8a5c --- /dev/null +++ b/contrib/llvm/tools/llvm-dis/CMakeLists.txt @@ -0,0 +1,6 @@ +set(LLVM_LINK_COMPONENTS bitreader analysis) +set(LLVM_REQUIRES_EH 1) + +add_llvm_tool(llvm-dis + llvm-dis.cpp + ) diff --git a/contrib/llvm/tools/llvm-dis/Makefile b/contrib/llvm/tools/llvm-dis/Makefile new file mode 100644 index 000000000..be7110008 --- /dev/null +++ b/contrib/llvm/tools/llvm-dis/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-dis/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-dis +LINK_COMPONENTS := bitreader analysis + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-dis/llvm-dis.cpp b/contrib/llvm/tools/llvm-dis/llvm-dis.cpp new file mode 100644 index 000000000..9020a5278 --- /dev/null +++ b/contrib/llvm/tools/llvm-dis/llvm-dis.cpp @@ -0,0 +1,186 @@ +//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility may be invoked in the following manner: +// llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout +// llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm +// to the x.ll file. +// Options: +// --help - Output information about command line switches +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Type.h" +#include "llvm/IntrinsicInst.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Analysis/DebugInfo.h" +#include "llvm/Assembly/AssemblyAnnotationWriter.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/system_error.h" +using namespace llvm; + +static cl::opt +InputFilename(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt +OutputFilename("o", cl::desc("Override output filename"), + cl::value_desc("filename")); + +static cl::opt +Force("f", cl::desc("Enable binary output on terminals")); + +static cl::opt +DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); + +static cl::opt +ShowAnnotations("show-annotations", + cl::desc("Add informational comments to the .ll file")); + +namespace { + +static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) { + OS << DL.getLine() << ":" << DL.getCol(); + if (MDNode *N = DL.getInlinedAt(getGlobalContext())) { + DebugLoc IDL = DebugLoc::getFromDILocation(N); + if (!IDL.isUnknown()) { + OS << "@"; + printDebugLoc(IDL,OS); + } + } +} +class CommentWriter : public AssemblyAnnotationWriter { +public: + void emitFunctionAnnot(const Function *F, + formatted_raw_ostream &OS) { + OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses + OS << '\n'; + } + void printInfoComment(const Value &V, formatted_raw_ostream &OS) { + bool Padded = false; + if (!V.getType()->isVoidTy()) { + OS.PadToColumn(50); + Padded = true; + OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type + } + if (const Instruction *I = dyn_cast(&V)) { + const DebugLoc &DL = I->getDebugLoc(); + if (!DL.isUnknown()) { + if (!Padded) { + OS.PadToColumn(50); + Padded = true; + OS << ";"; + } + OS << " [debug line = "; + printDebugLoc(DL,OS); + OS << "]"; + } + if (const DbgDeclareInst *DDI = dyn_cast(I)) { + DIVariable Var(DDI->getVariable()); + if (!Padded) { + OS.PadToColumn(50); + Padded = true; + OS << ";"; + } + OS << " [debug variable = " << Var.getName() << "]"; + } + else if (const DbgValueInst *DVI = dyn_cast(I)) { + DIVariable Var(DVI->getVariable()); + if (!Padded) { + OS.PadToColumn(50); + Padded = true; + OS << ";"; + } + OS << " [debug variable = " << Var.getName() << "]"; + } + } + } +}; + +} // end anon namespace + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + + cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); + + std::string ErrorMessage; + std::auto_ptr M; + + { + OwningPtr BufferPtr; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) + ErrorMessage = ec.message(); + else + M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage)); + } + + if (M.get() == 0) { + errs() << argv[0] << ": "; + if (ErrorMessage.size()) + errs() << ErrorMessage << "\n"; + else + errs() << "bitcode didn't read correctly.\n"; + return 1; + } + + // Just use stdout. We won't actually print anything on it. + if (DontPrint) + OutputFilename = "-"; + + if (OutputFilename.empty()) { // Unspecified output, infer it. + if (InputFilename == "-") { + OutputFilename = "-"; + } else { + const std::string &IFN = InputFilename; + int Len = IFN.length(); + // If the source ends in .bc, strip it off. + if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') + OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll"; + else + OutputFilename = IFN+".ll"; + } + } + + std::string ErrorInfo; + OwningPtr + Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary)); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + return 1; + } + + OwningPtr Annotator; + if (ShowAnnotations) + Annotator.reset(new CommentWriter()); + + // All that llvm-dis does is write the assembly to a file. + if (!DontPrint) + M->print(Out->os(), Annotator.get()); + + // Declare success. + Out->keep(); + + return 0; +} + diff --git a/contrib/llvm/tools/llvm-extract/CMakeLists.txt b/contrib/llvm/tools/llvm-extract/CMakeLists.txt new file mode 100644 index 000000000..a4e3266e3 --- /dev/null +++ b/contrib/llvm/tools/llvm-extract/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS asmparser ipo bitreader bitwriter) + +add_llvm_tool(llvm-extract + llvm-extract.cpp + ) diff --git a/contrib/llvm/tools/llvm-extract/Makefile b/contrib/llvm/tools/llvm-extract/Makefile new file mode 100644 index 000000000..5672aa329 --- /dev/null +++ b/contrib/llvm/tools/llvm-extract/Makefile @@ -0,0 +1,18 @@ +##===- tools/llvm-extract/Makefile -------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. + +TOOLNAME = llvm-extract +LINK_COMPONENTS := ipo bitreader bitwriter asmparser + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-extract/llvm-extract.cpp b/contrib/llvm/tools/llvm-extract/llvm-extract.cpp new file mode 100644 index 000000000..f6227ee25 --- /dev/null +++ b/contrib/llvm/tools/llvm-extract/llvm-extract.cpp @@ -0,0 +1,238 @@ +//===- llvm-extract.cpp - LLVM function extraction utility ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility changes the input module to only contain a single function, +// which is primarily used for debugging transformations. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Assembly/PrintModulePass.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/Regex.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SetVector.h" +#include +using namespace llvm; + +// InputFilename - The filename to read from. +static cl::opt +InputFilename(cl::Positional, cl::desc(""), + cl::init("-"), cl::value_desc("filename")); + +static cl::opt +OutputFilename("o", cl::desc("Specify output filename"), + cl::value_desc("filename"), cl::init("-")); + +static cl::opt +Force("f", cl::desc("Enable binary output on terminals")); + +static cl::opt +DeleteFn("delete", cl::desc("Delete specified Globals from Module")); + +// ExtractFuncs - The functions to extract from the module. +static cl::list +ExtractFuncs("func", cl::desc("Specify function to extract"), + cl::ZeroOrMore, cl::value_desc("function")); + +// ExtractRegExpFuncs - The functions, matched via regular expression, to +// extract from the module. +static cl::list +ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a " + "regular expression"), + cl::ZeroOrMore, cl::value_desc("rfunction")); + +// ExtractGlobals - The globals to extract from the module. +static cl::list +ExtractGlobals("glob", cl::desc("Specify global to extract"), + cl::ZeroOrMore, cl::value_desc("global")); + +// ExtractRegExpGlobals - The globals, matched via regular expression, to +// extract from the module... +static cl::list +ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a " + "regular expression"), + cl::ZeroOrMore, cl::value_desc("rglobal")); + +static cl::opt +OutputAssembly("S", + cl::desc("Write output as LLVM assembly"), cl::Hidden); + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n"); + + // Use lazy loading, since we only care about selected global values. + SMDiagnostic Err; + std::auto_ptr M; + M.reset(getLazyIRFileModule(InputFilename, Err, Context)); + + if (M.get() == 0) { + Err.Print(argv[0], errs()); + return 1; + } + + // Use SetVector to avoid duplicates. + SetVector GVs; + + // Figure out which globals we should extract. + for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { + GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]); + if (!GV) { + errs() << argv[0] << ": program doesn't contain global named '" + << ExtractGlobals[i] << "'!\n"; + return 1; + } + GVs.insert(GV); + } + + // Extract globals via regular expression matching. + for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) { + std::string Error; + Regex RegEx(ExtractRegExpGlobals[i]); + if (!RegEx.isValid(Error)) { + errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' " + "invalid regex: " << Error; + } + bool match = false; + for (Module::global_iterator GV = M.get()->global_begin(), + E = M.get()->global_end(); GV != E; GV++) { + if (RegEx.match(GV->getName())) { + GVs.insert(&*GV); + match = true; + } + } + if (!match) { + errs() << argv[0] << ": program doesn't contain global named '" + << ExtractRegExpGlobals[i] << "'!\n"; + return 1; + } + } + + // Figure out which functions we should extract. + for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { + GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]); + if (!GV) { + errs() << argv[0] << ": program doesn't contain function named '" + << ExtractFuncs[i] << "'!\n"; + return 1; + } + GVs.insert(GV); + } + // Extract functions via regular expression matching. + for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) { + std::string Error; + StringRef RegExStr = ExtractRegExpFuncs[i]; + Regex RegEx(RegExStr); + if (!RegEx.isValid(Error)) { + errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' " + "invalid regex: " << Error; + } + bool match = false; + for (Module::iterator F = M.get()->begin(), E = M.get()->end(); F != E; + F++) { + if (RegEx.match(F->getName())) { + GVs.insert(&*F); + match = true; + } + } + if (!match) { + errs() << argv[0] << ": program doesn't contain global named '" + << ExtractRegExpFuncs[i] << "'!\n"; + return 1; + } + } + + // Materialize requisite global values. + if (!DeleteFn) + for (size_t i = 0, e = GVs.size(); i != e; ++i) { + GlobalValue *GV = GVs[i]; + if (GV->isMaterializable()) { + std::string ErrInfo; + if (GV->Materialize(&ErrInfo)) { + errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; + return 1; + } + } + } + else { + // Deleting. Materialize every GV that's *not* in GVs. + SmallPtrSet GVSet(GVs.begin(), GVs.end()); + for (Module::global_iterator I = M->global_begin(), E = M->global_end(); + I != E; ++I) { + GlobalVariable *G = I; + if (!GVSet.count(G) && G->isMaterializable()) { + std::string ErrInfo; + if (G->Materialize(&ErrInfo)) { + errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; + return 1; + } + } + } + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + Function *F = I; + if (!GVSet.count(F) && F->isMaterializable()) { + std::string ErrInfo; + if (F->Materialize(&ErrInfo)) { + errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; + return 1; + } + } + } + } + + // In addition to deleting all other functions, we also want to spiff it + // up a little bit. Do this now. + PassManager Passes; + Passes.add(new TargetData(M.get())); // Use correct TargetData + + std::vector Gvs(GVs.begin(), GVs.end()); + + Passes.add(createGVExtractionPass(Gvs, DeleteFn)); + if (!DeleteFn) + Passes.add(createGlobalDCEPass()); // Delete unreachable globals + Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info + Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls + + std::string ErrorInfo; + tool_output_file Out(OutputFilename.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + return 1; + } + + if (OutputAssembly) + Passes.add(createPrintModulePass(&Out.os())); + else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) + Passes.add(createBitcodeWriterPass(Out.os())); + + Passes.run(*M.get()); + + // Declare success. + Out.keep(); + + return 0; +} diff --git a/contrib/llvm/tools/llvm-ld/CMakeLists.txt b/contrib/llvm/tools/llvm-ld/CMakeLists.txt new file mode 100644 index 000000000..370bcb4ab --- /dev/null +++ b/contrib/llvm/tools/llvm-ld/CMakeLists.txt @@ -0,0 +1,8 @@ +set(LLVM_LINK_COMPONENTS ipo scalaropts linker archive bitwriter) + +add_llvm_tool(llvm-ld + Optimize.cpp + llvm-ld.cpp + ) + +add_dependencies(llvm-ld llvm-stub) diff --git a/contrib/llvm/tools/llvm-ld/Makefile b/contrib/llvm/tools/llvm-ld/Makefile new file mode 100644 index 000000000..1ef9bf117 --- /dev/null +++ b/contrib/llvm/tools/llvm-ld/Makefile @@ -0,0 +1,15 @@ +##===- tools/llvm-ld/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. + +TOOLNAME = llvm-ld +LINK_COMPONENTS = ipo scalaropts linker archive bitwriter + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-ld/Optimize.cpp b/contrib/llvm/tools/llvm-ld/Optimize.cpp new file mode 100644 index 000000000..7f3f90014 --- /dev/null +++ b/contrib/llvm/tools/llvm-ld/Optimize.cpp @@ -0,0 +1,130 @@ +//===- Optimize.cpp - Optimize a complete program -------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements all optimization of the linked module for llvm-ld. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/DynamicLibrary.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Support/PassNameParser.h" +#include "llvm/Support/PluginLoader.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/IPO/PassManagerBuilder.h" +#include "llvm/Transforms/Scalar.h" +using namespace llvm; + +// Pass Name Options as generated by the PassNameParser +static cl::list + OptimizationList(cl::desc("Optimizations available:")); + +//Don't verify at the end +static cl::opt DontVerify("disable-verify", cl::ReallyHidden); + +static cl::opt DisableInline("disable-inlining", + cl::desc("Do not run the inliner pass")); + +static cl::opt +DisableOptimizations("disable-opt", + cl::desc("Do not run any optimization passes")); + +static cl::opt DisableInternalize("disable-internalize", + cl::desc("Do not mark all symbols as internal")); + +static cl::opt VerifyEach("verify-each", + cl::desc("Verify intermediate results of all passes")); + +static cl::alias ExportDynamic("export-dynamic", + cl::aliasopt(DisableInternalize), + cl::desc("Alias for -disable-internalize")); + +static cl::opt Strip("strip-all", + cl::desc("Strip all symbol info from executable")); + +static cl::alias A0("s", cl::desc("Alias for --strip-all"), + cl::aliasopt(Strip)); + +static cl::opt StripDebug("strip-debug", + cl::desc("Strip debugger symbol info from executable")); + +static cl::alias A1("S", cl::desc("Alias for --strip-debug"), + cl::aliasopt(StripDebug)); + +// A utility function that adds a pass to the pass manager but will also add +// a verifier pass after if we're supposed to verify. +static inline void addPass(PassManager &PM, Pass *P) { + // Add the pass to the pass manager... + PM.add(P); + + // If we are verifying all of the intermediate steps, add the verifier... + if (VerifyEach) + PM.add(createVerifierPass()); +} + +namespace llvm { +/// Optimize - Perform link time optimizations. This will run the scalar +/// optimizations, any loaded plugin-optimization modules, and then the +/// inter-procedural optimizations if applicable. +void Optimize(Module *M) { + + // Instantiate the pass manager to organize the passes. + PassManager Passes; + + // If we're verifying, start off with a verification pass. + if (VerifyEach) + Passes.add(createVerifierPass()); + + // Add an appropriate TargetData instance for this module... + addPass(Passes, new TargetData(M)); + + if (!DisableOptimizations) + PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize, + !DisableInline); + + // If the -s or -S command line options were specified, strip the symbols out + // of the resulting program to make it smaller. -s and -S are GNU ld options + // that we are supporting; they alias -strip-all and -strip-debug. + if (Strip || StripDebug) + addPass(Passes, createStripSymbolsPass(StripDebug && !Strip)); + + // Create a new optimization pass for each one specified on the command line + std::auto_ptr target; + for (unsigned i = 0; i < OptimizationList.size(); ++i) { + const PassInfo *Opt = OptimizationList[i]; + if (Opt->getNormalCtor()) + addPass(Passes, Opt->getNormalCtor()()); + else + errs() << "llvm-ld: cannot create pass: " << Opt->getPassName() + << "\n"; + } + + // The user's passes may leave cruft around. Clean up after them them but + // only if we haven't got DisableOptimizations set + if (!DisableOptimizations) { + addPass(Passes, createInstructionCombiningPass()); + addPass(Passes, createCFGSimplificationPass()); + addPass(Passes, createAggressiveDCEPass()); + addPass(Passes, createGlobalDCEPass()); + } + + // Make sure everything is still good. + if (!DontVerify) + Passes.add(createVerifierPass()); + + // Run our queue of passes all at once now, efficiently. + Passes.run(*M); +} + +} diff --git a/contrib/llvm/tools/llvm-ld/llvm-ld.cpp b/contrib/llvm/tools/llvm-ld/llvm-ld.cpp new file mode 100644 index 000000000..6b4c3c772 --- /dev/null +++ b/contrib/llvm/tools/llvm-ld/llvm-ld.cpp @@ -0,0 +1,732 @@ +//===- llvm-ld.cpp - LLVM 'ld' compatible linker --------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility is intended to be compatible with GCC, and follows standard +// system 'ld' conventions. As such, the default output file is ./a.out. +// Additionally, this program outputs a shell script that is used to invoke LLI +// to execute the program. In this manner, the generated executable (a.out for +// example), is directly executable, whereas the bitcode file actually lives in +// the a.out.bc file generated by this program. +// +// Note that if someone (or a script) deletes the executable program generated, +// the .bc file will be left around. Considering that this is a temporary hack, +// I'm not too worried about this. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LinkAllVMCore.h" +#include "llvm/Linker.h" +#include "llvm/LLVMContext.h" +#include "llvm/Support/Program.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Signals.h" +#include "llvm/Config/config.h" +#include +#include +using namespace llvm; + +// Rightly this should go in a header file but it just seems such a waste. +namespace llvm { +extern void Optimize(Module*); +} + +// Input/Output Options +static cl::list InputFilenames(cl::Positional, cl::OneOrMore, + cl::desc("")); + +static cl::opt OutputFilename("o", cl::init("a.out"), + cl::desc("Override output filename"), + cl::value_desc("filename")); + +static cl::opt BitcodeOutputFilename("b", cl::init(""), + cl::desc("Override bitcode output filename"), + cl::value_desc("filename")); + +static cl::opt Verbose("v", + cl::desc("Print information about actions taken")); + +static cl::list LibPaths("L", cl::Prefix, + cl::desc("Specify a library search path"), + cl::value_desc("directory")); + +static cl::list FrameworkPaths("F", cl::Prefix, + cl::desc("Specify a framework search path"), + cl::value_desc("directory")); + +static cl::list Libraries("l", cl::Prefix, + cl::desc("Specify libraries to link to"), + cl::value_desc("library prefix")); + +static cl::list Frameworks("framework", + cl::desc("Specify frameworks to link to"), + cl::value_desc("framework")); + +// Options to control the linking, optimization, and code gen processes +static cl::opt LinkAsLibrary("link-as-library", + cl::desc("Link the .bc files together as a library, not an executable")); + +static cl::alias Relink("r", cl::aliasopt(LinkAsLibrary), + cl::desc("Alias for -link-as-library")); + +static cl::opt Native("native", + cl::desc("Generate a native binary instead of a shell script")); + +static cl::optNativeCBE("native-cbe", + cl::desc("Generate a native binary with the C backend and GCC")); + +static cl::list PostLinkOpts("post-link-opts", + cl::value_desc("path"), + cl::desc("Run one or more optimization programs after linking")); + +static cl::list XLinker("Xlinker", cl::value_desc("option"), + cl::desc("Pass options to the system linker")); + +// Compatibility options that llvm-ld ignores but are supported for +// compatibility with LD +static cl::opt CO3("soname", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +static cl::opt CO4("version-script", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +static cl::opt CO5("eh-frame-hdr", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +static cl::opt CO6("h", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +static cl::opt CO7("start-group", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +static cl::opt CO8("end-group", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +static cl::opt CO9("m", cl::Hidden, + cl::desc("Compatibility option: ignored")); + +/// This is just for convenience so it doesn't have to be passed around +/// everywhere. +static std::string progname; + +/// FileRemover objects to clean up output files in the event of an error. +static FileRemover OutputRemover; +static FileRemover BitcodeOutputRemover; + +/// PrintAndExit - Prints a message to standard error and exits with error code +/// +/// Inputs: +/// Message - The message to print to standard error. +/// +static void PrintAndExit(const std::string &Message, Module *M, int errcode = 1) { + errs() << progname << ": " << Message << "\n"; + delete M; + llvm_shutdown(); + exit(errcode); +} + +static void PrintCommand(const std::vector &args) { + std::vector::const_iterator I = args.begin(), E = args.end(); + for (; I != E; ++I) + if (*I) + errs() << "'" << *I << "'" << " "; + errs() << "\n"; +} + +/// CopyEnv - This function takes an array of environment variables and makes a +/// copy of it. This copy can then be manipulated any way the caller likes +/// without affecting the process's real environment. +/// +/// Inputs: +/// envp - An array of C strings containing an environment. +/// +/// Return value: +/// NULL - An error occurred. +/// +/// Otherwise, a pointer to a new array of C strings is returned. Every string +/// in the array is a duplicate of the one in the original array (i.e. we do +/// not copy the char *'s from one array to another). +/// +static char ** CopyEnv(char ** const envp) { + // Count the number of entries in the old list; + unsigned entries; // The number of entries in the old environment list + for (entries = 0; envp[entries] != NULL; entries++) + /*empty*/; + + // Add one more entry for the NULL pointer that ends the list. + ++entries; + + // If there are no entries at all, just return NULL. + if (entries == 0) + return NULL; + + // Allocate a new environment list. + char **newenv = new char* [entries]; + if (newenv == NULL) + return NULL; + + // Make a copy of the list. Don't forget the NULL that ends the list. + entries = 0; + while (envp[entries] != NULL) { + size_t len = strlen(envp[entries]) + 1; + newenv[entries] = new char[len]; + memcpy(newenv[entries], envp[entries], len); + ++entries; + } + newenv[entries] = NULL; + + return newenv; +} + + +/// RemoveEnv - Remove the specified environment variable from the environment +/// array. +/// +/// Inputs: +/// name - The name of the variable to remove. It cannot be NULL. +/// envp - The array of environment variables. It cannot be NULL. +/// +/// Notes: +/// This is mainly done because functions to remove items from the environment +/// are not available across all platforms. In particular, Solaris does not +/// seem to have an unsetenv() function or a setenv() function (or they are +/// undocumented if they do exist). +/// +static void RemoveEnv(const char * name, char ** const envp) { + for (unsigned index=0; envp[index] != NULL; index++) { + // Find the first equals sign in the array and make it an EOS character. + char *p = strchr (envp[index], '='); + if (p == NULL) + continue; + else + *p = '\0'; + + // Compare the two strings. If they are equal, zap this string. + // Otherwise, restore it. + if (!strcmp(name, envp[index])) + *envp[index] = '\0'; + else + *p = '='; + } + + return; +} + +/// GenerateBitcode - generates a bitcode file from the module provided +void GenerateBitcode(Module* M, const std::string& FileName) { + + if (Verbose) + errs() << "Generating Bitcode To " << FileName << '\n'; + + // Create the output file. + std::string ErrorInfo; + tool_output_file Out(FileName.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary); + if (!ErrorInfo.empty()) { + PrintAndExit(ErrorInfo, M); + return; + } + + // Write it out + WriteBitcodeToFile(M, Out.os()); + Out.keep(); +} + +/// GenerateAssembly - generates a native assembly language source file from the +/// specified bitcode file. +/// +/// Inputs: +/// InputFilename - The name of the input bitcode file. +/// OutputFilename - The name of the file to generate. +/// llc - The pathname to use for LLC. +/// envp - The environment to use when running LLC. +/// +/// Return non-zero value on error. +/// +static int GenerateAssembly(const std::string &OutputFilename, + const std::string &InputFilename, + const sys::Path &llc, + std::string &ErrMsg ) { + // Run LLC to convert the bitcode file into assembly code. + std::vector args; + args.push_back(llc.c_str()); + // We will use GCC to assemble the program so set the assembly syntax to AT&T, + // regardless of what the target in the bitcode file is. + args.push_back("-x86-asm-syntax=att"); + args.push_back("-o"); + args.push_back(OutputFilename.c_str()); + args.push_back(InputFilename.c_str()); + args.push_back(0); + + if (Verbose) { + errs() << "Generating Assembly With: \n"; + PrintCommand(args); + } + + return sys::Program::ExecuteAndWait(llc, &args[0], 0, 0, 0, 0, &ErrMsg); +} + +/// GenerateCFile - generates a C source file from the specified bitcode file. +static int GenerateCFile(const std::string &OutputFile, + const std::string &InputFile, + const sys::Path &llc, + std::string& ErrMsg) { + // Run LLC to convert the bitcode file into C. + std::vector args; + args.push_back(llc.c_str()); + args.push_back("-march=c"); + args.push_back("-o"); + args.push_back(OutputFile.c_str()); + args.push_back(InputFile.c_str()); + args.push_back(0); + + if (Verbose) { + errs() << "Generating C Source With: \n"; + PrintCommand(args); + } + + return sys::Program::ExecuteAndWait(llc, &args[0], 0, 0, 0, 0, &ErrMsg); +} + +/// GenerateNative - generates a native object file from the +/// specified bitcode file. +/// +/// Inputs: +/// InputFilename - The name of the input bitcode file. +/// OutputFilename - The name of the file to generate. +/// NativeLinkItems - The native libraries, files, code with which to link +/// LibPaths - The list of directories in which to find libraries. +/// FrameworksPaths - The list of directories in which to find frameworks. +/// Frameworks - The list of frameworks (dynamic libraries) +/// gcc - The pathname to use for GGC. +/// envp - A copy of the process's current environment. +/// +/// Outputs: +/// None. +/// +/// Returns non-zero value on error. +/// +static int GenerateNative(const std::string &OutputFilename, + const std::string &InputFilename, + const Linker::ItemList &LinkItems, + const sys::Path &gcc, char ** const envp, + std::string& ErrMsg) { + // Remove these environment variables from the environment of the + // programs that we will execute. It appears that GCC sets these + // environment variables so that the programs it uses can configure + // themselves identically. + // + // However, when we invoke GCC below, we want it to use its normal + // configuration. Hence, we must sanitize its environment. + char ** clean_env = CopyEnv(envp); + if (clean_env == NULL) + return 1; + RemoveEnv("LIBRARY_PATH", clean_env); + RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); + RemoveEnv("GCC_EXEC_PREFIX", clean_env); + RemoveEnv("COMPILER_PATH", clean_env); + RemoveEnv("COLLECT_GCC", clean_env); + + + // Run GCC to assemble and link the program into native code. + // + // Note: + // We can't just assemble and link the file with the system assembler + // and linker because we don't know where to put the _start symbol. + // GCC mysteriously knows how to do it. + std::vector args; + args.push_back(gcc.c_str()); + args.push_back("-fno-strict-aliasing"); + args.push_back("-O3"); + args.push_back("-o"); + args.push_back(OutputFilename); + args.push_back(InputFilename); + + // Add in the library and framework paths + for (unsigned index = 0; index < LibPaths.size(); index++) { + args.push_back("-L" + LibPaths[index]); + } + for (unsigned index = 0; index < FrameworkPaths.size(); index++) { + args.push_back("-F" + FrameworkPaths[index]); + } + + // Add the requested options + for (unsigned index = 0; index < XLinker.size(); index++) + args.push_back(XLinker[index]); + + // Add in the libraries to link. + for (unsigned index = 0; index < LinkItems.size(); index++) + if (LinkItems[index].first != "crtend") { + if (LinkItems[index].second) + args.push_back("-l" + LinkItems[index].first); + else + args.push_back(LinkItems[index].first); + } + + // Add in frameworks to link. + for (unsigned index = 0; index < Frameworks.size(); index++) { + args.push_back("-framework"); + args.push_back(Frameworks[index]); + } + + // Now that "args" owns all the std::strings for the arguments, call the c_str + // method to get the underlying string array. We do this game so that the + // std::string array is guaranteed to outlive the const char* array. + std::vector Args; + for (unsigned i = 0, e = args.size(); i != e; ++i) + Args.push_back(args[i].c_str()); + Args.push_back(0); + + if (Verbose) { + errs() << "Generating Native Executable With:\n"; + PrintCommand(Args); + } + + // Run the compiler to assembly and link together the program. + int R = sys::Program::ExecuteAndWait( + gcc, &Args[0], const_cast(clean_env), 0, 0, 0, &ErrMsg); + delete [] clean_env; + return R; +} + +/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM +/// bitcode file for the program. +static void EmitShellScript(char **argv, Module *M) { + if (Verbose) + errs() << "Emitting Shell Script\n"; +#if defined(_WIN32) + // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To + // support windows systems, we copy the llvm-stub.exe executable from the + // build tree to the destination file. + std::string ErrMsg; + sys::Path llvmstub = PrependMainExecutablePath("llvm-stub", argv[0], + (void *)(intptr_t)&Optimize); + if (llvmstub.isEmpty()) + PrintAndExit("Could not find llvm-stub.exe executable!", M); + + if (0 != sys::CopyFile(sys::Path(OutputFilename), llvmstub, &ErrMsg)) + PrintAndExit(ErrMsg, M); + + return; +#endif + + // Output the script to start the program... + std::string ErrorInfo; + tool_output_file Out2(OutputFilename.c_str(), ErrorInfo); + if (!ErrorInfo.empty()) + PrintAndExit(ErrorInfo, M); + + Out2.os() << "#!/bin/sh\n"; + // Allow user to setenv LLVMINTERP if lli is not in their PATH. + Out2.os() << "lli=${LLVMINTERP-lli}\n"; + Out2.os() << "exec $lli \\\n"; + // gcc accepts -l and implicitly searches /lib and /usr/lib. + LibPaths.push_back("/lib"); + LibPaths.push_back("/usr/lib"); + LibPaths.push_back("/usr/X11R6/lib"); + // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a + // shared object at all! See RH 8: plain text. + std::vector::iterator libc = + std::find(Libraries.begin(), Libraries.end(), "c"); + if (libc != Libraries.end()) Libraries.erase(libc); + // List all the shared object (native) libraries this executable will need + // on the command line, so that we don't have to do this manually! + for (std::vector::iterator i = Libraries.begin(), + e = Libraries.end(); i != e; ++i) { + // try explicit -L arguments first: + sys::Path FullLibraryPath; + for (cl::list::const_iterator P = LibPaths.begin(), + E = LibPaths.end(); P != E; ++P) { + FullLibraryPath = *P; + FullLibraryPath.appendComponent("lib" + *i); + FullLibraryPath.appendSuffix(sys::Path::GetDLLSuffix()); + if (!FullLibraryPath.isEmpty()) { + if (!FullLibraryPath.isDynamicLibrary()) { + // Not a native shared library; mark as invalid + FullLibraryPath = sys::Path(); + } else break; + } + } + if (FullLibraryPath.isEmpty()) + FullLibraryPath = sys::Path::FindLibrary(*i); + if (!FullLibraryPath.isEmpty()) + Out2.os() << " -load=" << FullLibraryPath.str() << " \\\n"; + } + Out2.os() << " " << BitcodeOutputFilename << " ${1+\"$@\"}\n"; + Out2.keep(); +} + +// BuildLinkItems -- This function generates a LinkItemList for the LinkItems +// linker function by combining the Files and Libraries in the order they were +// declared on the command line. +static void BuildLinkItems( + Linker::ItemList& Items, + const cl::list& Files, + const cl::list& Libraries) { + + // Build the list of linkage items for LinkItems. + + cl::list::const_iterator fileIt = Files.begin(); + cl::list::const_iterator libIt = Libraries.begin(); + + int libPos = -1, filePos = -1; + while ( libIt != Libraries.end() || fileIt != Files.end() ) { + if (libIt != Libraries.end()) + libPos = Libraries.getPosition(libIt - Libraries.begin()); + else + libPos = -1; + if (fileIt != Files.end()) + filePos = Files.getPosition(fileIt - Files.begin()); + else + filePos = -1; + + if (filePos != -1 && (libPos == -1 || filePos < libPos)) { + // Add a source file + Items.push_back(std::make_pair(*fileIt++, false)); + } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) { + // Add a library + Items.push_back(std::make_pair(*libIt++, true)); + } + } +} + +int main(int argc, char **argv, char **envp) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + // Initialize passes + PassRegistry &Registry = *PassRegistry::getPassRegistry(); + initializeCore(Registry); + initializeScalarOpts(Registry); + initializeIPO(Registry); + initializeAnalysis(Registry); + initializeIPA(Registry); + initializeTransformUtils(Registry); + initializeInstCombine(Registry); + initializeTarget(Registry); + + // Initial global variable above for convenience printing of program name. + progname = sys::path::stem(argv[0]); + + // Parse the command line options + cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); + +#if defined(_WIN32) || defined(__CYGWIN__) + if (!LinkAsLibrary) { + // Default to "a.exe" instead of "a.out". + if (OutputFilename.getNumOccurrences() == 0) + OutputFilename = "a.exe"; + + // If there is no suffix add an "exe" one. + if (sys::path::extension(OutputFilename).empty()) + OutputFilename.append(".exe"); + } +#endif + + // Generate the bitcode for the optimized module. + // If -b wasn't specified, use the name specified + // with -o to construct BitcodeOutputFilename. + if (BitcodeOutputFilename.empty()) { + BitcodeOutputFilename = OutputFilename; + if (!LinkAsLibrary) BitcodeOutputFilename += ".bc"; + } + + // Arrange for the bitcode output file to be deleted on any errors. + BitcodeOutputRemover.setFile(BitcodeOutputFilename); + sys::RemoveFileOnSignal(sys::Path(BitcodeOutputFilename)); + + // Arrange for the output file to be deleted on any errors. + if (!LinkAsLibrary) { + OutputRemover.setFile(OutputFilename); + sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + } + + // Construct a Linker (now that Verbose is set) + Linker TheLinker(progname, OutputFilename, Context, Verbose); + + // Keep track of the native link items (versus the bitcode items) + Linker::ItemList NativeLinkItems; + + // Add library paths to the linker + TheLinker.addPaths(LibPaths); + TheLinker.addSystemPaths(); + + // Remove any consecutive duplicates of the same library... + Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), + Libraries.end()); + + if (LinkAsLibrary) { + std::vector Files; + for (unsigned i = 0; i < InputFilenames.size(); ++i ) + Files.push_back(sys::Path(InputFilenames[i])); + if (TheLinker.LinkInFiles(Files)) + return 1; // Error already printed + + // The libraries aren't linked in but are noted as "dependent" in the + // module. + for (cl::list::const_iterator I = Libraries.begin(), + E = Libraries.end(); I != E ; ++I) { + TheLinker.getModule()->addLibrary(*I); + } + } else { + // Build a list of the items from our command line + Linker::ItemList Items; + BuildLinkItems(Items, InputFilenames, Libraries); + + // Link all the items together + if (TheLinker.LinkInItems(Items, NativeLinkItems) ) + return 1; // Error already printed + } + + std::auto_ptr Composite(TheLinker.releaseModule()); + + // Optimize the module + Optimize(Composite.get()); + + // Generate the bitcode output. + GenerateBitcode(Composite.get(), BitcodeOutputFilename); + + // If we are not linking a library, generate either a native executable + // or a JIT shell script, depending upon what the user wants. + if (!LinkAsLibrary) { + // If the user wants to run a post-link optimization, run it now. + if (!PostLinkOpts.empty()) { + std::vector opts = PostLinkOpts; + for (std::vector::iterator I = opts.begin(), + E = opts.end(); I != E; ++I) { + sys::Path prog(*I); + if (!prog.canExecute()) { + prog = sys::Program::FindProgramByName(*I); + if (prog.isEmpty()) + PrintAndExit(std::string("Optimization program '") + *I + + "' is not found or not executable.", Composite.get()); + } + // Get the program arguments + sys::Path tmp_output("opt_result"); + std::string ErrMsg; + if (tmp_output.createTemporaryFileOnDisk(true, &ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + + const char* args[4]; + args[0] = I->c_str(); + args[1] = BitcodeOutputFilename.c_str(); + args[2] = tmp_output.c_str(); + args[3] = 0; + if (0 == sys::Program::ExecuteAndWait(prog, args, 0,0,0,0, &ErrMsg)) { + if (tmp_output.isBitcodeFile()) { + sys::Path target(BitcodeOutputFilename); + target.eraseFromDisk(); + if (tmp_output.renamePathOnDisk(target, &ErrMsg)) + PrintAndExit(ErrMsg, Composite.get(), 2); + } else + PrintAndExit("Post-link optimization output is not bitcode", + Composite.get()); + } else { + PrintAndExit(ErrMsg, Composite.get()); + } + } + } + + // If the user wants to generate a native executable, compile it from the + // bitcode file. + // + // Otherwise, create a script that will run the bitcode through the JIT. + if (Native) { + // Name of the Assembly Language output file + sys::Path AssemblyFile ( OutputFilename); + AssemblyFile.appendSuffix("s"); + + // Mark the output files for removal. + FileRemover AssemblyFileRemover(AssemblyFile.str()); + sys::RemoveFileOnSignal(AssemblyFile); + + // Determine the locations of the llc and gcc programs. + sys::Path llc = PrependMainExecutablePath("llc", argv[0], + (void *)(intptr_t)&Optimize); + if (llc.isEmpty()) + PrintAndExit("Failed to find llc", Composite.get()); + + sys::Path gcc = sys::Program::FindProgramByName("gcc"); + if (gcc.isEmpty()) + PrintAndExit("Failed to find gcc", Composite.get()); + + // Generate an assembly language file for the bitcode. + std::string ErrMsg; + if (0 != GenerateAssembly(AssemblyFile.str(), BitcodeOutputFilename, + llc, ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + + if (0 != GenerateNative(OutputFilename, AssemblyFile.str(), + NativeLinkItems, gcc, envp, ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + } else if (NativeCBE) { + sys::Path CFile (OutputFilename); + CFile.appendSuffix("cbe.c"); + + // Mark the output files for removal. + FileRemover CFileRemover(CFile.str()); + sys::RemoveFileOnSignal(CFile); + + // Determine the locations of the llc and gcc programs. + sys::Path llc = PrependMainExecutablePath("llc", argv[0], + (void *)(intptr_t)&Optimize); + if (llc.isEmpty()) + PrintAndExit("Failed to find llc", Composite.get()); + + sys::Path gcc = sys::Program::FindProgramByName("gcc"); + if (gcc.isEmpty()) + PrintAndExit("Failed to find gcc", Composite.get()); + + // Generate an assembly language file for the bitcode. + std::string ErrMsg; + if (GenerateCFile(CFile.str(), BitcodeOutputFilename, llc, ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + + if (GenerateNative(OutputFilename, CFile.str(), + NativeLinkItems, gcc, envp, ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + } else { + EmitShellScript(argv, Composite.get()); + } + + // Make the script executable... + std::string ErrMsg; + if (sys::Path(OutputFilename).makeExecutableOnDisk(&ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + + // Make the bitcode file readable and directly executable in LLEE as well + if (sys::Path(BitcodeOutputFilename).makeExecutableOnDisk(&ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + + if (sys::Path(BitcodeOutputFilename).makeReadableOnDisk(&ErrMsg)) + PrintAndExit(ErrMsg, Composite.get()); + } + + // Operations which may fail are now complete. + BitcodeOutputRemover.releaseFile(); + if (!LinkAsLibrary) + OutputRemover.releaseFile(); + + // Graceful exit + return 0; +} diff --git a/contrib/llvm/tools/llvm-link/CMakeLists.txt b/contrib/llvm/tools/llvm-link/CMakeLists.txt new file mode 100644 index 000000000..11933f7f9 --- /dev/null +++ b/contrib/llvm/tools/llvm-link/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS linker bitreader bitwriter asmparser) + +add_llvm_tool(llvm-link + llvm-link.cpp + ) diff --git a/contrib/llvm/tools/llvm-link/Makefile b/contrib/llvm/tools/llvm-link/Makefile new file mode 100644 index 000000000..26370187c --- /dev/null +++ b/contrib/llvm/tools/llvm-link/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-link/Makefile ----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-link +LINK_COMPONENTS = linker bitreader bitwriter asmparser + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-link/llvm-link.cpp b/contrib/llvm/tools/llvm-link/llvm-link.cpp new file mode 100644 index 000000000..95ad1ca5a --- /dev/null +++ b/contrib/llvm/tools/llvm-link/llvm-link.cpp @@ -0,0 +1,142 @@ +//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility may be invoked in the following manner: +// llvm-link a.bc b.bc c.bc -o x.bc +// +//===----------------------------------------------------------------------===// + +#include "llvm/Linker.h" +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/Path.h" +#include +using namespace llvm; + +static cl::list +InputFilenames(cl::Positional, cl::OneOrMore, + cl::desc("")); + +static cl::opt +OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), + cl::value_desc("filename")); + +static cl::opt +Force("f", cl::desc("Enable binary output on terminals")); + +static cl::opt +OutputAssembly("S", + cl::desc("Write output as LLVM assembly"), cl::Hidden); + +static cl::opt +Verbose("v", cl::desc("Print information about actions taken")); + +static cl::opt +DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden); + +// LoadFile - Read the specified bitcode file in and return it. This routine +// searches the link path for the specified file to try to find it... +// +static inline std::auto_ptr LoadFile(const char *argv0, + const std::string &FN, + LLVMContext& Context) { + sys::Path Filename; + if (!Filename.set(FN)) { + errs() << "Invalid file name: '" << FN << "'\n"; + return std::auto_ptr(); + } + + SMDiagnostic Err; + if (Verbose) errs() << "Loading '" << Filename.c_str() << "'\n"; + Module* Result = 0; + + const std::string &FNStr = Filename.str(); + Result = ParseIRFile(FNStr, Err, Context); + if (Result) return std::auto_ptr(Result); // Load successful! + + Err.Print(argv0, errs()); + return std::auto_ptr(); +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); + + unsigned BaseArg = 0; + std::string ErrorMessage; + + std::auto_ptr Composite(LoadFile(argv[0], + InputFilenames[BaseArg], Context)); + if (Composite.get() == 0) { + errs() << argv[0] << ": error loading file '" + << InputFilenames[BaseArg] << "'\n"; + return 1; + } + + for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) { + std::auto_ptr M(LoadFile(argv[0], + InputFilenames[i], Context)); + if (M.get() == 0) { + errs() << argv[0] << ": error loading file '" < > ByteArrayTy; + +namespace { +class VectorMemoryObject : public MemoryObject { +private: + const ByteArrayTy &Bytes; +public: + VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {} + + uint64_t getBase() const { return 0; } + uint64_t getExtent() const { return Bytes.size(); } + + int readByte(uint64_t Addr, uint8_t *Byte) const { + if (Addr >= getExtent()) + return -1; + *Byte = Bytes[Addr].first; + return 0; + } +}; +} + +static bool PrintInsts(const MCDisassembler &DisAsm, + MCInstPrinter &Printer, const ByteArrayTy &Bytes, + SourceMgr &SM, raw_ostream &Out) { + // Wrap the vector in a MemoryObject. + VectorMemoryObject memoryObject(Bytes); + + // Disassemble it to strings. + uint64_t Size; + uint64_t Index; + + for (Index = 0; Index < Bytes.size(); Index += Size) { + MCInst Inst; + + MCDisassembler::DecodeStatus S; + S = DisAsm.getInstruction(Inst, Size, memoryObject, Index, + /*REMOVE*/ nulls(), nulls()); + switch (S) { + case MCDisassembler::Fail: + SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second), + "invalid instruction encoding", "warning"); + if (Size == 0) + Size = 1; // skip illegible bytes + break; + + case MCDisassembler::SoftFail: + SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second), + "potentially undefined instruction encoding", "warning"); + // Fall through + + case MCDisassembler::Success: + Printer.printInst(&Inst, Out, ""); + Out << "\n"; + break; + } + } + + return false; +} + +static bool ByteArrayFromString(ByteArrayTy &ByteArray, + StringRef &Str, + SourceMgr &SM) { + while (!Str.empty()) { + // Strip horizontal whitespace. + if (size_t Pos = Str.find_first_not_of(" \t\r")) { + Str = Str.substr(Pos); + continue; + } + + // If this is the end of a line or start of a comment, remove the rest of + // the line. + if (Str[0] == '\n' || Str[0] == '#') { + // Strip to the end of line if we already processed any bytes on this + // line. This strips the comment and/or the \n. + if (Str[0] == '\n') { + Str = Str.substr(1); + } else { + Str = Str.substr(Str.find_first_of('\n')); + if (!Str.empty()) + Str = Str.substr(1); + } + continue; + } + + // Get the current token. + size_t Next = Str.find_first_of(" \t\n\r#"); + StringRef Value = Str.substr(0, Next); + + // Convert to a byte and add to the byte vector. + unsigned ByteVal; + if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) { + // If we have an error, print it and skip to the end of line. + SM.PrintMessage(SMLoc::getFromPointer(Value.data()), + "invalid input token", "error"); + Str = Str.substr(Str.find('\n')); + ByteArray.clear(); + continue; + } + + ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data())); + Str = Str.substr(Next); + } + + return false; +} + +int Disassembler::disassemble(const Target &T, + const std::string &Triple, + const std::string &Cpu, + const std::string &FeaturesStr, + MemoryBuffer &Buffer, + raw_ostream &Out) { + // Set up disassembler. + OwningPtr AsmInfo(T.createMCAsmInfo(Triple)); + + if (!AsmInfo) { + errs() << "error: no assembly info for target " << Triple << "\n"; + return -1; + } + + OwningPtr STI(T.createMCSubtargetInfo(Triple, Cpu, FeaturesStr)); + if (!STI) { + errs() << "error: no subtarget info for target " << Triple << "\n"; + return -1; + } + + OwningPtr DisAsm(T.createMCDisassembler(*STI)); + if (!DisAsm) { + errs() << "error: no disassembler for target " << Triple << "\n"; + return -1; + } + + int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); + OwningPtr IP(T.createMCInstPrinter(AsmPrinterVariant, + *AsmInfo, *STI)); + if (!IP) { + errs() << "error: no instruction printer for target " << Triple << '\n'; + return -1; + } + + bool ErrorOccurred = false; + + SourceMgr SM; + SM.AddNewSourceBuffer(&Buffer, SMLoc()); + + // Convert the input to a vector for disassembly. + ByteArrayTy ByteArray; + StringRef Str = Buffer.getBuffer(); + + ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM); + + if (!ByteArray.empty()) + ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM, Out); + + return ErrorOccurred; +} + +static int byteArrayReader(uint8_t *B, uint64_t A, void *Arg) { + ByteArrayTy &ByteArray = *((ByteArrayTy*)Arg); + + if (A >= ByteArray.size()) + return -1; + + *B = ByteArray[A].first; + + return 0; +} + +static int verboseEvaluator(uint64_t *V, unsigned R, void *Arg) { + EDDisassembler &disassembler = *(EDDisassembler *)((void **)Arg)[0]; + raw_ostream &Out = *(raw_ostream *)((void **)Arg)[1]; + + if (const char *regName = disassembler.nameWithRegisterID(R)) + Out << "[" << regName << "/" << R << "]"; + + if (disassembler.registerIsStackPointer(R)) + Out << "(sp)"; + if (disassembler.registerIsProgramCounter(R)) + Out << "(pc)"; + + *V = 0; + return 0; +} + +int Disassembler::disassembleEnhanced(const std::string &TS, + MemoryBuffer &Buffer, + raw_ostream &Out) { + ByteArrayTy ByteArray; + StringRef Str = Buffer.getBuffer(); + SourceMgr SM; + + SM.AddNewSourceBuffer(&Buffer, SMLoc()); + + if (ByteArrayFromString(ByteArray, Str, SM)) { + return -1; + } + + Triple T(TS); + EDDisassembler::AssemblySyntax AS; + + switch (T.getArch()) { + default: + errs() << "error: no default assembly syntax for " << TS.c_str() << "\n"; + return -1; + case Triple::arm: + case Triple::thumb: + AS = EDDisassembler::kEDAssemblySyntaxARMUAL; + break; + case Triple::x86: + case Triple::x86_64: + AS = EDDisassembler::kEDAssemblySyntaxX86ATT; + break; + } + + EDDisassembler::initialize(); + OwningPtr + disassembler(EDDisassembler::getDisassembler(TS.c_str(), AS)); + + if (disassembler == 0) { + errs() << "error: couldn't get disassembler for " << TS << '\n'; + return -1; + } + + while (ByteArray.size()) { + OwningPtr + inst(disassembler->createInst(byteArrayReader, 0, &ByteArray)); + + if (inst == 0) { + errs() << "error: Didn't get an instruction\n"; + return -1; + } + + ByteArray.erase (ByteArray.begin(), ByteArray.begin() + inst->byteSize()); + + unsigned numTokens = inst->numTokens(); + if ((int)numTokens < 0) { + errs() << "error: couldn't count the instruction's tokens\n"; + return -1; + } + + for (unsigned tokenIndex = 0; tokenIndex != numTokens; ++tokenIndex) { + EDToken *token; + + if (inst->getToken(token, tokenIndex)) { + errs() << "error: Couldn't get token\n"; + return -1; + } + + const char *buf; + if (token->getString(buf)) { + errs() << "error: Couldn't get string for token\n"; + return -1; + } + + Out << '['; + int operandIndex = token->operandID(); + + if (operandIndex >= 0) + Out << operandIndex << "-"; + + switch (token->type()) { + default: Out << "?"; break; + case EDToken::kTokenWhitespace: Out << "w"; break; + case EDToken::kTokenPunctuation: Out << "p"; break; + case EDToken::kTokenOpcode: Out << "o"; break; + case EDToken::kTokenLiteral: Out << "l"; break; + case EDToken::kTokenRegister: Out << "r"; break; + } + + Out << ":" << buf; + + if (token->type() == EDToken::kTokenLiteral) { + Out << "="; + if (token->literalSign()) + Out << "-"; + uint64_t absoluteValue; + if (token->literalAbsoluteValue(absoluteValue)) { + errs() << "error: Couldn't get the value of a literal token\n"; + return -1; + } + Out << absoluteValue; + } else if (token->type() == EDToken::kTokenRegister) { + Out << "="; + unsigned regID; + if (token->registerID(regID)) { + errs() << "error: Couldn't get the ID of a register token\n"; + return -1; + } + Out << "r" << regID; + } + + Out << "]"; + } + + Out << " "; + + if (inst->isBranch()) + Out << "
"; + if (inst->isMove()) + Out << " "; + + unsigned numOperands = inst->numOperands(); + + if ((int)numOperands < 0) { + errs() << "error: Couldn't count operands\n"; + return -1; + } + + for (unsigned operandIndex = 0; operandIndex != numOperands; + ++operandIndex) { + Out << operandIndex << ":"; + + EDOperand *operand; + if (inst->getOperand(operand, operandIndex)) { + errs() << "error: couldn't get operand\n"; + return -1; + } + + uint64_t evaluatedResult; + void *Arg[] = { disassembler.get(), &Out }; + if (operand->evaluate(evaluatedResult, verboseEvaluator, Arg)) { + errs() << "error: Couldn't evaluate an operand\n"; + return -1; + } + Out << "=" << evaluatedResult << " "; + } + + Out << '\n'; + } + + return 0; +} + diff --git a/contrib/llvm/tools/llvm-mc/Disassembler.h b/contrib/llvm/tools/llvm-mc/Disassembler.h new file mode 100644 index 000000000..e8cd92db0 --- /dev/null +++ b/contrib/llvm/tools/llvm-mc/Disassembler.h @@ -0,0 +1,42 @@ +//===- Disassembler.h - Text File Disassembler ----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class implements the disassembler of strings of bytes written in +// hexadecimal, from standard input or from a file. +// +//===----------------------------------------------------------------------===// + +#ifndef DISASSEMBLER_H +#define DISASSEMBLER_H + +#include + +namespace llvm { + +class MemoryBuffer; +class Target; +class raw_ostream; + +class Disassembler { +public: + static int disassemble(const Target &target, + const std::string &tripleString, + const std::string &Cpu, + const std::string &FeaturesStr, + MemoryBuffer &buffer, + raw_ostream &Out); + + static int disassembleEnhanced(const std::string &tripleString, + MemoryBuffer &buffer, + raw_ostream &Out); +}; + +} // namespace llvm + +#endif diff --git a/contrib/llvm/tools/llvm-mc/Makefile b/contrib/llvm/tools/llvm-mc/Makefile new file mode 100644 index 000000000..934a6e4dd --- /dev/null +++ b/contrib/llvm/tools/llvm-mc/Makefile @@ -0,0 +1,24 @@ +##===- tools/llvm-mc/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llvm-mc + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +# Include this here so we can get the configuration of the targets +# that have been configured for construction. We have to do this +# early so we can set up LINK_COMPONENTS before including Makefile.rules +include $(LEVEL)/Makefile.config + +LINK_COMPONENTS := $(TARGETS_TO_BUILD) MCDisassembler MCParser MC support + +include $(LLVM_SRC_ROOT)/Makefile.rules + diff --git a/contrib/llvm/tools/llvm-mc/llvm-mc.cpp b/contrib/llvm/tools/llvm-mc/llvm-mc.cpp new file mode 100644 index 000000000..5fb3fdf5b --- /dev/null +++ b/contrib/llvm/tools/llvm-mc/llvm-mc.cpp @@ -0,0 +1,517 @@ +//===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility is a simple driver that allows command line hacking on machine +// code. +// +//===----------------------------------------------------------------------===// + +#include "llvm/MC/MCParser/AsmLexer.h" +#include "llvm/MC/MCParser/MCAsmLexer.h" +#include "llvm/MC/MCAsmBackend.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCCodeEmitter.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/MCSectionMachO.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/MC/MCTargetAsmParser.h" +#include "llvm/MC/SubtargetFeature.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/system_error.h" +#include "Disassembler.h" +using namespace llvm; + +static cl::opt +InputFilename(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt +OutputFilename("o", cl::desc("Output filename"), + cl::value_desc("filename")); + +static cl::opt +ShowEncoding("show-encoding", cl::desc("Show instruction encodings")); + +static cl::opt +ShowInst("show-inst", cl::desc("Show internal instruction representation")); + +static cl::opt +ShowInstOperands("show-inst-operands", + cl::desc("Show instructions operands as parsed")); + +static cl::opt +OutputAsmVariant("output-asm-variant", + cl::desc("Syntax variant to use for output printing")); + +static cl::opt +RelaxAll("mc-relax-all", cl::desc("Relax all fixups")); + +static cl::opt +NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack")); + +static cl::opt +EnableLogging("enable-api-logging", cl::desc("Enable MC API logging")); + +enum OutputFileType { + OFT_Null, + OFT_AssemblyFile, + OFT_ObjectFile +}; +static cl::opt +FileType("filetype", cl::init(OFT_AssemblyFile), + cl::desc("Choose an output file type:"), + cl::values( + clEnumValN(OFT_AssemblyFile, "asm", + "Emit an assembly ('.s') file"), + clEnumValN(OFT_Null, "null", + "Don't emit anything (for timing purposes)"), + clEnumValN(OFT_ObjectFile, "obj", + "Emit a native object ('.o') file"), + clEnumValEnd)); + +static cl::list +IncludeDirs("I", cl::desc("Directory of include files"), + cl::value_desc("directory"), cl::Prefix); + +static cl::opt +ArchName("arch", cl::desc("Target arch to assemble for, " + "see -version for available targets")); + +static cl::opt +TripleName("triple", cl::desc("Target triple to assemble for, " + "see -version for available targets")); + +static cl::opt +MCPU("mcpu", + cl::desc("Target a specific cpu type (-mcpu=help for details)"), + cl::value_desc("cpu-name"), + cl::init("")); + +static cl::list +MAttrs("mattr", + cl::CommaSeparated, + cl::desc("Target specific attributes (-mattr=help for details)"), + cl::value_desc("a1,+a2,-a3,...")); + +static cl::opt +RelocModel("relocation-model", + cl::desc("Choose relocation model"), + cl::init(Reloc::Default), + cl::values( + clEnumValN(Reloc::Default, "default", + "Target default relocation model"), + clEnumValN(Reloc::Static, "static", + "Non-relocatable code"), + clEnumValN(Reloc::PIC_, "pic", + "Fully relocatable, position independent code"), + clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", + "Relocatable external references, non-relocatable code"), + clEnumValEnd)); + +static cl::opt +CMModel("code-model", + cl::desc("Choose code model"), + cl::init(CodeModel::Default), + cl::values(clEnumValN(CodeModel::Default, "default", + "Target default code model"), + clEnumValN(CodeModel::Small, "small", + "Small code model"), + clEnumValN(CodeModel::Kernel, "kernel", + "Kernel code model"), + clEnumValN(CodeModel::Medium, "medium", + "Medium code model"), + clEnumValN(CodeModel::Large, "large", + "Large code model"), + clEnumValEnd)); + +static cl::opt +NoInitialTextSection("n", cl::desc("Don't assume assembly file starts " + "in the text section")); + +static cl::opt +SaveTempLabels("L", cl::desc("Don't discard temporary labels")); + +enum ActionType { + AC_AsLex, + AC_Assemble, + AC_Disassemble, + AC_EDisassemble +}; + +static cl::opt +Action(cl::desc("Action to perform:"), + cl::init(AC_Assemble), + cl::values(clEnumValN(AC_AsLex, "as-lex", + "Lex tokens from a .s file"), + clEnumValN(AC_Assemble, "assemble", + "Assemble a .s file (default)"), + clEnumValN(AC_Disassemble, "disassemble", + "Disassemble strings of hex bytes"), + clEnumValN(AC_EDisassemble, "edis", + "Enhanced disassembly of strings of hex bytes"), + clEnumValEnd)); + +static const Target *GetTarget(const char *ProgName) { + // Figure out the target triple. + if (TripleName.empty()) + TripleName = sys::getHostTriple(); + Triple TheTriple(Triple::normalize(TripleName)); + + const Target *TheTarget = 0; + if (!ArchName.empty()) { + for (TargetRegistry::iterator it = TargetRegistry::begin(), + ie = TargetRegistry::end(); it != ie; ++it) { + if (ArchName == it->getName()) { + TheTarget = &*it; + break; + } + } + + if (!TheTarget) { + errs() << ProgName << ": error: invalid target '" << ArchName << "'.\n"; + return 0; + } + + // Adjust the triple to match (if known), otherwise stick with the + // module/host triple. + Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName); + if (Type != Triple::UnknownArch) + TheTriple.setArch(Type); + } else { + // Get the target specific parser. + std::string Error; + TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); + if (TheTarget == 0) { + errs() << ProgName << ": error: unable to get target for '" + << TheTriple.getTriple() + << "', see --version and --triple.\n"; + return 0; + } + } + + TripleName = TheTriple.getTriple(); + return TheTarget; +} + +static tool_output_file *GetOutputStream() { + if (OutputFilename == "") + OutputFilename = "-"; + + std::string Err; + tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err, + raw_fd_ostream::F_Binary); + if (!Err.empty()) { + errs() << Err << '\n'; + delete Out; + return 0; + } + + return Out; +} + +static int AsLexInput(const char *ProgName) { + OwningPtr BufferPtr; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) { + errs() << ProgName << ": " << ec.message() << '\n'; + return 1; + } + MemoryBuffer *Buffer = BufferPtr.take(); + + SourceMgr SrcMgr; + + // Tell SrcMgr about this buffer, which is what TGParser will pick up. + SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); + + // Record the location of the include directories so that the lexer can find + // it later. + SrcMgr.setIncludeDirs(IncludeDirs); + + const Target *TheTarget = GetTarget(ProgName); + if (!TheTarget) + return 1; + + llvm::OwningPtr MAI(TheTarget->createMCAsmInfo(TripleName)); + assert(MAI && "Unable to create target asm info!"); + + AsmLexer Lexer(*MAI); + Lexer.setBuffer(SrcMgr.getMemoryBuffer(0)); + + OwningPtr Out(GetOutputStream()); + if (!Out) + return 1; + + bool Error = false; + while (Lexer.Lex().isNot(AsmToken::Eof)) { + AsmToken Tok = Lexer.getTok(); + + switch (Tok.getKind()) { + default: + SrcMgr.PrintMessage(Lexer.getLoc(), "unknown token", "warning"); + Error = true; + break; + case AsmToken::Error: + Error = true; // error already printed. + break; + case AsmToken::Identifier: + Out->os() << "identifier: " << Lexer.getTok().getString(); + break; + case AsmToken::Integer: + Out->os() << "int: " << Lexer.getTok().getString(); + break; + case AsmToken::Real: + Out->os() << "real: " << Lexer.getTok().getString(); + break; + case AsmToken::Register: + Out->os() << "register: " << Lexer.getTok().getRegVal(); + break; + case AsmToken::String: + Out->os() << "string: " << Lexer.getTok().getString(); + break; + + case AsmToken::Amp: Out->os() << "Amp"; break; + case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break; + case AsmToken::At: Out->os() << "At"; break; + case AsmToken::Caret: Out->os() << "Caret"; break; + case AsmToken::Colon: Out->os() << "Colon"; break; + case AsmToken::Comma: Out->os() << "Comma"; break; + case AsmToken::Dollar: Out->os() << "Dollar"; break; + case AsmToken::Dot: Out->os() << "Dot"; break; + case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break; + case AsmToken::Eof: Out->os() << "Eof"; break; + case AsmToken::Equal: Out->os() << "Equal"; break; + case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break; + case AsmToken::Exclaim: Out->os() << "Exclaim"; break; + case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break; + case AsmToken::Greater: Out->os() << "Greater"; break; + case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break; + case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break; + case AsmToken::Hash: Out->os() << "Hash"; break; + case AsmToken::LBrac: Out->os() << "LBrac"; break; + case AsmToken::LCurly: Out->os() << "LCurly"; break; + case AsmToken::LParen: Out->os() << "LParen"; break; + case AsmToken::Less: Out->os() << "Less"; break; + case AsmToken::LessEqual: Out->os() << "LessEqual"; break; + case AsmToken::LessGreater: Out->os() << "LessGreater"; break; + case AsmToken::LessLess: Out->os() << "LessLess"; break; + case AsmToken::Minus: Out->os() << "Minus"; break; + case AsmToken::Percent: Out->os() << "Percent"; break; + case AsmToken::Pipe: Out->os() << "Pipe"; break; + case AsmToken::PipePipe: Out->os() << "PipePipe"; break; + case AsmToken::Plus: Out->os() << "Plus"; break; + case AsmToken::RBrac: Out->os() << "RBrac"; break; + case AsmToken::RCurly: Out->os() << "RCurly"; break; + case AsmToken::RParen: Out->os() << "RParen"; break; + case AsmToken::Slash: Out->os() << "Slash"; break; + case AsmToken::Star: Out->os() << "Star"; break; + case AsmToken::Tilde: Out->os() << "Tilde"; break; + } + + // Print the token string. + Out->os() << " (\""; + Out->os().write_escaped(Tok.getString()); + Out->os() << "\")\n"; + } + + // Keep output if no errors. + if (Error == 0) Out->keep(); + + return Error; +} + +static int AssembleInput(const char *ProgName) { + const Target *TheTarget = GetTarget(ProgName); + if (!TheTarget) + return 1; + + OwningPtr BufferPtr; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) { + errs() << ProgName << ": " << ec.message() << '\n'; + return 1; + } + MemoryBuffer *Buffer = BufferPtr.take(); + + SourceMgr SrcMgr; + + // Tell SrcMgr about this buffer, which is what the parser will pick up. + SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); + + // Record the location of the include directories so that the lexer can find + // it later. + SrcMgr.setIncludeDirs(IncludeDirs); + + + llvm::OwningPtr MAI(TheTarget->createMCAsmInfo(TripleName)); + assert(MAI && "Unable to create target asm info!"); + + llvm::OwningPtr MRI(TheTarget->createMCRegInfo(TripleName)); + assert(MRI && "Unable to create target register info!"); + + // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and + // MCObjectFileInfo needs a MCContext reference in order to initialize itself. + OwningPtr MOFI(new MCObjectFileInfo()); + MCContext Ctx(*MAI, *MRI, MOFI.get()); + MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx); + + if (SaveTempLabels) + Ctx.setAllowTemporaryLabels(false); + + // Package up features to be passed to target/subtarget + std::string FeaturesStr; + if (MAttrs.size()) { + SubtargetFeatures Features; + for (unsigned i = 0; i != MAttrs.size(); ++i) + Features.AddFeature(MAttrs[i]); + FeaturesStr = Features.getString(); + } + + OwningPtr Out(GetOutputStream()); + if (!Out) + return 1; + + formatted_raw_ostream FOS(Out->os()); + OwningPtr Str; + + OwningPtr MCII(TheTarget->createMCInstrInfo()); + OwningPtr + STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); + + // FIXME: There is a bit of code duplication with addPassesToEmitFile. + if (FileType == OFT_AssemblyFile) { + MCInstPrinter *IP = + TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *STI); + MCCodeEmitter *CE = 0; + MCAsmBackend *MAB = 0; + if (ShowEncoding) { + CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx); + MAB = TheTarget->createMCAsmBackend(TripleName); + } + Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true, + /*useLoc*/ true, + /*useCFI*/ true, IP, CE, MAB, + ShowInst)); + } else if (FileType == OFT_Null) { + Str.reset(createNullStreamer(Ctx)); + } else { + assert(FileType == OFT_ObjectFile && "Invalid file type!"); + MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx); + MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName); + Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB, + FOS, CE, RelaxAll, + NoExecStack)); + } + + if (EnableLogging) { + Str.reset(createLoggingStreamer(Str.take(), errs())); + } + + OwningPtr Parser(createMCAsmParser(SrcMgr, Ctx, + *Str.get(), *MAI)); + OwningPtr TAP(TheTarget->createMCAsmParser(*STI, *Parser)); + if (!TAP) { + errs() << ProgName + << ": error: this target does not support assembly parsing.\n"; + return 1; + } + + Parser->setShowParsedOperands(ShowInstOperands); + Parser->setTargetParser(*TAP.get()); + + int Res = Parser->Run(NoInitialTextSection); + + // Keep output if no errors. + if (Res == 0) Out->keep(); + + return Res; +} + +static int DisassembleInput(const char *ProgName, bool Enhanced) { + const Target *TheTarget = GetTarget(ProgName); + if (!TheTarget) + return 0; + + OwningPtr Buffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) { + errs() << ProgName << ": " << ec.message() << '\n'; + return 1; + } + + OwningPtr Out(GetOutputStream()); + if (!Out) + return 1; + + int Res; + if (Enhanced) { + Res = + Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os()); + } else { + // Package up features to be passed to target/subtarget + std::string FeaturesStr; + if (MAttrs.size()) { + SubtargetFeatures Features; + for (unsigned i = 0; i != MAttrs.size(); ++i) + Features.AddFeature(MAttrs[i]); + FeaturesStr = Features.getString(); + } + + Res = Disassembler::disassemble(*TheTarget, TripleName, MCPU, FeaturesStr, + *Buffer.take(), Out->os()); + } + + // Keep output if no errors. + if (Res == 0) Out->keep(); + + return Res; +} + + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + // Initialize targets and assembly printers/parsers. + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmParsers(); + llvm::InitializeAllDisassemblers(); + + cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n"); + TripleName = Triple::normalize(TripleName); + + switch (Action) { + default: + case AC_AsLex: + return AsLexInput(argv[0]); + case AC_Assemble: + return AssembleInput(argv[0]); + case AC_Disassemble: + return DisassembleInput(argv[0], false); + case AC_EDisassemble: + return DisassembleInput(argv[0], true); + } + + return 0; +} + diff --git a/contrib/llvm/tools/llvm-nm/CMakeLists.txt b/contrib/llvm/tools/llvm-nm/CMakeLists.txt new file mode 100644 index 000000000..b6cd80b47 --- /dev/null +++ b/contrib/llvm/tools/llvm-nm/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS archive bitreader object) + +add_llvm_tool(llvm-nm + llvm-nm.cpp + ) diff --git a/contrib/llvm/tools/llvm-nm/Makefile b/contrib/llvm/tools/llvm-nm/Makefile new file mode 100644 index 000000000..6bb4cd4ac --- /dev/null +++ b/contrib/llvm/tools/llvm-nm/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-nm/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-nm +LINK_COMPONENTS = archive bitreader object + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-nm/llvm-nm.cpp b/contrib/llvm/tools/llvm-nm/llvm-nm.cpp new file mode 100644 index 000000000..e79d72d19 --- /dev/null +++ b/contrib/llvm/tools/llvm-nm/llvm-nm.cpp @@ -0,0 +1,392 @@ +//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is a utility that works like traditional Unix "nm", +// that is, it prints out the names of symbols in a bitcode file, +// along with some information about each symbol. +// +// This "nm" does not print symbols' addresses. It supports many of +// the features of GNU "nm", including its different output formats. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Bitcode/Archive.h" +#include "llvm/Object/Archive.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/system_error.h" +#include +#include +#include +#include +#include +using namespace llvm; +using namespace object; + +namespace { + enum OutputFormatTy { bsd, sysv, posix }; + cl::opt + OutputFormat("format", + cl::desc("Specify output format"), + cl::values(clEnumVal(bsd, "BSD format"), + clEnumVal(sysv, "System V format"), + clEnumVal(posix, "POSIX.2 format"), + clEnumValEnd), cl::init(bsd)); + cl::alias OutputFormat2("f", cl::desc("Alias for --format"), + cl::aliasopt(OutputFormat)); + + cl::list + InputFilenames(cl::Positional, cl::desc(""), + cl::ZeroOrMore); + + cl::opt UndefinedOnly("undefined-only", + cl::desc("Show only undefined symbols")); + cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"), + cl::aliasopt(UndefinedOnly)); + + cl::opt DefinedOnly("defined-only", + cl::desc("Show only defined symbols")); + + cl::opt ExternalOnly("extern-only", + cl::desc("Show only external symbols")); + cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"), + cl::aliasopt(ExternalOnly)); + + cl::opt BSDFormat("B", cl::desc("Alias for --format=bsd")); + cl::opt POSIXFormat("P", cl::desc("Alias for --format=posix")); + + cl::opt PrintFileName("print-file-name", + cl::desc("Precede each symbol with the object file it came from")); + + cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"), + cl::aliasopt(PrintFileName)); + cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"), + cl::aliasopt(PrintFileName)); + + cl::opt DebugSyms("debug-syms", + cl::desc("Show all symbols, even debugger only")); + cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"), + cl::aliasopt(DebugSyms)); + + cl::opt NumericSort("numeric-sort", + cl::desc("Sort symbols by address")); + cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"), + cl::aliasopt(NumericSort)); + cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"), + cl::aliasopt(NumericSort)); + + cl::opt NoSort("no-sort", + cl::desc("Show symbols in order encountered")); + cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), + cl::aliasopt(NoSort)); + + cl::opt PrintSize("print-size", + cl::desc("Show symbol size instead of address")); + cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"), + cl::aliasopt(PrintSize)); + + cl::opt SizeSort("size-sort", cl::desc("Sort symbols by size")); + + bool PrintAddress = true; + + bool MultipleFiles = false; + + std::string ToolName; +} + +namespace { + struct NMSymbol { + uint64_t Address; + uint64_t Size; + char TypeChar; + StringRef Name; + }; + + static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) { + if (a.Address < b.Address) + return true; + else if (a.Address == b.Address && a.Name < b.Name) + return true; + else + return false; + + } + + static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) { + if (a.Size < b.Size) + return true; + else if (a.Size == b.Size && a.Name < b.Name) + return true; + else + return false; + } + + static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) { + return a.Name < b.Name; + } + + StringRef CurrentFilename; + typedef std::vector SymbolListT; + SymbolListT SymbolList; + + bool error(error_code ec) { + if (!ec) return false; + + outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; + outs().flush(); + return true; + } +} + +static void SortAndPrintSymbolList() { + if (!NoSort) { + if (NumericSort) + std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress); + else if (SizeSort) + std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize); + else + std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName); + } + + if (OutputFormat == posix && MultipleFiles) { + outs() << '\n' << CurrentFilename << ":\n"; + } else if (OutputFormat == bsd && MultipleFiles) { + outs() << "\n" << CurrentFilename << ":\n"; + } else if (OutputFormat == sysv) { + outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n" + << "Name Value Class Type" + << " Size Line Section\n"; + } + + for (SymbolListT::iterator i = SymbolList.begin(), + e = SymbolList.end(); i != e; ++i) { + if ((i->TypeChar != 'U') && UndefinedOnly) + continue; + if ((i->TypeChar == 'U') && DefinedOnly) + continue; + if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize) + continue; + + char SymbolAddrStr[10] = ""; + char SymbolSizeStr[10] = ""; + + if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize) + strcpy(SymbolAddrStr, " "); + if (OutputFormat == sysv) + strcpy(SymbolSizeStr, " "); + + if (i->Address != object::UnknownAddressOrSize) + format("%08"PRIx64, i->Address).print(SymbolAddrStr, sizeof(SymbolAddrStr)); + if (i->Size != object::UnknownAddressOrSize) + format("%08"PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr)); + + if (OutputFormat == posix) { + outs() << i->Name << " " << i->TypeChar << " " + << SymbolAddrStr << SymbolSizeStr << "\n"; + } else if (OutputFormat == bsd) { + if (PrintAddress) + outs() << SymbolAddrStr << ' '; + if (PrintSize) { + outs() << SymbolSizeStr; + if (i->Size != object::UnknownAddressOrSize) + outs() << ' '; + } + outs() << i->TypeChar << " " << i->Name << "\n"; + } else if (OutputFormat == sysv) { + std::string PaddedName (i->Name); + while (PaddedName.length () < 20) + PaddedName += " "; + outs() << PaddedName << "|" << SymbolAddrStr << "| " + << i->TypeChar + << " | |" << SymbolSizeStr << "| |\n"; + } + } + + SymbolList.clear(); +} + +static char TypeCharForSymbol(GlobalValue &GV) { + if (GV.isDeclaration()) return 'U'; + if (GV.hasLinkOnceLinkage()) return 'C'; + if (GV.hasCommonLinkage()) return 'C'; + if (GV.hasWeakLinkage()) return 'W'; + if (isa(GV) && GV.hasInternalLinkage()) return 't'; + if (isa(GV)) return 'T'; + if (isa(GV) && GV.hasInternalLinkage()) return 'd'; + if (isa(GV)) return 'D'; + if (const GlobalAlias *GA = dyn_cast(&GV)) { + const GlobalValue *AliasedGV = GA->getAliasedGlobal(); + if (isa(AliasedGV)) return 'T'; + if (isa(AliasedGV)) return 'D'; + } + return '?'; +} + +static void DumpSymbolNameForGlobalValue(GlobalValue &GV) { + // Private linkage and available_externally linkage don't exist in symtab. + if (GV.hasPrivateLinkage() || + GV.hasLinkerPrivateLinkage() || + GV.hasLinkerPrivateWeakLinkage() || + GV.hasLinkerPrivateWeakDefAutoLinkage() || + GV.hasAvailableExternallyLinkage()) + return; + char TypeChar = TypeCharForSymbol(GV); + if (GV.hasLocalLinkage () && ExternalOnly) + return; + + NMSymbol s; + s.Address = object::UnknownAddressOrSize; + s.Size = object::UnknownAddressOrSize; + s.TypeChar = TypeChar; + s.Name = GV.getName(); + SymbolList.push_back(s); +} + +static void DumpSymbolNamesFromModule(Module *M) { + CurrentFilename = M->getModuleIdentifier(); + std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue); + std::for_each (M->global_begin(), M->global_end(), + DumpSymbolNameForGlobalValue); + std::for_each (M->alias_begin(), M->alias_end(), + DumpSymbolNameForGlobalValue); + + SortAndPrintSymbolList(); +} + +static void DumpSymbolNamesFromObject(ObjectFile *obj) { + error_code ec; + for (symbol_iterator i = obj->begin_symbols(), + e = obj->end_symbols(); + i != e; i.increment(ec)) { + if (error(ec)) break; + bool internal; + if (error(i->isInternal(internal))) break; + if (!DebugSyms && internal) + continue; + NMSymbol s; + s.Size = object::UnknownAddressOrSize; + s.Address = object::UnknownAddressOrSize; + if (PrintSize || SizeSort) { + if (error(i->getSize(s.Size))) break; + } + if (PrintAddress) + if (error(i->getOffset(s.Address))) break; + if (error(i->getNMTypeChar(s.TypeChar))) break; + if (error(i->getName(s.Name))) break; + SymbolList.push_back(s); + } + + CurrentFilename = obj->getFileName(); + SortAndPrintSymbolList(); +} + +static void DumpSymbolNamesFromFile(std::string &Filename) { + LLVMContext &Context = getGlobalContext(); + std::string ErrorMessage; + sys::Path aPath(Filename); + bool exists; + if (sys::fs::exists(aPath.str(), exists) || !exists) + errs() << ToolName << ": '" << Filename << "': " << "No such file\n"; + // Note: Currently we do not support reading an archive from stdin. + if (Filename == "-" || aPath.isBitcodeFile()) { + OwningPtr Buffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) + ErrorMessage = ec.message(); + Module *Result = 0; + if (Buffer.get()) + Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); + + if (Result) { + DumpSymbolNamesFromModule(Result); + delete Result; + } else + errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n"; + + } else if (aPath.isArchive()) { + OwningPtr arch; + if (error_code ec = object::createBinary(aPath.str(), arch)) { + errs() << ToolName << ": " << Filename << ": " << ec.message() << ".\n"; + return; + } + if (object::Archive *a = dyn_cast(arch.get())) { + for (object::Archive::child_iterator i = a->begin_children(), + e = a->end_children(); i != e; ++i) { + OwningPtr child; + if (error_code ec = i->getAsBinary(child)) { + // Try opening it as a bitcode file. + OwningPtr buff(i->getBuffer()); + Module *Result = 0; + if (buff) + Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage); + + if (Result) { + DumpSymbolNamesFromModule(Result); + delete Result; + } + continue; + } + if (object::ObjectFile *o = dyn_cast(child.get())) { + outs() << o->getFileName() << ":\n"; + DumpSymbolNamesFromObject(o); + } + } + } + } else if (aPath.isObjectFile()) { + OwningPtr obj; + if (error_code ec = object::createBinary(aPath.str(), obj)) { + errs() << ToolName << ": " << Filename << ": " << ec.message() << ".\n"; + return; + } + if (object::ObjectFile *o = dyn_cast(obj.get())) + DumpSymbolNamesFromObject(o); + } else { + errs() << ToolName << ": " << Filename << ": " + << "unrecognizable file type\n"; + return; + } +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n"); + + ToolName = argv[0]; + if (BSDFormat) OutputFormat = bsd; + if (POSIXFormat) OutputFormat = posix; + + // The relative order of these is important. If you pass --size-sort it should + // only print out the size. However, if you pass -S --size-sort, it should + // print out both the size and address. + if (SizeSort && !PrintSize) PrintAddress = false; + if (OutputFormat == sysv || SizeSort) PrintSize = true; + + switch (InputFilenames.size()) { + case 0: InputFilenames.push_back("-"); + case 1: break; + default: MultipleFiles = true; + } + + std::for_each(InputFilenames.begin(), InputFilenames.end(), + DumpSymbolNamesFromFile); + return 0; +} diff --git a/contrib/llvm/tools/llvm-objdump/CMakeLists.txt b/contrib/llvm/tools/llvm-objdump/CMakeLists.txt new file mode 100644 index 000000000..f3b2e1fe4 --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/CMakeLists.txt @@ -0,0 +1,14 @@ +set(LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + DebugInfo + MC + MCParser + MCDisassembler + Object + ) + +add_llvm_tool(llvm-objdump + llvm-objdump.cpp + MachODump.cpp + MCFunction.cpp + ) diff --git a/contrib/llvm/tools/llvm-objdump/MCFunction.cpp b/contrib/llvm/tools/llvm-objdump/MCFunction.cpp new file mode 100644 index 000000000..5c67f1b70 --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/MCFunction.cpp @@ -0,0 +1,138 @@ +//===-- MCFunction.cpp ----------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the algorithm to break down a region of machine code +// into basic blocks and try to reconstruct a CFG from it. +// +//===----------------------------------------------------------------------===// + +#include "MCFunction.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/MC/MCDisassembler.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrAnalysis.h" +#include "llvm/MC/MCInstrDesc.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/Support/MemoryObject.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" +#include +using namespace llvm; + +MCFunction +MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, + const MemoryObject &Region, uint64_t Start, + uint64_t End, const MCInstrAnalysis *Ana, + raw_ostream &DebugOut, + SmallVectorImpl &Calls) { + std::vector Instructions; + std::set Splits; + Splits.insert(Start); + uint64_t Size; + + MCFunction f(Name); + + { + DenseSet VisitedInsts; + SmallVector WorkList; + WorkList.push_back(Start); + // Disassemble code and gather basic block split points. + while (!WorkList.empty()) { + uint64_t Index = WorkList.pop_back_val(); + if (VisitedInsts.find(Index) != VisitedInsts.end()) + continue; // Already visited this location. + + for (;Index < End; Index += Size) { + VisitedInsts.insert(Index); + + MCInst Inst; + if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut, nulls())){ + Instructions.push_back(MCDecodedInst(Index, Size, Inst)); + if (Ana->isBranch(Inst)) { + uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); + if (targ != -1ULL && targ == Index+Size) + continue; // Skip nop jumps. + + // If we could determine the branch target, make a note to start a + // new basic block there and add the target to the worklist. + if (targ != -1ULL) { + Splits.insert(targ); + WorkList.push_back(targ); + WorkList.push_back(Index+Size); + } + Splits.insert(Index+Size); + break; + } else if (Ana->isReturn(Inst)) { + // Return instruction. This basic block ends here. + Splits.insert(Index+Size); + break; + } else if (Ana->isCall(Inst)) { + uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); + // Add the call to the call list if the destination is known. + if (targ != -1ULL && targ != Index+Size) + Calls.push_back(targ); + } + } else { + errs().write_hex(Index) << ": warning: invalid instruction encoding\n"; + if (Size == 0) + Size = 1; // skip illegible bytes + } + } + } + } + + // Make sure the instruction list is sorted. + std::sort(Instructions.begin(), Instructions.end()); + + // Create basic blocks. + unsigned ii = 0, ie = Instructions.size(); + for (std::set::iterator spi = Splits.begin(), + spe = llvm::prior(Splits.end()); spi != spe; ++spi) { + MCBasicBlock BB; + uint64_t BlockEnd = *llvm::next(spi); + // Add instructions to the BB. + for (; ii != ie; ++ii) { + if (Instructions[ii].Address < *spi || + Instructions[ii].Address >= BlockEnd) + break; + BB.addInst(Instructions[ii]); + } + f.addBlock(*spi, BB); + } + + std::sort(f.Blocks.begin(), f.Blocks.end()); + + // Calculate successors of each block. + for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) { + MCBasicBlock &BB = const_cast(i->second); + if (BB.getInsts().empty()) continue; + const MCDecodedInst &Inst = BB.getInsts().back(); + + if (Ana->isBranch(Inst.Inst)) { + uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size); + if (targ == -1ULL) { + // Indirect branch. Bail and add all blocks of the function as a + // successor. + for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) + BB.addSucc(i->first); + } else if (targ != Inst.Address+Inst.Size) + BB.addSucc(targ); + // Conditional branches can also fall through to the next block. + if (Ana->isConditionalBranch(Inst.Inst) && llvm::next(i) != e) + BB.addSucc(llvm::next(i)->first); + } else { + // No branch. Fall through to the next block. + if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e) + BB.addSucc(llvm::next(i)->first); + } + } + + return f; +} diff --git a/contrib/llvm/tools/llvm-objdump/MCFunction.h b/contrib/llvm/tools/llvm-objdump/MCFunction.h new file mode 100644 index 000000000..6d3a548d4 --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/MCFunction.h @@ -0,0 +1,100 @@ +//===-- MCFunction.h ------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the data structures to hold a CFG reconstructed from +// machine code. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTDUMP_MCFUNCTION_H +#define LLVM_OBJECTDUMP_MCFUNCTION_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/MC/MCInst.h" +#include + +namespace llvm { + +class MCDisassembler; +class MCInstrAnalysis; +class MemoryObject; +class raw_ostream; + +/// MCDecodedInst - Small container to hold an MCInst and associated info like +/// address and size. +struct MCDecodedInst { + uint64_t Address; + uint64_t Size; + MCInst Inst; + + MCDecodedInst() {} + MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst) + : Address(Address), Size(Size), Inst(Inst) {} + + bool operator<(const MCDecodedInst &RHS) const { + return Address < RHS.Address; + } +}; + +/// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing +/// MCBasicBlocks. +class MCBasicBlock { + std::vector Insts; + typedef DenseSet SetTy; + SetTy Succs; +public: + ArrayRef getInsts() const { return Insts; } + + typedef SetTy::const_iterator succ_iterator; + succ_iterator succ_begin() const { return Succs.begin(); } + succ_iterator succ_end() const { return Succs.end(); } + + bool contains(uint64_t Addr) const { return Succs.count(Addr); } + + void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); } + void addSucc(uint64_t Addr) { Succs.insert(Addr); } + + bool operator<(const MCBasicBlock &RHS) const { + return Insts.size() < RHS.Insts.size(); + } +}; + +/// MCFunction - Represents a named function in machine code, containing +/// multiple MCBasicBlocks. +class MCFunction { + const StringRef Name; + // Keep BBs sorted by address. + typedef std::vector > MapTy; + MapTy Blocks; +public: + MCFunction(StringRef Name) : Name(Name) {} + + // Create an MCFunction from a region of binary machine code. + static MCFunction + createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, + const MemoryObject &Region, uint64_t Start, uint64_t End, + const MCInstrAnalysis *Ana, raw_ostream &DebugOut, + SmallVectorImpl &Calls); + + typedef MapTy::const_iterator iterator; + iterator begin() const { return Blocks.begin(); } + iterator end() const { return Blocks.end(); } + + StringRef getName() const { return Name; } + + MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) { + Blocks.push_back(std::make_pair(Address, BB)); + return Blocks.back().second; + } +}; + +} + +#endif diff --git a/contrib/llvm/tools/llvm-objdump/MachODump.cpp b/contrib/llvm/tools/llvm-objdump/MachODump.cpp new file mode 100644 index 000000000..3f44b295d --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/MachODump.cpp @@ -0,0 +1,617 @@ +//===-- MachODump.cpp - Object file dumping utility for llvm --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the MachO-specific dumper for llvm-objdump. +// +//===----------------------------------------------------------------------===// + +#include "llvm-objdump.h" +#include "MCFunction.h" +#include "llvm/Support/MachO.h" +#include "llvm/Object/MachOObject.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/Triple.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/DebugInfo/DIContext.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCDisassembler.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrAnalysis.h" +#include "llvm/MC/MCInstrDesc.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/GraphWriter.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" +#include +#include +using namespace llvm; +using namespace object; + +static cl::opt + CFG("cfg", cl::desc("Create a CFG for every symbol in the object file and" + "write it to a graphviz file (MachO-only)")); + +static cl::opt + UseDbg("g", cl::desc("Print line information from debug info if available")); + +static cl::opt + DSYMFile("dsym", cl::desc("Use .dSYM file for debug info")); + +static const Target *GetTarget(const MachOObject *MachOObj) { + // Figure out the target triple. + llvm::Triple TT("unknown-unknown-unknown"); + switch (MachOObj->getHeader().CPUType) { + case llvm::MachO::CPUTypeI386: + TT.setArch(Triple::ArchType(Triple::x86)); + break; + case llvm::MachO::CPUTypeX86_64: + TT.setArch(Triple::ArchType(Triple::x86_64)); + break; + case llvm::MachO::CPUTypeARM: + TT.setArch(Triple::ArchType(Triple::arm)); + break; + case llvm::MachO::CPUTypePowerPC: + TT.setArch(Triple::ArchType(Triple::ppc)); + break; + case llvm::MachO::CPUTypePowerPC64: + TT.setArch(Triple::ArchType(Triple::ppc64)); + break; + } + + TripleName = TT.str(); + + // Get the target specific parser. + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); + if (TheTarget) + return TheTarget; + + errs() << "llvm-objdump: error: unable to get target for '" << TripleName + << "', see --version and --triple.\n"; + return 0; +} + +struct Section { + char Name[16]; + uint64_t Address; + uint64_t Size; + uint32_t Offset; + uint32_t NumRelocs; + uint64_t RelocTableOffset; +}; + +struct Symbol { + uint64_t Value; + uint32_t StringIndex; + uint8_t SectionIndex; + bool operator<(const Symbol &RHS) const { return Value < RHS.Value; } +}; + +template +static Section copySection(const T &Sect) { + Section S; + memcpy(S.Name, Sect->Name, 16); + S.Address = Sect->Address; + S.Size = Sect->Size; + S.Offset = Sect->Offset; + S.NumRelocs = Sect->NumRelocationTableEntries; + S.RelocTableOffset = Sect->RelocationTableOffset; + return S; +} + +template +static Symbol copySymbol(const T &STE) { + Symbol S; + S.StringIndex = STE->StringIndex; + S.SectionIndex = STE->SectionIndex; + S.Value = STE->Value; + return S; +} + +// Print additional information about an address, if available. +static void DumpAddress(uint64_t Address, ArrayRef
Sections, + MachOObject *MachOObj, raw_ostream &OS) { + for (unsigned i = 0; i != Sections.size(); ++i) { + uint64_t addr = Address-Sections[i].Address; + if (Sections[i].Address <= Address && + Sections[i].Address + Sections[i].Size > Address) { + StringRef bytes = MachOObj->getData(Sections[i].Offset, + Sections[i].Size); + // Print constant strings. + if (!strcmp(Sections[i].Name, "__cstring")) + OS << '"' << bytes.substr(addr, bytes.find('\0', addr)) << '"'; + // Print constant CFStrings. + if (!strcmp(Sections[i].Name, "__cfstring")) + OS << "@\"" << bytes.substr(addr, bytes.find('\0', addr)) << '"'; + } + } +} + +typedef std::map FunctionMapTy; +typedef SmallVector FunctionListTy; +static void createMCFunctionAndSaveCalls(StringRef Name, + const MCDisassembler *DisAsm, + MemoryObject &Object, uint64_t Start, + uint64_t End, + MCInstrAnalysis *InstrAnalysis, + uint64_t Address, + raw_ostream &DebugOut, + FunctionMapTy &FunctionMap, + FunctionListTy &Functions) { + SmallVector Calls; + MCFunction f = + MCFunction::createFunctionFromMC(Name, DisAsm, Object, Start, End, + InstrAnalysis, DebugOut, Calls); + Functions.push_back(f); + FunctionMap[Address] = &Functions.back(); + + // Add the gathered callees to the map. + for (unsigned i = 0, e = Calls.size(); i != e; ++i) + FunctionMap.insert(std::make_pair(Calls[i], (MCFunction*)0)); +} + +// Write a graphviz file for the CFG inside an MCFunction. +static void emitDOTFile(const char *FileName, const MCFunction &f, + MCInstPrinter *IP) { + // Start a new dot file. + std::string Error; + raw_fd_ostream Out(FileName, Error); + if (!Error.empty()) { + errs() << "llvm-objdump: warning: " << Error << '\n'; + return; + } + + Out << "digraph " << f.getName() << " {\n"; + Out << "graph [ rankdir = \"LR\" ];\n"; + for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) { + bool hasPreds = false; + // Only print blocks that have predecessors. + // FIXME: Slow. + for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe; + ++pi) + if (pi->second.contains(i->first)) { + hasPreds = true; + break; + } + + if (!hasPreds && i != f.begin()) + continue; + + Out << '"' << i->first << "\" [ label=\""; + // Print instructions. + for (unsigned ii = 0, ie = i->second.getInsts().size(); ii != ie; + ++ii) { + // Escape special chars and print the instruction in mnemonic form. + std::string Str; + raw_string_ostream OS(Str); + IP->printInst(&i->second.getInsts()[ii].Inst, OS, ""); + Out << DOT::EscapeString(OS.str()) << '|'; + } + Out << "\" shape=\"record\" ];\n"; + + // Add edges. + for (MCBasicBlock::succ_iterator si = i->second.succ_begin(), + se = i->second.succ_end(); si != se; ++si) + Out << i->first << ":o -> " << *si <<":a\n"; + } + Out << "}\n"; +} + +static void getSectionsAndSymbols(const macho::Header &Header, + MachOObject *MachOObj, + InMemoryStruct *SymtabLC, + std::vector
&Sections, + std::vector &Symbols, + SmallVectorImpl &FoundFns) { + // Make a list of all symbols in the object file. + for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { + const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i); + if (LCI.Command.Type == macho::LCT_Segment) { + InMemoryStruct SegmentLC; + MachOObj->ReadSegmentLoadCommand(LCI, SegmentLC); + + // Store the sections in this segment. + for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { + InMemoryStruct Sect; + MachOObj->ReadSection(LCI, SectNum, Sect); + Sections.push_back(copySection(Sect)); + + } + } else if (LCI.Command.Type == macho::LCT_Segment64) { + InMemoryStruct Segment64LC; + MachOObj->ReadSegment64LoadCommand(LCI, Segment64LC); + + // Store the sections in this segment. + for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; + ++SectNum) { + InMemoryStruct Sect64; + MachOObj->ReadSection64(LCI, SectNum, Sect64); + Sections.push_back(copySection(Sect64)); + } + } else if (LCI.Command.Type == macho::LCT_FunctionStarts) { + // We found a function starts segment, parse the addresses for later + // consumption. + InMemoryStruct LLC; + MachOObj->ReadLinkeditDataLoadCommand(LCI, LLC); + + MachOObj->ReadULEB128s(LLC->DataOffset, FoundFns); + } + } + // Store the symbols. + if (SymtabLC) { + for (unsigned i = 0; i != (*SymtabLC)->NumSymbolTableEntries; ++i) { + if (MachOObj->is64Bit()) { + InMemoryStruct STE; + MachOObj->ReadSymbol64TableEntry((*SymtabLC)->SymbolTableOffset, i, + STE); + Symbols.push_back(copySymbol(STE)); + } else { + InMemoryStruct STE; + MachOObj->ReadSymbolTableEntry((*SymtabLC)->SymbolTableOffset, i, + STE); + Symbols.push_back(copySymbol(STE)); + } + } + } +} + +void llvm::DisassembleInputMachO(StringRef Filename) { + OwningPtr Buff; + + if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { + errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n"; + return; + } + + OwningPtr MachOObj(MachOObject::LoadFromBuffer(Buff.take())); + + const Target *TheTarget = GetTarget(MachOObj.get()); + if (!TheTarget) { + // GetTarget prints out stuff. + return; + } + OwningPtr InstrInfo(TheTarget->createMCInstrInfo()); + OwningPtr + InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo.get())); + + // Set up disassembler. + OwningPtr AsmInfo(TheTarget->createMCAsmInfo(TripleName)); + OwningPtr + STI(TheTarget->createMCSubtargetInfo(TripleName, "", "")); + OwningPtr DisAsm(TheTarget->createMCDisassembler(*STI)); + int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); + OwningPtr IP(TheTarget->createMCInstPrinter( + AsmPrinterVariant, *AsmInfo, *STI)); + + if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) { + errs() << "error: couldn't initialize disassembler for target " + << TripleName << '\n'; + return; + } + + outs() << '\n' << Filename << ":\n\n"; + + const macho::Header &Header = MachOObj->getHeader(); + + const MachOObject::LoadCommandInfo *SymtabLCI = 0; + // First, find the symbol table segment. + for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { + const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i); + if (LCI.Command.Type == macho::LCT_Symtab) { + SymtabLCI = &LCI; + break; + } + } + + // Read and register the symbol table data. + InMemoryStruct SymtabLC; + MachOObj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC); + MachOObj->RegisterStringTable(*SymtabLC); + + std::vector
Sections; + std::vector Symbols; + SmallVector FoundFns; + + getSectionsAndSymbols(Header, MachOObj.get(), &SymtabLC, Sections, Symbols, + FoundFns); + + // Make a copy of the unsorted symbol list. FIXME: duplication + std::vector UnsortedSymbols(Symbols); + // Sort the symbols by address, just in case they didn't come in that way. + array_pod_sort(Symbols.begin(), Symbols.end()); + +#ifndef NDEBUG + raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); +#else + raw_ostream &DebugOut = nulls(); +#endif + + StringRef DebugAbbrevSection, DebugInfoSection, DebugArangesSection, + DebugLineSection, DebugStrSection; + OwningPtr diContext; + OwningPtr DSYMObj; + MachOObject *DbgInfoObj = MachOObj.get(); + // Try to find debug info and set up the DIContext for it. + if (UseDbg) { + ArrayRef
DebugSections = Sections; + std::vector
DSYMSections; + + // A separate DSym file path was specified, parse it as a macho file, + // get the sections and supply it to the section name parsing machinery. + if (!DSYMFile.empty()) { + OwningPtr Buf; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile.c_str(), Buf)) { + errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n'; + return; + } + DSYMObj.reset(MachOObject::LoadFromBuffer(Buf.take())); + const macho::Header &Header = DSYMObj->getHeader(); + + std::vector Symbols; + SmallVector FoundFns; + getSectionsAndSymbols(Header, DSYMObj.get(), 0, DSYMSections, Symbols, + FoundFns); + DebugSections = DSYMSections; + DbgInfoObj = DSYMObj.get(); + } + + // Find the named debug info sections. + for (unsigned SectIdx = 0; SectIdx != DebugSections.size(); SectIdx++) { + if (!strcmp(DebugSections[SectIdx].Name, "__debug_abbrev")) + DebugAbbrevSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset, + DebugSections[SectIdx].Size); + else if (!strcmp(DebugSections[SectIdx].Name, "__debug_info")) + DebugInfoSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset, + DebugSections[SectIdx].Size); + else if (!strcmp(DebugSections[SectIdx].Name, "__debug_aranges")) + DebugArangesSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset, + DebugSections[SectIdx].Size); + else if (!strcmp(DebugSections[SectIdx].Name, "__debug_line")) + DebugLineSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset, + DebugSections[SectIdx].Size); + else if (!strcmp(DebugSections[SectIdx].Name, "__debug_str")) + DebugStrSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset, + DebugSections[SectIdx].Size); + } + + // Setup the DIContext. + diContext.reset(DIContext::getDWARFContext(DbgInfoObj->isLittleEndian(), + DebugInfoSection, + DebugAbbrevSection, + DebugArangesSection, + DebugLineSection, + DebugStrSection)); + } + + FunctionMapTy FunctionMap; + FunctionListTy Functions; + + for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { + if (strcmp(Sections[SectIdx].Name, "__text")) + continue; // Skip non-text sections + + // Insert the functions from the function starts segment into our map. + uint64_t VMAddr = Sections[SectIdx].Address - Sections[SectIdx].Offset; + for (unsigned i = 0, e = FoundFns.size(); i != e; ++i) + FunctionMap.insert(std::make_pair(FoundFns[i]+VMAddr, (MCFunction*)0)); + + StringRef Bytes = MachOObj->getData(Sections[SectIdx].Offset, + Sections[SectIdx].Size); + StringRefMemoryObject memoryObject(Bytes); + bool symbolTableWorked = false; + + // Parse relocations. + std::vector > Relocs; + for (unsigned j = 0; j != Sections[SectIdx].NumRelocs; ++j) { + InMemoryStruct RE; + MachOObj->ReadRelocationEntry(Sections[SectIdx].RelocTableOffset, j, RE); + Relocs.push_back(std::make_pair(RE->Word0, RE->Word1 & 0xffffff)); + } + array_pod_sort(Relocs.begin(), Relocs.end()); + + // Disassemble symbol by symbol. + for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { + // Make sure the symbol is defined in this section. + if ((unsigned)Symbols[SymIdx].SectionIndex - 1 != SectIdx) + continue; + + // Start at the address of the symbol relative to the section's address. + uint64_t Start = Symbols[SymIdx].Value - Sections[SectIdx].Address; + // Stop disassembling either at the beginning of the next symbol or at + // the end of the section. + uint64_t End = (SymIdx+1 == Symbols.size() || + Symbols[SymIdx].SectionIndex != Symbols[SymIdx+1].SectionIndex) ? + Sections[SectIdx].Size : + Symbols[SymIdx+1].Value - Sections[SectIdx].Address; + uint64_t Size; + + if (Start >= End) + continue; + + symbolTableWorked = true; + + if (!CFG) { + // Normal disassembly, print addresses, bytes and mnemonic form. + outs() << MachOObj->getStringAtIndex(Symbols[SymIdx].StringIndex) + << ":\n"; + DILineInfo lastLine; + for (uint64_t Index = Start; Index < End; Index += Size) { + MCInst Inst; + + if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, + DebugOut, nulls())) { + outs() << format("%8llx:\t", Sections[SectIdx].Address + Index); + DumpBytes(StringRef(Bytes.data() + Index, Size)); + IP->printInst(&Inst, outs(), ""); + + // Print debug info. + if (diContext) { + DILineInfo dli = + diContext->getLineInfoForAddress(Sections[SectIdx].Address + + Index); + // Print valid line info if it changed. + if (dli != lastLine && dli.getLine() != 0) + outs() << "\t## " << dli.getFileName() << ':' + << dli.getLine() << ':' << dli.getColumn(); + lastLine = dli; + } + outs() << "\n"; + } else { + errs() << "llvm-objdump: warning: invalid instruction encoding\n"; + if (Size == 0) + Size = 1; // skip illegible bytes + } + } + } else { + // Create CFG and use it for disassembly. + createMCFunctionAndSaveCalls( + MachOObj->getStringAtIndex(Symbols[SymIdx].StringIndex), + DisAsm.get(), memoryObject, Start, End, InstrAnalysis.get(), + Start, DebugOut, FunctionMap, Functions); + } + } + + if (CFG) { + if (!symbolTableWorked) { + // Reading the symbol table didn't work, create a big __TEXT symbol. + createMCFunctionAndSaveCalls("__TEXT", DisAsm.get(), memoryObject, + 0, Sections[SectIdx].Size, + InstrAnalysis.get(), + Sections[SectIdx].Offset, DebugOut, + FunctionMap, Functions); + } + for (std::map::iterator mi = FunctionMap.begin(), + me = FunctionMap.end(); mi != me; ++mi) + if (mi->second == 0) { + // Create functions for the remaining callees we have gathered, + // but we didn't find a name for them. + SmallVector Calls; + MCFunction f = + MCFunction::createFunctionFromMC("unknown", DisAsm.get(), + memoryObject, mi->first, + Sections[SectIdx].Size, + InstrAnalysis.get(), DebugOut, + Calls); + Functions.push_back(f); + mi->second = &Functions.back(); + for (unsigned i = 0, e = Calls.size(); i != e; ++i) { + std::pair p(Calls[i], (MCFunction*)0); + if (FunctionMap.insert(p).second) + mi = FunctionMap.begin(); + } + } + + DenseSet PrintedBlocks; + for (unsigned ffi = 0, ffe = Functions.size(); ffi != ffe; ++ffi) { + MCFunction &f = Functions[ffi]; + for (MCFunction::iterator fi = f.begin(), fe = f.end(); fi != fe; ++fi){ + if (!PrintedBlocks.insert(fi->first).second) + continue; // We already printed this block. + + // We assume a block has predecessors when it's the first block after + // a symbol. + bool hasPreds = FunctionMap.find(fi->first) != FunctionMap.end(); + + // See if this block has predecessors. + // FIXME: Slow. + for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe; + ++pi) + if (pi->second.contains(fi->first)) { + hasPreds = true; + break; + } + + // No predecessors, this is a data block. Print as .byte directives. + if (!hasPreds) { + uint64_t End = llvm::next(fi) == fe ? Sections[SectIdx].Size : + llvm::next(fi)->first; + outs() << "# " << End-fi->first << " bytes of data:\n"; + for (unsigned pos = fi->first; pos != End; ++pos) { + outs() << format("%8x:\t", Sections[SectIdx].Address + pos); + DumpBytes(StringRef(Bytes.data() + pos, 1)); + outs() << format("\t.byte 0x%02x\n", (uint8_t)Bytes[pos]); + } + continue; + } + + if (fi->second.contains(fi->first)) // Print a header for simple loops + outs() << "# Loop begin:\n"; + + DILineInfo lastLine; + // Walk over the instructions and print them. + for (unsigned ii = 0, ie = fi->second.getInsts().size(); ii != ie; + ++ii) { + const MCDecodedInst &Inst = fi->second.getInsts()[ii]; + + // If there's a symbol at this address, print its name. + if (FunctionMap.find(Sections[SectIdx].Address + Inst.Address) != + FunctionMap.end()) + outs() << FunctionMap[Sections[SectIdx].Address + Inst.Address]-> + getName() << ":\n"; + + outs() << format("%8llx:\t", Sections[SectIdx].Address + + Inst.Address); + DumpBytes(StringRef(Bytes.data() + Inst.Address, Inst.Size)); + + if (fi->second.contains(fi->first)) // Indent simple loops. + outs() << '\t'; + + IP->printInst(&Inst.Inst, outs(), ""); + + // Look for relocations inside this instructions, if there is one + // print its target and additional information if available. + for (unsigned j = 0; j != Relocs.size(); ++j) + if (Relocs[j].first >= Sections[SectIdx].Address + Inst.Address && + Relocs[j].first < Sections[SectIdx].Address + Inst.Address + + Inst.Size) { + outs() << "\t# " + << MachOObj->getStringAtIndex( + UnsortedSymbols[Relocs[j].second].StringIndex) + << ' '; + DumpAddress(UnsortedSymbols[Relocs[j].second].Value, Sections, + MachOObj.get(), outs()); + } + + // If this instructions contains an address, see if we can evaluate + // it and print additional information. + uint64_t targ = InstrAnalysis->evaluateBranch(Inst.Inst, + Inst.Address, + Inst.Size); + if (targ != -1ULL) + DumpAddress(targ, Sections, MachOObj.get(), outs()); + + // Print debug info. + if (diContext) { + DILineInfo dli = + diContext->getLineInfoForAddress(Sections[SectIdx].Address + + Inst.Address); + // Print valid line info if it changed. + if (dli != lastLine && dli.getLine() != 0) + outs() << "\t## " << dli.getFileName() << ':' + << dli.getLine() << ':' << dli.getColumn(); + lastLine = dli; + } + + outs() << '\n'; + } + } + + emitDOTFile((f.getName().str() + ".dot").c_str(), f, IP.get()); + } + } + } +} diff --git a/contrib/llvm/tools/llvm-objdump/Makefile b/contrib/llvm/tools/llvm-objdump/Makefile new file mode 100644 index 000000000..703bf6c8a --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/Makefile @@ -0,0 +1,18 @@ +##===- tools/llvm-objdump/Makefile -------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-objdump +LINK_COMPONENTS = $(TARGETS_TO_BUILD) DebugInfo MC MCParser MCDisassembler \ + Object + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-objdump/llvm-objdump.cpp b/contrib/llvm/tools/llvm-objdump/llvm-objdump.cpp new file mode 100644 index 000000000..40c59bd8c --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -0,0 +1,459 @@ +//===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is a utility that works like binutils "objdump", that is, it +// dumps out a plethora of information about an object file depending on the +// flags. +// +//===----------------------------------------------------------------------===// + +#include "llvm-objdump.h" +#include "MCFunction.h" +#include "llvm/Object/Archive.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/Triple.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCDisassembler.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/GraphWriter.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/MemoryObject.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" +#include +#include +using namespace llvm; +using namespace object; + +static cl::list +InputFilenames(cl::Positional, cl::desc(""),cl::ZeroOrMore); + +static cl::opt +Disassemble("disassemble", + cl::desc("Display assembler mnemonics for the machine instructions")); +static cl::alias +Disassembled("d", cl::desc("Alias for --disassemble"), + cl::aliasopt(Disassemble)); + +static cl::opt +Relocations("r", cl::desc("Display the relocation entries in the file")); + +static cl::opt +MachO("macho", cl::desc("Use MachO specific object file parser")); +static cl::alias +MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO)); + +cl::opt +llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " + "see -version for available targets")); + +cl::opt +llvm::ArchName("arch", cl::desc("Target arch to disassemble for, " + "see -version for available targets")); + +static cl::opt +SectionHeaders("section-headers", cl::desc("Display summaries of the headers " + "for each section.")); +static cl::alias +SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), + cl::aliasopt(SectionHeaders)); +static cl::alias +SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), + cl::aliasopt(SectionHeaders)); + +static StringRef ToolName; + +static bool error(error_code ec) { + if (!ec) return false; + + outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; + outs().flush(); + return true; +} + +static const Target *GetTarget(const ObjectFile *Obj = NULL) { + // Figure out the target triple. + llvm::Triple TT("unknown-unknown-unknown"); + if (TripleName.empty()) { + if (Obj) + TT.setArch(Triple::ArchType(Obj->getArch())); + } else + TT.setTriple(Triple::normalize(TripleName)); + + if (!ArchName.empty()) + TT.setArchName(ArchName); + + TripleName = TT.str(); + + // Get the target specific parser. + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); + if (TheTarget) + return TheTarget; + + errs() << ToolName << ": error: unable to get target for '" << TripleName + << "', see --version and --triple.\n"; + return 0; +} + +void llvm::DumpBytes(StringRef bytes) { + static const char hex_rep[] = "0123456789abcdef"; + // FIXME: The real way to do this is to figure out the longest instruction + // and align to that size before printing. I'll fix this when I get + // around to outputting relocations. + // 15 is the longest x86 instruction + // 3 is for the hex rep of a byte + a space. + // 1 is for the null terminator. + enum { OutputSize = (15 * 3) + 1 }; + char output[OutputSize]; + + assert(bytes.size() <= 15 + && "DumpBytes only supports instructions of up to 15 bytes"); + memset(output, ' ', sizeof(output)); + unsigned index = 0; + for (StringRef::iterator i = bytes.begin(), + e = bytes.end(); i != e; ++i) { + output[index] = hex_rep[(*i & 0xF0) >> 4]; + output[index + 1] = hex_rep[*i & 0xF]; + index += 3; + } + + output[sizeof(output) - 1] = 0; + outs() << output; +} + +static bool RelocAddressLess(RelocationRef a, RelocationRef b) { + uint64_t a_addr, b_addr; + if (error(a.getAddress(a_addr))) return false; + if (error(b.getAddress(b_addr))) return false; + return a_addr < b_addr; +} + +static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { + const Target *TheTarget = GetTarget(Obj); + if (!TheTarget) { + // GetTarget prints out stuff. + return; + } + + outs() << '\n'; + outs() << Obj->getFileName() + << ":\tfile format " << Obj->getFileFormatName() << "\n\n"; + + error_code ec; + for (section_iterator i = Obj->begin_sections(), + e = Obj->end_sections(); + i != e; i.increment(ec)) { + if (error(ec)) break; + bool text; + if (error(i->isText(text))) break; + if (!text) continue; + + uint64_t SectionAddr; + if (error(i->getAddress(SectionAddr))) break; + + // Make a list of all the symbols in this section. + std::vector > Symbols; + for (symbol_iterator si = Obj->begin_symbols(), + se = Obj->end_symbols(); + si != se; si.increment(ec)) { + bool contains; + if (!error(i->containsSymbol(*si, contains)) && contains) { + uint64_t Address; + if (error(si->getOffset(Address))) break; + StringRef Name; + if (error(si->getName(Name))) break; + Symbols.push_back(std::make_pair(Address, Name)); + } + } + + // Sort the symbols by address, just in case they didn't come in that way. + array_pod_sort(Symbols.begin(), Symbols.end()); + + // Make a list of all the relocations for this section. + std::vector Rels; + if (InlineRelocs) { + for (relocation_iterator ri = i->begin_relocations(), + re = i->end_relocations(); + ri != re; ri.increment(ec)) { + if (error(ec)) break; + Rels.push_back(*ri); + } + } + + // Sort relocations by address. + std::sort(Rels.begin(), Rels.end(), RelocAddressLess); + + StringRef name; + if (error(i->getName(name))) break; + outs() << "Disassembly of section " << name << ':'; + + // If the section has no symbols just insert a dummy one and disassemble + // the whole section. + if (Symbols.empty()) + Symbols.push_back(std::make_pair(0, name)); + + // Set up disassembler. + OwningPtr AsmInfo(TheTarget->createMCAsmInfo(TripleName)); + + if (!AsmInfo) { + errs() << "error: no assembly info for target " << TripleName << "\n"; + return; + } + + OwningPtr STI( + TheTarget->createMCSubtargetInfo(TripleName, "", "")); + + if (!STI) { + errs() << "error: no subtarget info for target " << TripleName << "\n"; + return; + } + + OwningPtr DisAsm( + TheTarget->createMCDisassembler(*STI)); + if (!DisAsm) { + errs() << "error: no disassembler for target " << TripleName << "\n"; + return; + } + + int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); + OwningPtr IP(TheTarget->createMCInstPrinter( + AsmPrinterVariant, *AsmInfo, *STI)); + if (!IP) { + errs() << "error: no instruction printer for target " << TripleName + << '\n'; + return; + } + + StringRef Bytes; + if (error(i->getContents(Bytes))) break; + StringRefMemoryObject memoryObject(Bytes); + uint64_t Size; + uint64_t Index; + uint64_t SectSize; + if (error(i->getSize(SectSize))) break; + + std::vector::const_iterator rel_cur = Rels.begin(); + std::vector::const_iterator rel_end = Rels.end(); + // Disassemble symbol by symbol. + for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { + uint64_t Start = Symbols[si].first; + uint64_t End; + // The end is either the size of the section or the beginning of the next + // symbol. + if (si == se - 1) + End = SectSize; + // Make sure this symbol takes up space. + else if (Symbols[si + 1].first != Start) + End = Symbols[si + 1].first - 1; + else + // This symbol has the same address as the next symbol. Skip it. + continue; + + outs() << '\n' << Symbols[si].second << ":\n"; + +#ifndef NDEBUG + raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); +#else + raw_ostream &DebugOut = nulls(); +#endif + + for (Index = Start; Index < End; Index += Size) { + MCInst Inst; + + if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, + DebugOut, nulls())) { + outs() << format("%8"PRIx64":\t", SectionAddr + Index); + DumpBytes(StringRef(Bytes.data() + Index, Size)); + IP->printInst(&Inst, outs(), ""); + outs() << "\n"; + } else { + errs() << ToolName << ": warning: invalid instruction encoding\n"; + if (Size == 0) + Size = 1; // skip illegible bytes + } + + // Print relocation for instruction. + while (rel_cur != rel_end) { + uint64_t addr; + SmallString<16> name; + SmallString<32> val; + if (error(rel_cur->getAddress(addr))) goto skip_print_rel; + // Stop when rel_cur's address is past the current instruction. + if (addr > Index + Size) break; + if (error(rel_cur->getTypeName(name))) goto skip_print_rel; + if (error(rel_cur->getValueString(val))) goto skip_print_rel; + + outs() << format("\t\t\t%8"PRIx64": ", SectionAddr + addr) << name << "\t" + << val << "\n"; + + skip_print_rel: + ++rel_cur; + } + } + } + } +} + +static void PrintRelocations(const ObjectFile *o) { + error_code ec; + for (section_iterator si = o->begin_sections(), se = o->end_sections(); + si != se; si.increment(ec)){ + if (error(ec)) return; + if (si->begin_relocations() == si->end_relocations()) + continue; + StringRef secname; + if (error(si->getName(secname))) continue; + outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; + for (relocation_iterator ri = si->begin_relocations(), + re = si->end_relocations(); + ri != re; ri.increment(ec)) { + if (error(ec)) return; + + uint64_t address; + SmallString<32> relocname; + SmallString<32> valuestr; + if (error(ri->getTypeName(relocname))) continue; + if (error(ri->getAddress(address))) continue; + if (error(ri->getValueString(valuestr))) continue; + outs() << address << " " << relocname << " " << valuestr << "\n"; + } + outs() << "\n"; + } +} + +static void PrintSectionHeaders(const ObjectFile *o) { + outs() << "Sections:\n" + "Idx Name Size Address Type\n"; + error_code ec; + unsigned i = 0; + for (section_iterator si = o->begin_sections(), se = o->end_sections(); + si != se; si.increment(ec)) { + if (error(ec)) return; + StringRef Name; + if (error(si->getName(Name))) return; + uint64_t Address; + if (error(si->getAddress(Address))) return; + uint64_t Size; + if (error(si->getSize(Size))) return; + bool Text, Data, BSS; + if (error(si->isText(Text))) return; + if (error(si->isData(Data))) return; + if (error(si->isBSS(BSS))) return; + std::string Type = (std::string(Text ? "TEXT " : "") + + (Data ? "DATA " : "") + (BSS ? "BSS" : "")); + outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size, + Address, Type.c_str()); + ++i; + } +} + +static void DumpObject(const ObjectFile *o) { + if (Disassemble) + DisassembleObject(o, Relocations); + if (Relocations && !Disassemble) + PrintRelocations(o); + if (SectionHeaders) + PrintSectionHeaders(o); +} + +/// @brief Dump each object file in \a a; +static void DumpArchive(const Archive *a) { + for (Archive::child_iterator i = a->begin_children(), + e = a->end_children(); i != e; ++i) { + OwningPtr child; + if (error_code ec = i->getAsBinary(child)) { + errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message() + << ".\n"; + continue; + } + if (ObjectFile *o = dyn_cast(child.get())) + DumpObject(o); + else + errs() << ToolName << ": '" << a->getFileName() << "': " + << "Unrecognized file type.\n"; + } +} + +/// @brief Open file and figure out how to dump it. +static void DumpInput(StringRef file) { + // If file isn't stdin, check that it exists. + if (file != "-" && !sys::fs::exists(file)) { + errs() << ToolName << ": '" << file << "': " << "No such file\n"; + return; + } + + if (MachO && Disassemble) { + DisassembleInputMachO(file); + return; + } + + // Attempt to open the binary. + OwningPtr binary; + if (error_code ec = createBinary(file, binary)) { + errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n"; + return; + } + + if (Archive *a = dyn_cast(binary.get())) { + DumpArchive(a); + } else if (ObjectFile *o = dyn_cast(binary.get())) { + DumpObject(o); + } else { + errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n"; + } +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + // Initialize targets and assembly printers/parsers. + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmParsers(); + llvm::InitializeAllDisassemblers(); + + cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); + TripleName = Triple::normalize(TripleName); + + ToolName = argv[0]; + + // Defaults to a.out if no filenames specified. + if (InputFilenames.size() == 0) + InputFilenames.push_back("a.out"); + + if (!Disassemble && !Relocations && !SectionHeaders) { + cl::PrintHelpMessage(); + return 2; + } + + std::for_each(InputFilenames.begin(), InputFilenames.end(), + DumpInput); + + return 0; +} diff --git a/contrib/llvm/tools/llvm-objdump/llvm-objdump.h b/contrib/llvm/tools/llvm-objdump/llvm-objdump.h new file mode 100644 index 000000000..75f852afb --- /dev/null +++ b/contrib/llvm/tools/llvm-objdump/llvm-objdump.h @@ -0,0 +1,46 @@ +//===-- llvm-objdump.h ----------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJDUMP_H +#define LLVM_OBJDUMP_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/DataTypes.h" +#include "llvm/Support/MemoryObject.h" + +namespace llvm { + +extern cl::opt TripleName; +extern cl::opt ArchName; + +// Various helper functions. +void DumpBytes(StringRef bytes); +void DisassembleInputMachO(StringRef Filename); + +class StringRefMemoryObject : public MemoryObject { +private: + StringRef Bytes; +public: + StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {} + + uint64_t getBase() const { return 0; } + uint64_t getExtent() const { return Bytes.size(); } + + int readByte(uint64_t Addr, uint8_t *Byte) const { + if (Addr >= getExtent()) + return -1; + *Byte = Bytes[Addr]; + return 0; + } +}; + +} + +#endif diff --git a/contrib/llvm/tools/llvm-prof/CMakeLists.txt b/contrib/llvm/tools/llvm-prof/CMakeLists.txt new file mode 100644 index 000000000..442112bc8 --- /dev/null +++ b/contrib/llvm/tools/llvm-prof/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS bitreader analysis) + +add_llvm_tool(llvm-prof + llvm-prof.cpp + ) diff --git a/contrib/llvm/tools/llvm-prof/Makefile b/contrib/llvm/tools/llvm-prof/Makefile new file mode 100644 index 000000000..86eb54d51 --- /dev/null +++ b/contrib/llvm/tools/llvm-prof/Makefile @@ -0,0 +1,17 @@ +##===- tools/llvm-prof/Makefile ----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. + +TOOLNAME = llvm-prof +LINK_COMPONENTS = bitreader analysis + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-prof/llvm-prof.cpp b/contrib/llvm/tools/llvm-prof/llvm-prof.cpp new file mode 100644 index 000000000..9d0b46833 --- /dev/null +++ b/contrib/llvm/tools/llvm-prof/llvm-prof.cpp @@ -0,0 +1,293 @@ +//===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This tools is meant for use with the various LLVM profiling instrumentation +// passes. It reads in the data file produced by executing an instrumented +// program, and outputs a nice report. +// +//===----------------------------------------------------------------------===// + +#include "llvm/InstrTypes.h" +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Assembly/AssemblyAnnotationWriter.h" +#include "llvm/Analysis/ProfileInfo.h" +#include "llvm/Analysis/ProfileInfoLoader.h" +#include "llvm/Analysis/Passes.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/system_error.h" +#include +#include +#include +#include + +using namespace llvm; + +namespace { + cl::opt + BitcodeFile(cl::Positional, cl::desc(""), + cl::Required); + + cl::opt + ProfileDataFile(cl::Positional, cl::desc(""), + cl::Optional, cl::init("llvmprof.out")); + + cl::opt + PrintAnnotatedLLVM("annotated-llvm", + cl::desc("Print LLVM code with frequency annotations")); + cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"), + cl::aliasopt(PrintAnnotatedLLVM)); + cl::opt + PrintAllCode("print-all-code", + cl::desc("Print annotated code for the entire program")); +} + +// PairSecondSort - A sorting predicate to sort by the second element of a pair. +template +struct PairSecondSortReverse + : public std::binary_function, + std::pair, bool> { + bool operator()(const std::pair &LHS, + const std::pair &RHS) const { + return LHS.second > RHS.second; + } +}; + +static double ignoreMissing(double w) { + if (w == ProfileInfo::MissingValue) return 0; + return w; +} + +namespace { + class ProfileAnnotator : public AssemblyAnnotationWriter { + ProfileInfo &PI; + public: + ProfileAnnotator(ProfileInfo &pi) : PI(pi) {} + + virtual void emitFunctionAnnot(const Function *F, + formatted_raw_ostream &OS) { + double w = PI.getExecutionCount(F); + if (w != ProfileInfo::MissingValue) { + OS << ";;; %" << F->getName() << " called "<<(unsigned)w + <<" times.\n;;;\n"; + } + } + virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, + formatted_raw_ostream &OS) { + double w = PI.getExecutionCount(BB); + if (w != ProfileInfo::MissingValue) { + if (w != 0) { + OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n"; + } else { + OS << "\t;;; Never executed!\n"; + } + } + } + + virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, + formatted_raw_ostream &OS) { + // Figure out how many times each successor executed. + std::vector > SuccCounts; + + const TerminatorInst *TI = BB->getTerminator(); + for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { + BasicBlock* Succ = TI->getSuccessor(s); + double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ))); + if (w != 0) + SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w)); + } + if (!SuccCounts.empty()) { + OS << "\t;;; Out-edge counts:"; + for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i) + OS << " [" << (SuccCounts[i]).second << " -> " + << (SuccCounts[i]).first.second->getName() << "]"; + OS << "\n"; + } + } + }; +} + +namespace { + /// ProfileInfoPrinterPass - Helper pass to dump the profile information for + /// a module. + // + // FIXME: This should move elsewhere. + class ProfileInfoPrinterPass : public ModulePass { + ProfileInfoLoader &PIL; + public: + static char ID; // Class identification, replacement for typeinfo. + explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL) + : ModulePass(ID), PIL(_PIL) {} + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); + } + + bool runOnModule(Module &M); + }; +} + +char ProfileInfoPrinterPass::ID = 0; + +bool ProfileInfoPrinterPass::runOnModule(Module &M) { + ProfileInfo &PI = getAnalysis(); + std::map FuncFreqs; + std::map BlockFreqs; + std::map EdgeFreqs; + + // Output a report. Eventually, there will be multiple reports selectable on + // the command line, for now, just keep things simple. + + // Emit the most frequent function table... + std::vector > FunctionCounts; + std::vector > Counts; + for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { + if (FI->isDeclaration()) continue; + double w = ignoreMissing(PI.getExecutionCount(FI)); + FunctionCounts.push_back(std::make_pair(FI, w)); + for (Function::iterator BB = FI->begin(), BBE = FI->end(); + BB != BBE; ++BB) { + double w = ignoreMissing(PI.getExecutionCount(BB)); + Counts.push_back(std::make_pair(BB, w)); + } + } + + // Sort by the frequency, backwards. + sort(FunctionCounts.begin(), FunctionCounts.end(), + PairSecondSortReverse()); + + double TotalExecutions = 0; + for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) + TotalExecutions += FunctionCounts[i].second; + + outs() << "===" << std::string(73, '-') << "===\n" + << "LLVM profiling output for execution"; + if (PIL.getNumExecutions() != 1) outs() << "s"; + outs() << ":\n"; + + for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) { + outs() << " "; + if (e != 1) outs() << i+1 << ". "; + outs() << PIL.getExecution(i) << "\n"; + } + + outs() << "\n===" << std::string(73, '-') << "===\n"; + outs() << "Function execution frequencies:\n\n"; + + // Print out the function frequencies... + outs() << " ## Frequency\n"; + for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) { + if (FunctionCounts[i].second == 0) { + outs() << "\n NOTE: " << e-i << " function" + << (e-i-1 ? "s were" : " was") << " never executed!\n"; + break; + } + + outs() << format("%3d", i+1) << ". " + << format("%5.2g", FunctionCounts[i].second) << "/" + << format("%g", TotalExecutions) << " " + << FunctionCounts[i].first->getNameStr() << "\n"; + } + + std::set FunctionsToPrint; + + TotalExecutions = 0; + for (unsigned i = 0, e = Counts.size(); i != e; ++i) + TotalExecutions += Counts[i].second; + + // Sort by the frequency, backwards. + sort(Counts.begin(), Counts.end(), + PairSecondSortReverse()); + + outs() << "\n===" << std::string(73, '-') << "===\n"; + outs() << "Top 20 most frequently executed basic blocks:\n\n"; + + // Print out the function frequencies... + outs() <<" ## %% \tFrequency\n"; + unsigned BlocksToPrint = Counts.size(); + if (BlocksToPrint > 20) BlocksToPrint = 20; + for (unsigned i = 0; i != BlocksToPrint; ++i) { + if (Counts[i].second == 0) break; + Function *F = Counts[i].first->getParent(); + outs() << format("%3d", i+1) << ". " + << format("%5g", Counts[i].second/(double)TotalExecutions*100) << "% " + << format("%5.0f", Counts[i].second) << "/" + << format("%g", TotalExecutions) << "\t" + << F->getNameStr() << "() - " + << Counts[i].first->getNameStr() << "\n"; + FunctionsToPrint.insert(F); + } + + if (PrintAnnotatedLLVM || PrintAllCode) { + outs() << "\n===" << std::string(73, '-') << "===\n"; + outs() << "Annotated LLVM code for the module:\n\n"; + + ProfileAnnotator PA(PI); + + if (FunctionsToPrint.empty() || PrintAllCode) + M.print(outs(), &PA); + else + // Print just a subset of the functions. + for (std::set::iterator I = FunctionsToPrint.begin(), + E = FunctionsToPrint.end(); I != E; ++I) + (*I)->print(outs(), &PA); + } + + return false; +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n"); + + // Read in the bitcode file... + std::string ErrorMessage; + OwningPtr Buffer; + error_code ec; + Module *M = 0; + if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) { + M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); + } else + ErrorMessage = ec.message(); + if (M == 0) { + errs() << argv[0] << ": " << BitcodeFile << ": " + << ErrorMessage << "\n"; + return 1; + } + + // Read the profiling information. This is redundant since we load it again + // using the standard profile info provider pass, but for now this gives us + // access to additional information not exposed via the ProfileInfo + // interface. + ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M); + + // Run the printer pass. + PassManager PassMgr; + PassMgr.add(createProfileLoaderPass(ProfileDataFile)); + PassMgr.add(new ProfileInfoPrinterPass(PIL)); + PassMgr.run(*M); + + return 0; +} diff --git a/contrib/llvm/tools/llvm-ranlib/CMakeLists.txt b/contrib/llvm/tools/llvm-ranlib/CMakeLists.txt new file mode 100644 index 000000000..3116d2e4f --- /dev/null +++ b/contrib/llvm/tools/llvm-ranlib/CMakeLists.txt @@ -0,0 +1,6 @@ +set(LLVM_LINK_COMPONENTS archive) +set(LLVM_REQUIRES_EH 1) + +add_llvm_tool(llvm-ranlib + llvm-ranlib.cpp + ) diff --git a/contrib/llvm/tools/llvm-ranlib/Makefile b/contrib/llvm/tools/llvm-ranlib/Makefile new file mode 100644 index 000000000..46a10e644 --- /dev/null +++ b/contrib/llvm/tools/llvm-ranlib/Makefile @@ -0,0 +1,18 @@ +##===- tools/llvm-ranlib/Makefile --------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llvm-ranlib +LINK_COMPONENTS = archive +REQUIRES_EH := 1 + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/llvm-ranlib/llvm-ranlib.cpp b/contrib/llvm/tools/llvm-ranlib/llvm-ranlib.cpp new file mode 100644 index 000000000..64f795f7f --- /dev/null +++ b/contrib/llvm/tools/llvm-ranlib/llvm-ranlib.cpp @@ -0,0 +1,101 @@ +//===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Adds or updates an index (symbol table) for an LLVM archive file. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/Bitcode/Archive.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" +#include +using namespace llvm; + +// llvm-ar operation code and modifier flags +static cl::opt +ArchiveName(cl::Positional, cl::Optional, cl::desc("")); + +static cl::opt +Verbose("verbose",cl::Optional,cl::init(false), + cl::desc("Print the symbol table")); + +// printSymbolTable - print out the archive's symbol table. +void printSymbolTable(Archive* TheArchive) { + outs() << "\nArchive Symbol Table:\n"; + const Archive::SymTabType& symtab = TheArchive->getSymbolTable(); + for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); + I != E; ++I ) { + unsigned offset = TheArchive->getFirstFileOffset() + I->second; + outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n"; + } +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + llvm::sys::PrintStackTraceOnErrorSignal(); + llvm::PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + // Have the command line options parsed and handle things + // like --help and --version. + cl::ParseCommandLineOptions(argc, argv, + "LLVM Archive Index Generator (llvm-ranlib)\n\n" + " This program adds or updates an index of bitcode symbols\n" + " to an LLVM archive file." + ); + + int exitCode = 0; + + // Make sure we don't exit with "unhandled exception". + try { + + // Check the path name of the archive + sys::Path ArchivePath; + if (!ArchivePath.set(ArchiveName)) + throw std::string("Archive name invalid: ") + ArchiveName; + + // Make sure it exists, we don't create empty archives + bool Exists; + if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) + throw std::string("Archive file does not exist"); + + std::string err_msg; + std::auto_ptr + AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg)); + Archive* TheArchive = AutoArchive.get(); + if (!TheArchive) + throw err_msg; + + if (TheArchive->writeToDisk(true, false, false, &err_msg )) + throw err_msg; + + if (Verbose) + printSymbolTable(TheArchive); + + } catch (const char* msg) { + errs() << argv[0] << ": " << msg << "\n\n"; + exitCode = 1; + } catch (const std::string& msg) { + errs() << argv[0] << ": " << msg << "\n"; + exitCode = 2; + } catch (...) { + errs() << argv[0] << ": An unexpected unknown exception occurred.\n"; + exitCode = 3; + } + return exitCode; +} diff --git a/contrib/llvm/tools/llvm-rtdyld/CMakeLists.txt b/contrib/llvm/tools/llvm-rtdyld/CMakeLists.txt new file mode 100644 index 000000000..17e2c3e2d --- /dev/null +++ b/contrib/llvm/tools/llvm-rtdyld/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} support MC object RuntimeDyld JIT) + +add_llvm_tool(llvm-rtdyld + llvm-rtdyld.cpp + ) diff --git a/contrib/llvm/tools/llvm-rtdyld/Makefile b/contrib/llvm/tools/llvm-rtdyld/Makefile new file mode 100644 index 000000000..0d57277f0 --- /dev/null +++ b/contrib/llvm/tools/llvm-rtdyld/Makefile @@ -0,0 +1,23 @@ +##===- tools/llvm-rtdyld/Makefile --------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llvm-rtdyld + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +# Include this here so we can get the configuration of the targets +# that have been configured for construction. We have to do this +# early so we can set up LINK_COMPONENTS before including Makefile.rules +include $(LEVEL)/Makefile.config + +LINK_COMPONENTS := $(TARGETS_TO_BUILD) support MC object RuntimeDyld JIT + +include $(LLVM_SRC_ROOT)/Makefile.rules diff --git a/contrib/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp b/contrib/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp new file mode 100644 index 000000000..ec9d6526e --- /dev/null +++ b/contrib/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -0,0 +1,151 @@ +//===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This is a testing tool for use with the MC-JIT LLVM components. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ExecutionEngine/RuntimeDyld.h" +#include "llvm/Object/MachOObject.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Memory.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" +using namespace llvm; +using namespace llvm::object; + +static cl::list +InputFileList(cl::Positional, cl::ZeroOrMore, + cl::desc("")); + +enum ActionType { + AC_Execute +}; + +static cl::opt +Action(cl::desc("Action to perform:"), + cl::init(AC_Execute), + cl::values(clEnumValN(AC_Execute, "execute", + "Load, link, and execute the inputs."), + clEnumValEnd)); + +static cl::opt +EntryPoint("entry", + cl::desc("Function to call as entry point."), + cl::init("_main")); + +/* *** */ + +// A trivial memory manager that doesn't do anything fancy, just uses the +// support library allocation routines directly. +class TrivialMemoryManager : public RTDyldMemoryManager { +public: + SmallVector FunctionMemory; + + uint8_t *startFunctionBody(const char *Name, uintptr_t &Size); + void endFunctionBody(const char *Name, uint8_t *FunctionStart, + uint8_t *FunctionEnd); +}; + +uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name, + uintptr_t &Size) { + return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); +} + +void TrivialMemoryManager::endFunctionBody(const char *Name, + uint8_t *FunctionStart, + uint8_t *FunctionEnd) { + uintptr_t Size = FunctionEnd - FunctionStart + 1; + FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size)); +} + +static const char *ProgramName; + +static void Message(const char *Type, const Twine &Msg) { + errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; +} + +static int Error(const Twine &Msg) { + Message("error", Msg); + return 1; +} + +/* *** */ + +static int executeInput() { + // Instantiate a dynamic linker. + TrivialMemoryManager *MemMgr = new TrivialMemoryManager; + RuntimeDyld Dyld(MemMgr); + + // If we don't have any input files, read from stdin. + if (!InputFileList.size()) + InputFileList.push_back("-"); + for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { + // Load the input memory buffer. + OwningPtr InputBuffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i], + InputBuffer)) + return Error("unable to read input: '" + ec.message() + "'"); + + // Load the object file into it. + if (Dyld.loadObject(InputBuffer.take())) { + return Error(Dyld.getErrorString()); + } + } + + // Resolve all the relocations we can. + Dyld.resolveRelocations(); + + // FIXME: Error out if there are unresolved relocations. + + // Get the address of the entry point (_main by default). + void *MainAddress = Dyld.getSymbolAddress(EntryPoint); + if (MainAddress == 0) + return Error("no definition for '" + EntryPoint + "'"); + + // Invalidate the instruction cache for each loaded function. + for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) { + sys::MemoryBlock &Data = MemMgr->FunctionMemory[i]; + // Make sure the memory is executable. + std::string ErrorStr; + sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); + if (!sys::Memory::setExecutable(Data, &ErrorStr)) + return Error("unable to mark function executable: '" + ErrorStr + "'"); + } + + // Dispatch to _main(). + errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; + + int (*Main)(int, const char**) = + (int(*)(int,const char**)) uintptr_t(MainAddress); + const char **Argv = new const char*[2]; + // Use the name of the first input object module as argv[0] for the target. + Argv[0] = InputFileList[0].c_str(); + Argv[1] = 0; + return Main(1, Argv); +} + +int main(int argc, char **argv) { + ProgramName = argv[0]; + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); + + switch (Action) { + default: + case AC_Execute: + return executeInput(); + } + + return 0; +} diff --git a/contrib/llvm/tools/llvm-stub/CMakeLists.txt b/contrib/llvm/tools/llvm-stub/CMakeLists.txt new file mode 100644 index 000000000..a98dc9ed4 --- /dev/null +++ b/contrib/llvm/tools/llvm-stub/CMakeLists.txt @@ -0,0 +1,3 @@ +add_llvm_tool(llvm-stub + llvm-stub.c + ) diff --git a/contrib/llvm/tools/llvm-stub/Makefile b/contrib/llvm/tools/llvm-stub/Makefile new file mode 100644 index 000000000..7ffe14976 --- /dev/null +++ b/contrib/llvm/tools/llvm-stub/Makefile @@ -0,0 +1,13 @@ +##===- tools/llvm-stub/Makefile ----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = llvm-stub +include $(LEVEL)/Makefile.common + diff --git a/contrib/llvm/tools/llvm-stub/llvm-stub.c b/contrib/llvm/tools/llvm-stub/llvm-stub.c new file mode 100644 index 000000000..69cd6edbe --- /dev/null +++ b/contrib/llvm/tools/llvm-stub/llvm-stub.c @@ -0,0 +1,77 @@ +/*===- llvm-stub.c - Stub executable to run llvm bitcode files ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This tool is used by the gccld program to enable transparent execution of +// bitcode files by the user. Specifically, gccld outputs two files when asked +// to compile a file: +// 1. It outputs the LLVM bitcode file to .bc +// 2. It outputs a stub executable that runs lli on .bc +// +// This allows the end user to just say ./ and have the JIT executed +// automatically. On unix, the stub executable emitted is actually a bourne +// shell script that does the forwarding. Windows does not like #!/bin/sh +// programs in .exe files, so we make it an actual program, defined here. +// +//===----------------------------------------------------------------------===*/ + +#include +#include +#include + +#include "llvm/Config/config.h" + +#if defined(HAVE_UNISTD_H) && !defined(_MSC_VER) +#include +#endif + +#ifdef _WIN32 +#include +#include +#endif + +int main(int argc, char** argv) { + const char *Interp = getenv("LLVMINTERP"); + const char **Args; + if (Interp == 0) Interp = "lli"; + + /* Set up the command line options to pass to the JIT. */ + Args = (const char**)malloc(sizeof(char*) * (argc+2)); + /* argv[0] is the JIT */ + Args[0] = Interp; + +#ifdef LLVM_ON_WIN32 + { + int len = strlen(argv[0]); + if (len < 4 || strcmp(argv[0] + len - 4, ".exe") != 0) { + /* .exe suffix is stripped off of argv[0] if the executable was run on the + * command line without one. Put it back on. + */ + argv[0] = strcat(strcpy((char*)malloc(len + 5), argv[0]), ".exe"); + } + } +#endif + + /* argv[1] is argv[0] + ".bc". */ + Args[1] = strcat(strcpy((char*)malloc(strlen(argv[0])+4), argv[0]), ".bc"); + + /* The rest of the args are as before. */ + memcpy((char **)Args+2, argv+1, sizeof(char*)*argc); + + /* Run the JIT. */ +#if !defined(_WIN32) || defined(__MINGW64__) + execvp(Interp, (char **)Args); /* POSIX execvp takes a char *const[]. */ +#else + execvp(Interp, Args); /* windows execvp takes a const char *const *. */ +#endif + /* if _execv returns, the JIT could not be started. */ + fprintf(stderr, "Could not execute the LLVM JIT. Either add 'lli' to your" + " path, or set the\ninterpreter you want to use in the LLVMINTERP " + "environment variable.\n"); + return 1; +} diff --git a/contrib/llvm/tools/macho-dump/CMakeLists.txt b/contrib/llvm/tools/macho-dump/CMakeLists.txt new file mode 100644 index 000000000..d55e1d5c4 --- /dev/null +++ b/contrib/llvm/tools/macho-dump/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} support object) + +add_llvm_tool(macho-dump + macho-dump.cpp + ) diff --git a/contrib/llvm/tools/macho-dump/Makefile b/contrib/llvm/tools/macho-dump/Makefile new file mode 100644 index 000000000..638015e92 --- /dev/null +++ b/contrib/llvm/tools/macho-dump/Makefile @@ -0,0 +1,23 @@ +##===- tools/macho-dump/Makefile ---------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = macho-dump + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +# Include this here so we can get the configuration of the targets +# that have been configured for construction. We have to do this +# early so we can set up LINK_COMPONENTS before including Makefile.rules +include $(LEVEL)/Makefile.config + +LINK_COMPONENTS := support object + +include $(LLVM_SRC_ROOT)/Makefile.rules diff --git a/contrib/llvm/tools/macho-dump/macho-dump.cpp b/contrib/llvm/tools/macho-dump/macho-dump.cpp new file mode 100644 index 000000000..2b22c3b0f --- /dev/null +++ b/contrib/llvm/tools/macho-dump/macho-dump.cpp @@ -0,0 +1,400 @@ +//===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This is a testing tool for use with the MC/Mach-O LLVM components. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/MachOObject.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" +using namespace llvm; +using namespace llvm::object; + +static cl::opt +InputFile(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt +ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"), + cl::init(false)); + +/// + +static const char *ProgramName; + +static void Message(const char *Type, const Twine &Msg) { + errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; +} + +static int Error(const Twine &Msg) { + Message("error", Msg); + return 1; +} + +static void Warning(const Twine &Msg) { + Message("warning", Msg); +} + +/// + +static void DumpSegmentCommandData(StringRef Name, + uint64_t VMAddr, uint64_t VMSize, + uint64_t FileOffset, uint64_t FileSize, + uint32_t MaxProt, uint32_t InitProt, + uint32_t NumSections, uint32_t Flags) { + outs() << " ('segment_name', '"; + outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n"; + outs() << " ('vm_addr', " << VMAddr << ")\n"; + outs() << " ('vm_size', " << VMSize << ")\n"; + outs() << " ('file_offset', " << FileOffset << ")\n"; + outs() << " ('file_size', " << FileSize << ")\n"; + outs() << " ('maxprot', " << MaxProt << ")\n"; + outs() << " ('initprot', " << InitProt << ")\n"; + outs() << " ('num_sections', " << NumSections << ")\n"; + outs() << " ('flags', " << Flags << ")\n"; +} + +static int DumpSectionData(MachOObject &Obj, unsigned Index, StringRef Name, + StringRef SegmentName, uint64_t Address, + uint64_t Size, uint32_t Offset, + uint32_t Align, uint32_t RelocationTableOffset, + uint32_t NumRelocationTableEntries, + uint32_t Flags, uint32_t Reserved1, + uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) { + outs() << " # Section " << Index << "\n"; + outs() << " (('section_name', '"; + outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n"; + outs() << " ('segment_name', '"; + outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n"; + outs() << " ('address', " << Address << ")\n"; + outs() << " ('size', " << Size << ")\n"; + outs() << " ('offset', " << Offset << ")\n"; + outs() << " ('alignment', " << Align << ")\n"; + outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n"; + outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n"; + outs() << " ('flags', " << format("0x%x", Flags) << ")\n"; + outs() << " ('reserved1', " << Reserved1 << ")\n"; + outs() << " ('reserved2', " << Reserved2 << ")\n"; + if (Reserved3 != ~0ULL) + outs() << " ('reserved3', " << Reserved3 << ")\n"; + outs() << " ),\n"; + + // Dump the relocation entries. + int Res = 0; + outs() << " ('_relocations', [\n"; + for (unsigned i = 0; i != NumRelocationTableEntries; ++i) { + InMemoryStruct RE; + Obj.ReadRelocationEntry(RelocationTableOffset, i, RE); + if (!RE) { + Res = Error("unable to read relocation table entry '" + Twine(i) + "'"); + break; + } + + outs() << " # Relocation " << i << "\n"; + outs() << " (('word-0', " << format("0x%x", RE->Word0) << "),\n"; + outs() << " ('word-1', " << format("0x%x", RE->Word1) << ")),\n"; + } + outs() << " ])\n"; + + // Dump the section data, if requested. + if (ShowSectionData) { + outs() << " ('_section_data', '"; + StringRef Data = Obj.getData(Offset, Size); + for (unsigned i = 0; i != Data.size(); ++i) { + if (i && (i % 4) == 0) + outs() << ' '; + outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true); + outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true); + } + outs() << "')\n"; + } + + return Res; +} + +static int DumpSegmentCommand(MachOObject &Obj, + const MachOObject::LoadCommandInfo &LCI) { + InMemoryStruct SLC; + Obj.ReadSegmentLoadCommand(LCI, SLC); + if (!SLC) + return Error("unable to read segment load command"); + + DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, + SLC->VMSize, SLC->FileOffset, SLC->FileSize, + SLC->MaxVMProtection, SLC->InitialVMProtection, + SLC->NumSections, SLC->Flags); + + // Dump the sections. + int Res = 0; + outs() << " ('sections', [\n"; + for (unsigned i = 0; i != SLC->NumSections; ++i) { + InMemoryStruct Sect; + Obj.ReadSection(LCI, i, Sect); + if (!SLC) { + Res = Error("unable to read section '" + Twine(i) + "'"); + break; + } + + if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16), + StringRef(Sect->SegmentName, 16), Sect->Address, + Sect->Size, Sect->Offset, Sect->Align, + Sect->RelocationTableOffset, + Sect->NumRelocationTableEntries, Sect->Flags, + Sect->Reserved1, Sect->Reserved2))) + break; + } + outs() << " ])\n"; + + return Res; +} + +static int DumpSegment64Command(MachOObject &Obj, + const MachOObject::LoadCommandInfo &LCI) { + InMemoryStruct SLC; + Obj.ReadSegment64LoadCommand(LCI, SLC); + if (!SLC) + return Error("unable to read segment load command"); + + DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, + SLC->VMSize, SLC->FileOffset, SLC->FileSize, + SLC->MaxVMProtection, SLC->InitialVMProtection, + SLC->NumSections, SLC->Flags); + + // Dump the sections. + int Res = 0; + outs() << " ('sections', [\n"; + for (unsigned i = 0; i != SLC->NumSections; ++i) { + InMemoryStruct Sect; + Obj.ReadSection64(LCI, i, Sect); + if (!SLC) { + Res = Error("unable to read section '" + Twine(i) + "'"); + break; + } + + if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16), + StringRef(Sect->SegmentName, 16), Sect->Address, + Sect->Size, Sect->Offset, Sect->Align, + Sect->RelocationTableOffset, + Sect->NumRelocationTableEntries, Sect->Flags, + Sect->Reserved1, Sect->Reserved2, + Sect->Reserved3))) + break; + } + outs() << " ])\n"; + + return 0; +} + +static void DumpSymbolTableEntryData(MachOObject &Obj, + unsigned Index, uint32_t StringIndex, + uint8_t Type, uint8_t SectionIndex, + uint16_t Flags, uint64_t Value) { + outs() << " # Symbol " << Index << "\n"; + outs() << " (('n_strx', " << StringIndex << ")\n"; + outs() << " ('n_type', " << format("0x%x", Type) << ")\n"; + outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n"; + outs() << " ('n_desc', " << Flags << ")\n"; + outs() << " ('n_value', " << Value << ")\n"; + outs() << " ('_string', '" << Obj.getStringAtIndex(StringIndex) << "')\n"; + outs() << " ),\n"; +} + +static int DumpSymtabCommand(MachOObject &Obj, + const MachOObject::LoadCommandInfo &LCI) { + InMemoryStruct SLC; + Obj.ReadSymtabLoadCommand(LCI, SLC); + if (!SLC) + return Error("unable to read segment load command"); + + outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n"; + outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n"; + outs() << " ('stroff', " << SLC->StringTableOffset << ")\n"; + outs() << " ('strsize', " << SLC->StringTableSize << ")\n"; + + // Cache the string table data. + Obj.RegisterStringTable(*SLC); + + // Dump the string data. + outs() << " ('_string_data', '"; + outs().write_escaped(Obj.getStringTableData(), + /*UseHexEscapes=*/true) << "')\n"; + + // Dump the symbol table. + int Res = 0; + outs() << " ('_symbols', [\n"; + for (unsigned i = 0; i != SLC->NumSymbolTableEntries; ++i) { + if (Obj.is64Bit()) { + InMemoryStruct STE; + Obj.ReadSymbol64TableEntry(SLC->SymbolTableOffset, i, STE); + if (!STE) { + Res = Error("unable to read symbol: '" + Twine(i) + "'"); + break; + } + + DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type, + STE->SectionIndex, STE->Flags, STE->Value); + } else { + InMemoryStruct STE; + Obj.ReadSymbolTableEntry(SLC->SymbolTableOffset, i, STE); + if (!SLC) { + Res = Error("unable to read symbol: '" + Twine(i) + "'"); + break; + } + + DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type, + STE->SectionIndex, STE->Flags, STE->Value); + } + } + outs() << " ])\n"; + + return Res; +} + +static int DumpDysymtabCommand(MachOObject &Obj, + const MachOObject::LoadCommandInfo &LCI) { + InMemoryStruct DLC; + Obj.ReadDysymtabLoadCommand(LCI, DLC); + if (!DLC) + return Error("unable to read segment load command"); + + outs() << " ('ilocalsym', " << DLC->LocalSymbolsIndex << ")\n"; + outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n"; + outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n"; + outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n"; + outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n"; + outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n"; + outs() << " ('tocoff', " << DLC->TOCOffset << ")\n"; + outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n"; + outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n"; + outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n"; + outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n"; + outs() << " ('nextrefsyms', " + << DLC->NumReferencedSymbolTableEntries << ")\n"; + outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n"; + outs() << " ('nindirectsyms', " + << DLC->NumIndirectSymbolTableEntries << ")\n"; + outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n"; + outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n"; + outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n"; + outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n"; + + // Dump the indirect symbol table. + int Res = 0; + outs() << " ('_indirect_symbols', [\n"; + for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) { + InMemoryStruct ISTE; + Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE); + if (!ISTE) { + Res = Error("unable to read segment load command"); + break; + } + + outs() << " # Indirect Symbol " << i << "\n"; + outs() << " (('symbol_index', " + << format("0x%x", ISTE->Index) << "),),\n"; + } + outs() << " ])\n"; + + return Res; +} + +static int DumpLinkeditDataCommand(MachOObject &Obj, + const MachOObject::LoadCommandInfo &LCI) { + InMemoryStruct LLC; + Obj.ReadLinkeditDataLoadCommand(LCI, LLC); + if (!LLC) + return Error("unable to read segment load command"); + + outs() << " ('dataoff', " << LLC->DataOffset << ")\n" + << " ('datasize', " << LLC->DataSize << ")\n" + << " ('_addresses', [\n"; + + SmallVector Addresses; + Obj.ReadULEB128s(LLC->DataOffset, Addresses); + for (unsigned i = 0, e = Addresses.size(); i != e; ++i) + outs() << " # Address " << i << '\n' + << " ('address', " << format("0x%x", Addresses[i]) << "),\n"; + + outs() << " ])\n"; + + return 0; +} + + +static int DumpLoadCommand(MachOObject &Obj, unsigned Index) { + const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index); + int Res = 0; + + outs() << " # Load Command " << Index << "\n" + << " (('command', " << LCI.Command.Type << ")\n" + << " ('size', " << LCI.Command.Size << ")\n"; + switch (LCI.Command.Type) { + case macho::LCT_Segment: + Res = DumpSegmentCommand(Obj, LCI); + break; + case macho::LCT_Segment64: + Res = DumpSegment64Command(Obj, LCI); + break; + case macho::LCT_Symtab: + Res = DumpSymtabCommand(Obj, LCI); + break; + case macho::LCT_Dysymtab: + Res = DumpDysymtabCommand(Obj, LCI); + break; + case macho::LCT_CodeSignature: + case macho::LCT_SegmentSplitInfo: + case macho::LCT_FunctionStarts: + Res = DumpLinkeditDataCommand(Obj, LCI); + break; + default: + Warning("unknown load command: " + Twine(LCI.Command.Type)); + break; + } + outs() << " ),\n"; + + return Res; +} + +int main(int argc, char **argv) { + ProgramName = argv[0]; + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); + + // Load the input file. + std::string ErrorStr; + OwningPtr InputBuffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer)) + return Error("unable to read input: '" + ec.message() + "'"); + + // Construct the Mach-O wrapper object. + OwningPtr InputObject( + MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr)); + if (!InputObject) + return Error("unable to load object: '" + ErrorStr + "'"); + + // Print the header + InputObject->printHeader(outs()); + + // Print the load commands. + int Res = 0; + outs() << "('load_commands', [\n"; + for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i) + if ((Res = DumpLoadCommand(*InputObject, i))) + break; + outs() << "])\n"; + + return Res; +} diff --git a/contrib/llvm/tools/opt/AnalysisWrappers.cpp b/contrib/llvm/tools/opt/AnalysisWrappers.cpp new file mode 100644 index 000000000..a2b57bb3e --- /dev/null +++ b/contrib/llvm/tools/opt/AnalysisWrappers.cpp @@ -0,0 +1,94 @@ +//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines pass wrappers around LLVM analyses that don't make sense to +// be passes. It provides a nice standard pass interface to these classes so +// that they can be printed out by analyze. +// +// These classes are separated out of analyze.cpp so that it is more clear which +// code is the integral part of the analyze tool, and which part of the code is +// just making it so more passes are available. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/Support/CallSite.h" +#include "llvm/Analysis/CallGraph.h" +#include "llvm/Support/raw_ostream.h" +using namespace llvm; + +namespace { + /// ExternalFunctionsPassedConstants - This pass prints out call sites to + /// external functions that are called with constant arguments. This can be + /// useful when looking for standard library functions we should constant fold + /// or handle in alias analyses. + struct ExternalFunctionsPassedConstants : public ModulePass { + static char ID; // Pass ID, replacement for typeid + ExternalFunctionsPassedConstants() : ModulePass(ID) {} + virtual bool runOnModule(Module &M) { + for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { + if (!I->isDeclaration()) continue; + + bool PrintedFn = false; + for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); + UI != E; ++UI) { + Instruction *User = dyn_cast(*UI); + if (!User) continue; + + CallSite CS(cast(User)); + if (!CS) continue; + + for (CallSite::arg_iterator AI = CS.arg_begin(), + E = CS.arg_end(); AI != E; ++AI) { + if (!isa(*AI)) continue; + + if (!PrintedFn) { + errs() << "Function '" << I->getName() << "':\n"; + PrintedFn = true; + } + errs() << *User; + break; + } + } + } + + return false; + } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } + }; +} + +char ExternalFunctionsPassedConstants::ID = 0; +static RegisterPass + P1("print-externalfnconstants", + "Print external fn callsites passed constants"); + +namespace { + struct CallGraphPrinter : public ModulePass { + static char ID; // Pass ID, replacement for typeid + CallGraphPrinter() : ModulePass(ID) {} + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequiredTransitive(); + } + virtual bool runOnModule(Module &M) { + getAnalysis().print(errs(), &M); + return false; + } + }; +} + +char CallGraphPrinter::ID = 0; +static RegisterPass + P2("print-callgraph", "Print a call graph"); diff --git a/contrib/llvm/tools/opt/CMakeLists.txt b/contrib/llvm/tools/opt/CMakeLists.txt new file mode 100644 index 000000000..0570d0e04 --- /dev/null +++ b/contrib/llvm/tools/opt/CMakeLists.txt @@ -0,0 +1,8 @@ +set(LLVM_LINK_COMPONENTS bitreader asmparser bitwriter instrumentation scalaropts ipo) + +add_llvm_tool(opt + AnalysisWrappers.cpp + GraphPrinters.cpp + PrintSCC.cpp + opt.cpp + ) diff --git a/contrib/llvm/tools/opt/GraphPrinters.cpp b/contrib/llvm/tools/opt/GraphPrinters.cpp new file mode 100644 index 000000000..30361f501 --- /dev/null +++ b/contrib/llvm/tools/opt/GraphPrinters.cpp @@ -0,0 +1,118 @@ +//===- GraphPrinters.cpp - DOT printers for various graph types -----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines several printers for various different types of graphs used +// by the LLVM infrastructure. It uses the generic graph interface to convert +// the graph into a .dot graph. These graphs can then be processed with the +// "dot" tool to convert them to postscript or some other suitable format. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/GraphWriter.h" +#include "llvm/Pass.h" +#include "llvm/Value.h" +#include "llvm/Analysis/CallGraph.h" +#include "llvm/Analysis/Dominators.h" +#include "llvm/Support/ToolOutputFile.h" +using namespace llvm; + +template +static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName, + const GraphType >) { + std::string Filename = GraphName + ".dot"; + O << "Writing '" << Filename << "'..."; + std::string ErrInfo; + tool_output_file F(Filename.c_str(), ErrInfo); + + if (ErrInfo.empty()) { + WriteGraph(F.os(), GT); + F.os().close(); + if (!F.os().has_error()) { + O << "\n"; + F.keep(); + return; + } + } + O << " error opening file for writing!\n"; + F.os().clear_error(); +} + + +//===----------------------------------------------------------------------===// +// Call Graph Printer +//===----------------------------------------------------------------------===// + +namespace llvm { + template<> + struct DOTGraphTraits : public DefaultDOTGraphTraits { + + DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} + + static std::string getGraphName(CallGraph *F) { + return "Call Graph"; + } + + static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) { + if (Node->getFunction()) + return ((Value*)Node->getFunction())->getName(); + return "external node"; + } + }; +} + + +namespace { + struct CallGraphPrinter : public ModulePass { + static char ID; // Pass ID, replacement for typeid + CallGraphPrinter() : ModulePass(ID) {} + + virtual bool runOnModule(Module &M) { + WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis()); + return false; + } + + void print(raw_ostream &OS, const llvm::Module*) const {} + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired(); + AU.setPreservesAll(); + } + }; +} + +char CallGraphPrinter::ID = 0; +static RegisterPass P2("dot-callgraph", + "Print Call Graph to 'dot' file"); + +//===----------------------------------------------------------------------===// +// DomInfoPrinter Pass +//===----------------------------------------------------------------------===// + +namespace { + class DomInfoPrinter : public FunctionPass { + public: + static char ID; // Pass identification, replacement for typeid + DomInfoPrinter() : FunctionPass(ID) {} + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); + + } + + virtual bool runOnFunction(Function &F) { + getAnalysis().dump(); + return false; + } + }; +} + +char DomInfoPrinter::ID = 0; +static RegisterPass +DIP("print-dom-info", "Dominator Info Printer", true, true); diff --git a/contrib/llvm/tools/opt/Makefile b/contrib/llvm/tools/opt/Makefile new file mode 100644 index 000000000..726cad871 --- /dev/null +++ b/contrib/llvm/tools/opt/Makefile @@ -0,0 +1,14 @@ +##===- tools/opt/Makefile ----------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../.. +TOOLNAME = opt + +LINK_COMPONENTS := bitreader bitwriter asmparser instrumentation scalaropts ipo + +include $(LEVEL)/Makefile.common diff --git a/contrib/llvm/tools/opt/PrintSCC.cpp b/contrib/llvm/tools/opt/PrintSCC.cpp new file mode 100644 index 000000000..533f49ec2 --- /dev/null +++ b/contrib/llvm/tools/opt/PrintSCC.cpp @@ -0,0 +1,112 @@ +//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file provides passes to print out SCCs in a CFG or a CallGraph. +// Normally, you would not use these passes; instead, you would use the +// scc_iterator directly to enumerate SCCs and process them in some way. These +// passes serve three purposes: +// +// (1) As a reference for how to use the scc_iterator. +// (2) To print out the SCCs for a CFG or a CallGraph: +// analyze -print-cfg-sccs to print the SCCs in each CFG of a module. +// analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size. +// analyze -print-cfg-sccs -debug > /dev/null to watch the algorithm in action. +// +// and similarly: +// analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph +// +// (3) To test the scc_iterator. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Pass.h" +#include "llvm/Module.h" +#include "llvm/Analysis/CallGraph.h" +#include "llvm/Support/CFG.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/ADT/SCCIterator.h" +using namespace llvm; + +namespace { + struct CFGSCC : public FunctionPass { + static char ID; // Pass identification, replacement for typeid + CFGSCC() : FunctionPass(ID) {} + bool runOnFunction(Function& func); + + void print(raw_ostream &O, const Module* = 0) const { } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } + }; + + struct CallGraphSCC : public ModulePass { + static char ID; // Pass identification, replacement for typeid + CallGraphSCC() : ModulePass(ID) {} + + // run - Print out SCCs in the call graph for the specified module. + bool runOnModule(Module &M); + + void print(raw_ostream &O, const Module* = 0) const { } + + // getAnalysisUsage - This pass requires the CallGraph. + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); + } + }; +} + +char CFGSCC::ID = 0; +static RegisterPass +Y("print-cfg-sccs", "Print SCCs of each function CFG"); + +char CallGraphSCC::ID = 0; +static RegisterPass +Z("print-callgraph-sccs", "Print SCCs of the Call Graph"); + +bool CFGSCC::runOnFunction(Function &F) { + unsigned sccNum = 0; + errs() << "SCCs for Function " << F.getName() << " in PostOrder:"; + for (scc_iterator SCCI = scc_begin(&F), + E = scc_end(&F); SCCI != E; ++SCCI) { + std::vector &nextSCC = *SCCI; + errs() << "\nSCC #" << ++sccNum << " : "; + for (std::vector::const_iterator I = nextSCC.begin(), + E = nextSCC.end(); I != E; ++I) + errs() << (*I)->getName() << ", "; + if (nextSCC.size() == 1 && SCCI.hasLoop()) + errs() << " (Has self-loop)."; + } + errs() << "\n"; + + return true; +} + + +// run - Print out SCCs in the call graph for the specified module. +bool CallGraphSCC::runOnModule(Module &M) { + CallGraphNode* rootNode = getAnalysis().getRoot(); + unsigned sccNum = 0; + errs() << "SCCs for the program in PostOrder:"; + for (scc_iterator SCCI = scc_begin(rootNode), + E = scc_end(rootNode); SCCI != E; ++SCCI) { + const std::vector &nextSCC = *SCCI; + errs() << "\nSCC #" << ++sccNum << " : "; + for (std::vector::const_iterator I = nextSCC.begin(), + E = nextSCC.end(); I != E; ++I) + errs() << ((*I)->getFunction() ? (*I)->getFunction()->getNameStr() + : std::string("external node")) << ", "; + if (nextSCC.size() == 1 && SCCI.hasLoop()) + errs() << " (Has self-loop)."; + } + errs() << "\n"; + + return true; +} diff --git a/contrib/llvm/tools/opt/opt.cpp b/contrib/llvm/tools/opt/opt.cpp new file mode 100644 index 000000000..ffd2c2173 --- /dev/null +++ b/contrib/llvm/tools/opt/opt.cpp @@ -0,0 +1,713 @@ +//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Optimizations may be specified an arbitrary number of times on the command +// line, They are run in the order specified. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/CallGraphSCCPass.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Assembly/PrintModulePass.h" +#include "llvm/Analysis/DebugInfo.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Analysis/LoopPass.h" +#include "llvm/Analysis/RegionPass.h" +#include "llvm/Analysis/CallGraph.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetLibraryInfo.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Support/PassNameParser.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PluginLoader.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/LinkAllPasses.h" +#include "llvm/LinkAllVMCore.h" +#include "llvm/Transforms/IPO/PassManagerBuilder.h" +#include +#include +using namespace llvm; + +// The OptimizationList is automatically populated with registered Passes by the +// PassNameParser. +// +static cl::list +PassList(cl::desc("Optimizations available:")); + +// Other command line options... +// +static cl::opt +InputFilename(cl::Positional, cl::desc(""), + cl::init("-"), cl::value_desc("filename")); + +static cl::opt +OutputFilename("o", cl::desc("Override output filename"), + cl::value_desc("filename")); + +static cl::opt +Force("f", cl::desc("Enable binary output on terminals")); + +static cl::opt +PrintEachXForm("p", cl::desc("Print module after each transformation")); + +static cl::opt +NoOutput("disable-output", + cl::desc("Do not write result bitcode file"), cl::Hidden); + +static cl::opt +OutputAssembly("S", cl::desc("Write output as LLVM assembly")); + +static cl::opt +NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden); + +static cl::opt +VerifyEach("verify-each", cl::desc("Verify after each transform")); + +static cl::opt +StripDebug("strip-debug", + cl::desc("Strip debugger symbol info from translation unit")); + +static cl::opt +DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); + +static cl::opt +DisableOptimizations("disable-opt", + cl::desc("Do not run any optimization passes")); + +static cl::opt +DisableInternalize("disable-internalize", + cl::desc("Do not mark all symbols as internal")); + +static cl::opt +StandardCompileOpts("std-compile-opts", + cl::desc("Include the standard compile time optimizations")); + +static cl::opt +StandardLinkOpts("std-link-opts", + cl::desc("Include the standard link time optimizations")); + +static cl::opt +OptLevelO1("O1", + cl::desc("Optimization level 1. Similar to llvm-gcc -O1")); + +static cl::opt +OptLevelO2("O2", + cl::desc("Optimization level 2. Similar to llvm-gcc -O2")); + +static cl::opt +OptLevelO3("O3", + cl::desc("Optimization level 3. Similar to llvm-gcc -O3")); + +static cl::opt +UnitAtATime("funit-at-a-time", + cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"), + cl::init(true)); + +static cl::opt +DisableSimplifyLibCalls("disable-simplify-libcalls", + cl::desc("Disable simplify-libcalls")); + +static cl::opt +Quiet("q", cl::desc("Obsolete option"), cl::Hidden); + +static cl::alias +QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); + +static cl::opt +AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); + +static cl::opt +PrintBreakpoints("print-breakpoints-for-testing", + cl::desc("Print select breakpoints location for testing")); + +static cl::opt +DefaultDataLayout("default-data-layout", + cl::desc("data layout string to use if not specified by module"), + cl::value_desc("layout-string"), cl::init("")); + +// ---------- Define Printers for module and function passes ------------ +namespace { + +struct CallGraphSCCPassPrinter : public CallGraphSCCPass { + static char ID; + const PassInfo *PassToPrint; + raw_ostream &Out; + std::string PassName; + + CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) : + CallGraphSCCPass(ID), PassToPrint(PI), Out(out) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "CallGraphSCCPass Printer: " + PassToPrintName; + } + + virtual bool runOnSCC(CallGraphSCC &SCC) { + if (!Quiet) + Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + + // Get and print pass... + for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { + Function *F = (*I)->getFunction(); + if (F) + getAnalysisID(PassToPrint->getTypeInfo()).print(Out, + F->getParent()); + } + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char CallGraphSCCPassPrinter::ID = 0; + +struct ModulePassPrinter : public ModulePass { + static char ID; + const PassInfo *PassToPrint; + raw_ostream &Out; + std::string PassName; + + ModulePassPrinter(const PassInfo *PI, raw_ostream &out) + : ModulePass(ID), PassToPrint(PI), Out(out) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "ModulePass Printer: " + PassToPrintName; + } + + virtual bool runOnModule(Module &M) { + if (!Quiet) + Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(Out, &M); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char ModulePassPrinter::ID = 0; +struct FunctionPassPrinter : public FunctionPass { + const PassInfo *PassToPrint; + raw_ostream &Out; + static char ID; + std::string PassName; + + FunctionPassPrinter(const PassInfo *PI, raw_ostream &out) + : FunctionPass(ID), PassToPrint(PI), Out(out) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "FunctionPass Printer: " + PassToPrintName; + } + + virtual bool runOnFunction(Function &F) { + if (!Quiet) + Out << "Printing analysis '" << PassToPrint->getPassName() + << "' for function '" << F.getName() << "':\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(Out, + F.getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char FunctionPassPrinter::ID = 0; + +struct LoopPassPrinter : public LoopPass { + static char ID; + const PassInfo *PassToPrint; + raw_ostream &Out; + std::string PassName; + + LoopPassPrinter(const PassInfo *PI, raw_ostream &out) : + LoopPass(ID), PassToPrint(PI), Out(out) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "LoopPass Printer: " + PassToPrintName; + } + + + virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { + if (!Quiet) + Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(Out, + L->getHeader()->getParent()->getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char LoopPassPrinter::ID = 0; + +struct RegionPassPrinter : public RegionPass { + static char ID; + const PassInfo *PassToPrint; + raw_ostream &Out; + std::string PassName; + + RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID), + PassToPrint(PI), Out(out) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "RegionPass Printer: " + PassToPrintName; + } + + virtual bool runOnRegion(Region *R, RGPassManager &RGM) { + if (!Quiet) { + Out << "Printing analysis '" << PassToPrint->getPassName() << "' for " + << "region: '" << R->getNameStr() << "' in function '" + << R->getEntry()->getParent()->getNameStr() << "':\n"; + } + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(Out, + R->getEntry()->getParent()->getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char RegionPassPrinter::ID = 0; + +struct BasicBlockPassPrinter : public BasicBlockPass { + const PassInfo *PassToPrint; + raw_ostream &Out; + static char ID; + std::string PassName; + + BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out) + : BasicBlockPass(ID), PassToPrint(PI), Out(out) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "BasicBlockPass Printer: " + PassToPrintName; + } + + virtual bool runOnBasicBlock(BasicBlock &BB) { + if (!Quiet) + Out << "Printing Analysis info for BasicBlock '" << BB.getName() + << "': Pass " << PassToPrint->getPassName() << ":\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(Out, + BB.getParent()->getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char BasicBlockPassPrinter::ID = 0; + +struct BreakpointPrinter : public ModulePass { + raw_ostream &Out; + static char ID; + + BreakpointPrinter(raw_ostream &out) + : ModulePass(ID), Out(out) { + } + + void getContextName(DIDescriptor Context, std::string &N) { + if (Context.isNameSpace()) { + DINameSpace NS(Context); + if (!NS.getName().empty()) { + getContextName(NS.getContext(), N); + N = N + NS.getName().str() + "::"; + } + } else if (Context.isType()) { + DIType TY(Context); + if (!TY.getName().empty()) { + getContextName(TY.getContext(), N); + N = N + TY.getName().str() + "::"; + } + } + } + + virtual bool runOnModule(Module &M) { + StringSet<> Processed; + if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) + for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { + std::string Name; + DISubprogram SP(NMD->getOperand(i)); + if (SP.Verify()) + getContextName(SP.getContext(), Name); + Name = Name + SP.getDisplayName().str(); + if (!Name.empty() && Processed.insert(Name)) { + Out << Name << "\n"; + } + } + return false; + } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } +}; + +} // anonymous namespace + +char BreakpointPrinter::ID = 0; + +static inline void addPass(PassManagerBase &PM, Pass *P) { + // Add the pass to the pass manager... + PM.add(P); + + // If we are verifying all of the intermediate steps, add the verifier... + if (VerifyEach) PM.add(createVerifierPass()); +} + +/// AddOptimizationPasses - This routine adds optimization passes +/// based on selected optimization level, OptLevel. This routine +/// duplicates llvm-gcc behaviour. +/// +/// OptLevel - Optimization Level +static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM, + unsigned OptLevel) { + PassManagerBuilder Builder; + Builder.OptLevel = OptLevel; + + if (DisableInline) { + // No inlining pass + } else if (OptLevel > 1) { + unsigned Threshold = 225; + if (OptLevel > 2) + Threshold = 275; + Builder.Inliner = createFunctionInliningPass(Threshold); + } else { + Builder.Inliner = createAlwaysInlinerPass(); + } + Builder.DisableUnitAtATime = !UnitAtATime; + Builder.DisableUnrollLoops = OptLevel == 0; + Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls; + + Builder.populateFunctionPassManager(FPM); + Builder.populateModulePassManager(MPM); +} + +static void AddStandardCompilePasses(PassManagerBase &PM) { + PM.add(createVerifierPass()); // Verify that input is correct + + // If the -strip-debug command line option was specified, do it. + if (StripDebug) + addPass(PM, createStripSymbolsPass(true)); + + if (DisableOptimizations) return; + + // -std-compile-opts adds the same module passes as -O3. + PassManagerBuilder Builder; + if (!DisableInline) + Builder.Inliner = createFunctionInliningPass(); + Builder.OptLevel = 3; + Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls; + Builder.populateModulePassManager(PM); +} + +static void AddStandardLinkPasses(PassManagerBase &PM) { + PM.add(createVerifierPass()); // Verify that input is correct + + // If the -strip-debug command line option was specified, do it. + if (StripDebug) + addPass(PM, createStripSymbolsPass(true)); + + if (DisableOptimizations) return; + + PassManagerBuilder Builder; + Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize, + /*RunInliner=*/ !DisableInline); +} + + +//===----------------------------------------------------------------------===// +// main for opt +// +int main(int argc, char **argv) { + sys::PrintStackTraceOnErrorSignal(); + llvm::PrettyStackTraceProgram X(argc, argv); + + // Enable debug stream buffering. + EnableDebugBuffering = true; + + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + LLVMContext &Context = getGlobalContext(); + + // Initialize passes + PassRegistry &Registry = *PassRegistry::getPassRegistry(); + initializeCore(Registry); + initializeScalarOpts(Registry); + initializeIPO(Registry); + initializeAnalysis(Registry); + initializeIPA(Registry); + initializeTransformUtils(Registry); + initializeInstCombine(Registry); + initializeInstrumentation(Registry); + initializeTarget(Registry); + + cl::ParseCommandLineOptions(argc, argv, + "llvm .bc -> .bc modular optimizer and analysis printer\n"); + + if (AnalyzeOnly && NoOutput) { + errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; + return 1; + } + + // Allocate a full target machine description only if necessary. + // FIXME: The choice of target should be controllable on the command line. + std::auto_ptr target; + + SMDiagnostic Err; + + // Load the input module... + std::auto_ptr M; + M.reset(ParseIRFile(InputFilename, Err, Context)); + + if (M.get() == 0) { + Err.Print(argv[0], errs()); + return 1; + } + + // Figure out what stream we are supposed to write to... + OwningPtr Out; + if (NoOutput) { + if (!OutputFilename.empty()) + errs() << "WARNING: The -o (output filename) option is ignored when\n" + "the --disable-output option is used.\n"; + } else { + // Default to standard output. + if (OutputFilename.empty()) + OutputFilename = "-"; + + std::string ErrorInfo; + Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary)); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + return 1; + } + } + + // If the output is set to be emitted to standard out, and standard out is a + // console, print out a warning message and refuse to do it. We don't + // impress anyone by spewing tons of binary goo to a terminal. + if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) + if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) + NoOutput = true; + + // Create a PassManager to hold and optimize the collection of passes we are + // about to build. + // + PassManager Passes; + + // Add an appropriate TargetLibraryInfo pass for the module's triple. + TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple())); + + // The -disable-simplify-libcalls flag actually disables all builtin optzns. + if (DisableSimplifyLibCalls) + TLI->disableAllFunctions(); + Passes.add(TLI); + + // Add an appropriate TargetData instance for this module. + TargetData *TD = 0; + const std::string &ModuleDataLayout = M.get()->getDataLayout(); + if (!ModuleDataLayout.empty()) + TD = new TargetData(ModuleDataLayout); + else if (!DefaultDataLayout.empty()) + TD = new TargetData(DefaultDataLayout); + + if (TD) + Passes.add(TD); + + OwningPtr FPasses; + if (OptLevelO1 || OptLevelO2 || OptLevelO3) { + FPasses.reset(new FunctionPassManager(M.get())); + if (TD) + FPasses->add(new TargetData(*TD)); + } + + if (PrintBreakpoints) { + // Default to standard output. + if (!Out) { + if (OutputFilename.empty()) + OutputFilename = "-"; + + std::string ErrorInfo; + Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary)); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + return 1; + } + } + Passes.add(new BreakpointPrinter(Out->os())); + NoOutput = true; + } + + // If the -strip-debug command line option was specified, add it. If + // -std-compile-opts was also specified, it will handle StripDebug. + if (StripDebug && !StandardCompileOpts) + addPass(Passes, createStripSymbolsPass(true)); + + // Create a new optimization pass for each one specified on the command line + for (unsigned i = 0; i < PassList.size(); ++i) { + // Check to see if -std-compile-opts was specified before this option. If + // so, handle it. + if (StandardCompileOpts && + StandardCompileOpts.getPosition() < PassList.getPosition(i)) { + AddStandardCompilePasses(Passes); + StandardCompileOpts = false; + } + + if (StandardLinkOpts && + StandardLinkOpts.getPosition() < PassList.getPosition(i)) { + AddStandardLinkPasses(Passes); + StandardLinkOpts = false; + } + + if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { + AddOptimizationPasses(Passes, *FPasses, 1); + OptLevelO1 = false; + } + + if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { + AddOptimizationPasses(Passes, *FPasses, 2); + OptLevelO2 = false; + } + + if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { + AddOptimizationPasses(Passes, *FPasses, 3); + OptLevelO3 = false; + } + + const PassInfo *PassInf = PassList[i]; + Pass *P = 0; + if (PassInf->getNormalCtor()) + P = PassInf->getNormalCtor()(); + else + errs() << argv[0] << ": cannot create pass: " + << PassInf->getPassName() << "\n"; + if (P) { + PassKind Kind = P->getPassKind(); + addPass(Passes, P); + + if (AnalyzeOnly) { + switch (Kind) { + case PT_BasicBlock: + Passes.add(new BasicBlockPassPrinter(PassInf, Out->os())); + break; + case PT_Region: + Passes.add(new RegionPassPrinter(PassInf, Out->os())); + break; + case PT_Loop: + Passes.add(new LoopPassPrinter(PassInf, Out->os())); + break; + case PT_Function: + Passes.add(new FunctionPassPrinter(PassInf, Out->os())); + break; + case PT_CallGraphSCC: + Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os())); + break; + default: + Passes.add(new ModulePassPrinter(PassInf, Out->os())); + break; + } + } + } + + if (PrintEachXForm) + Passes.add(createPrintModulePass(&errs())); + } + + // If -std-compile-opts was specified at the end of the pass list, add them. + if (StandardCompileOpts) { + AddStandardCompilePasses(Passes); + StandardCompileOpts = false; + } + + if (StandardLinkOpts) { + AddStandardLinkPasses(Passes); + StandardLinkOpts = false; + } + + if (OptLevelO1) + AddOptimizationPasses(Passes, *FPasses, 1); + + if (OptLevelO2) + AddOptimizationPasses(Passes, *FPasses, 2); + + if (OptLevelO3) + AddOptimizationPasses(Passes, *FPasses, 3); + + if (OptLevelO1 || OptLevelO2 || OptLevelO3) { + FPasses->doInitialization(); + for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F) + FPasses->run(*F); + FPasses->doFinalization(); + } + + // Check that the module is well formed on completion of optimization + if (!NoVerify && !VerifyEach) + Passes.add(createVerifierPass()); + + // Write bitcode or assembly to the output as the last step... + if (!NoOutput && !AnalyzeOnly) { + if (OutputAssembly) + Passes.add(createPrintModulePass(&Out->os())); + else + Passes.add(createBitcodeWriterPass(Out->os())); + } + + // Before executing passes, print the final values of the LLVM options. + cl::PrintOptionValues(); + + // Now that we have all of the passes ready, run them. + Passes.run(*M.get()); + + // Declare success. + if (!NoOutput || PrintBreakpoints) + Out->keep(); + + return 0; +} diff --git a/lib/clang/Makefile b/lib/clang/Makefile index 8f7d18a31..e0a6201e0 100644 --- a/lib/clang/Makefile +++ b/lib/clang/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + .if !make(install) SUBDIR= libclanganalysis \ libclangarcmigrate \ @@ -60,6 +62,19 @@ SUBDIR= libclanganalysis \ libllvmx86info \ libllvmx86instprinter \ libllvmx86utils + +.if ${MK_CLANG_EXTRAS} != "no" +SUBDIR+=libllvmarchive \ + libllvmdebuginfo \ + libllvmexecutionengine \ + libllvminterpreter \ + libllvmjit \ + libllvmlinker \ + libllvmmcdisassembler \ + libllvmmcjit \ + libllvmobject \ + libllvmruntimedyld +.endif .endif SUBDIR+= include diff --git a/lib/clang/libllvmanalysis/Makefile b/lib/clang/libllvmanalysis/Makefile index 2b0bccff9..301c39b16 100644 --- a/lib/clang/libllvmanalysis/Makefile +++ b/lib/clang/libllvmanalysis/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmanalysis SRCDIR= lib/Analysis @@ -55,6 +57,14 @@ SRCS= AliasAnalysis.cpp \ TypeBasedAliasAnalysis.cpp \ ValueTracking.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= BlockFrequencyInfo.cpp \ + LibCallSemantics.cpp \ + PathNumbering.cpp \ + PathProfileInfo.cpp \ + PathProfileVerifier.cpp +.endif + TGHDRS= Intrinsics .include "../clang.lib.mk" diff --git a/lib/clang/libllvmarchive/Makefile b/lib/clang/libllvmarchive/Makefile new file mode 100644 index 000000000..a28f2f510 --- /dev/null +++ b/lib/clang/libllvmarchive/Makefile @@ -0,0 +1,10 @@ +# $FreeBSD$ + +LIB= llvmarchive + +SRCDIR= lib/Archive +SRCS= Archive.cpp \ + ArchiveReader.cpp \ + ArchiveWriter.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmdebuginfo/Makefile b/lib/clang/libllvmdebuginfo/Makefile new file mode 100644 index 000000000..b7241573c --- /dev/null +++ b/lib/clang/libllvmdebuginfo/Makefile @@ -0,0 +1,17 @@ +# $FreeBSD$ + +LIB= llvmdebuginfo + +SRCDIR= lib/DebugInfo +SRCS= DIContext.cpp \ + DWARFAbbreviationDeclaration.cpp \ + DWARFCompileUnit.cpp \ + DWARFContext.cpp \ + DWARFDebugAbbrev.cpp \ + DWARFDebugArangeSet.cpp \ + DWARFDebugAranges.cpp \ + DWARFDebugInfoEntry.cpp \ + DWARFDebugLine.cpp \ + DWARFFormValue.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmexecutionengine/Makefile b/lib/clang/libllvmexecutionengine/Makefile new file mode 100644 index 000000000..c0dde71de --- /dev/null +++ b/lib/clang/libllvmexecutionengine/Makefile @@ -0,0 +1,10 @@ +# $FreeBSD$ + +LIB= llvmexecutionengine + +SRCDIR= lib/ExecutionEngine +SRCS= ExecutionEngine.cpp \ + ExecutionEngineBindings.cpp \ + TargetSelect.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvminterpreter/Makefile b/lib/clang/libllvminterpreter/Makefile new file mode 100644 index 000000000..6a1b45345 --- /dev/null +++ b/lib/clang/libllvminterpreter/Makefile @@ -0,0 +1,12 @@ +# $FreeBSD$ + +LIB= llvminterpreter + +SRCDIR= lib/ExecutionEngine/Interpreter +SRCS= Execution.cpp \ + ExternalFunctions.cpp \ + Interpreter.cpp \ + +TGHDRS= Intrinsics + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmipa/Makefile b/lib/clang/libllvmipa/Makefile index fc241dfc2..4caa0e5de 100644 --- a/lib/clang/libllvmipa/Makefile +++ b/lib/clang/libllvmipa/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmipa SRCDIR= lib/Analysis/IPA @@ -8,6 +10,10 @@ SRCS= CallGraph.cpp \ FindUsedTypes.cpp \ GlobalsModRef.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= IPA.cpp +.endif + TGHDRS= Intrinsics .include "../clang.lib.mk" diff --git a/lib/clang/libllvmipo/Makefile b/lib/clang/libllvmipo/Makefile index 42cc2fa93..a493532d6 100644 --- a/lib/clang/libllvmipo/Makefile +++ b/lib/clang/libllvmipo/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmipo SRCDIR= lib/Transforms/IPO @@ -23,6 +25,10 @@ SRCS= ArgumentPromotion.cpp \ StripDeadPrototypes.cpp \ StripSymbols.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= IPO.cpp +.endif + TGHDRS= Intrinsics .include "../clang.lib.mk" diff --git a/lib/clang/libllvmjit/Makefile b/lib/clang/libllvmjit/Makefile new file mode 100644 index 000000000..fc92fd07d --- /dev/null +++ b/lib/clang/libllvmjit/Makefile @@ -0,0 +1,14 @@ +# $FreeBSD$ + +LIB= llvmjit + +SRCDIR= lib/ExecutionEngine/JIT +SRCS= Intercept.cpp \ + JIT.cpp \ + JITDebugRegisterer.cpp \ + JITDwarfEmitter.cpp \ + JITEmitter.cpp \ + JITMemoryManager.cpp \ + OProfileJITEventListener.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmlinker/Makefile b/lib/clang/libllvmlinker/Makefile new file mode 100644 index 000000000..73f153b37 --- /dev/null +++ b/lib/clang/libllvmlinker/Makefile @@ -0,0 +1,11 @@ +# $FreeBSD$ + +LIB= llvmlinker + +SRCDIR= lib/Linker +SRCS= LinkArchives.cpp \ + LinkItems.cpp \ + LinkModules.cpp \ + Linker.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmmc/Makefile b/lib/clang/libllvmmc/Makefile index 5fef44011..9bb572390 100644 --- a/lib/clang/libllvmmc/Makefile +++ b/lib/clang/libllvmmc/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmmc SRCDIR= lib/MC @@ -43,4 +45,8 @@ SRCS= ELFObjectWriter.cpp \ WinCOFFObjectWriter.cpp \ WinCOFFStreamer.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= MCDisassembler.cpp +.endif + .include "../clang.lib.mk" diff --git a/lib/clang/libllvmmcdisassembler/Makefile b/lib/clang/libllvmmcdisassembler/Makefile new file mode 100644 index 000000000..dc5f29437 --- /dev/null +++ b/lib/clang/libllvmmcdisassembler/Makefile @@ -0,0 +1,12 @@ +# $FreeBSD$ + +LIB= llvmmcdisassembler + +SRCDIR= lib/MC/MCDisassembler +SRCS= Disassembler.cpp \ + EDDisassembler.cpp \ + EDInst.cpp \ + EDOperand.cpp \ + EDToken.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmmcjit/Makefile b/lib/clang/libllvmmcjit/Makefile new file mode 100644 index 000000000..58e252925 --- /dev/null +++ b/lib/clang/libllvmmcjit/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +LIB= llvmmcjit + +SRCDIR= lib/ExecutionEngine/MCJIT +SRCS= Intercept.cpp \ + MCJIT.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmobject/Makefile b/lib/clang/libllvmobject/Makefile new file mode 100644 index 000000000..d056dc7c1 --- /dev/null +++ b/lib/clang/libllvmobject/Makefile @@ -0,0 +1,16 @@ +# $FreeBSD$ + +LIB= llvmobject + +SRCDIR= lib/Object +SRCS= Archive.cpp \ + Binary.cpp \ + COFFObjectFile.cpp \ + ELFObjectFile.cpp \ + Error.cpp \ + MachOObject.cpp \ + MachOObjectFile.cpp \ + Object.cpp \ + ObjectFile.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmruntimedyld/Makefile b/lib/clang/libllvmruntimedyld/Makefile new file mode 100644 index 000000000..c17f03172 --- /dev/null +++ b/lib/clang/libllvmruntimedyld/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +LIB= llvmruntimedyld + +SRCDIR= lib/ExecutionEngine/RuntimeDyld +SRCS= RuntimeDyld.cpp \ + RuntimeDyldMachO.cpp + +.include "../clang.lib.mk" diff --git a/lib/clang/libllvmscalaropts/Makefile b/lib/clang/libllvmscalaropts/Makefile index c376b685d..20235958c 100644 --- a/lib/clang/libllvmscalaropts/Makefile +++ b/lib/clang/libllvmscalaropts/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmscalaropts SRCDIR= lib/Transforms/Scalar @@ -31,6 +33,13 @@ SRCS= ADCE.cpp \ Sink.cpp \ TailRecursionElimination.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= LoopInstSimplify.cpp \ + LowerAtomic.cpp \ + Reg2Mem.cpp \ + Scalar.cpp +.endif + TGHDRS= Intrinsics .include "../clang.lib.mk" diff --git a/lib/clang/libllvmsupport/Makefile b/lib/clang/libllvmsupport/Makefile index e1a16c462..fb9309f67 100644 --- a/lib/clang/libllvmsupport/Makefile +++ b/lib/clang/libllvmsupport/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmsupport SRCDIR= lib/Support @@ -67,4 +69,14 @@ SRCS= APFloat.cpp \ system_error.cpp LLVM_REQUIRES_RTTI= +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= BlockFrequency.cpp \ + BranchProbability.cpp \ + DataExtractor.cpp \ + Disassembler.cpp \ + FileUtilities.cpp \ + MemoryObject.cpp \ + SystemUtils.cpp +.endif + .include "../clang.lib.mk" diff --git a/lib/clang/libllvmtransformutils/Makefile b/lib/clang/libllvmtransformutils/Makefile index 18eecda5d..196978156 100644 --- a/lib/clang/libllvmtransformutils/Makefile +++ b/lib/clang/libllvmtransformutils/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + LIB= llvmtransformutils SRCDIR= lib/Transforms/Utils @@ -29,6 +31,11 @@ SRCS= AddrModeMatcher.cpp \ UnifyFunctionExitNodes.cpp \ ValueMapper.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= SimplifyInstructions.cpp \ + Utils.cpp +.endif + TGHDRS= Intrinsics .include "../clang.lib.mk" diff --git a/lib/clang/libllvmx86disassembler/Makefile b/lib/clang/libllvmx86disassembler/Makefile index 1d1b3b651..9b738007c 100644 --- a/lib/clang/libllvmx86disassembler/Makefile +++ b/lib/clang/libllvmx86disassembler/Makefile @@ -1,11 +1,17 @@ # $FreeBSD$ +.include + LIB= llvmx86disassembler SRCDIR= lib/Target/X86/Disassembler INCDIR= lib/Target/X86 SRCS= X86Disassembler.cpp +.if ${MK_CLANG_EXTRAS} != "no" +SRCS+= X86DisassemblerDecoder.c +.endif + TGHDRS= X86GenDisassemblerTables \ X86GenEDInfo \ X86GenInstrInfo \ diff --git a/share/man/man5/src.conf.5 b/share/man/man5/src.conf.5 index 544344c44..96cb37829 100644 --- a/share/man/man5/src.conf.5 +++ b/share/man/man5/src.conf.5 @@ -1,7 +1,7 @@ .\" DO NOT EDIT-- this file is automatically generated. .\" from FreeBSD: head/tools/build/options/makeman 221733 2011-05-10 13:01:11Z ru .\" $FreeBSD$ -.Dd June 17, 2011 +.Dd February 6, 2012 .Dt SRC.CONF 5 .Os .Sh NAME @@ -266,12 +266,21 @@ Set to not build the Clang C/C++ compiler. .Pp It is a default setting on arm/arm, arm/armeb, ia64/ia64, mips/mipsel, mips/mipseb, mips/mips64el, mips/mips64eb, mips/mipsn32eb, powerpc/powerpc64 and sparc64/sparc64. +When set, it also enforces the following options: +.Pp +.Bl -item -compact +.It +.Va WITHOUT_CLANG_EXTRAS +.El .It Va WITH_CLANG .\" from FreeBSD: head/tools/build/options/WITH_CLANG 221730 2011-05-10 11:14:40Z ru Set to build the Clang C/C++ compiler. .Pp It is a default setting on amd64/amd64, i386/i386, pc98/i386 and powerpc/powerpc. +.It Va WITH_CLANG_EXTRAS +.\" from FreeBSD: head/tools/build/options/WITH_CLANG_EXTRAS 231057 2012-02-05 23:56:22Z dim +Set to build additional clang and llvm tools, such as bugpoint. .It Va WITHOUT_CPP .\" from FreeBSD: head/tools/build/options/WITHOUT_CPP 156932 2006-03-21 07:50:50Z ru Set to not build diff --git a/share/mk/bsd.own.mk b/share/mk/bsd.own.mk index a8091acf8..5fdb00f5f 100644 --- a/share/mk/bsd.own.mk +++ b/share/mk/bsd.own.mk @@ -413,6 +413,7 @@ __DEFAULT_NO_OPTIONS = \ BIND_LIBS \ BIND_SIGCHASE \ BIND_XML \ + CLANG_EXTRAS \ HESIOD \ ICONV \ IDEA \ @@ -516,6 +517,10 @@ MK_SOURCELESS_UCODE:= no MK_ZFS:= no .endif +.if ${MK_CLANG} == "no" +MK_CLANG_EXTRAS:= no +.endif + .if ${MK_CRYPT} == "no" MK_OPENSSL:= no MK_OPENSSH:= no diff --git a/tools/build/mk/OptionalObsoleteFiles.inc b/tools/build/mk/OptionalObsoleteFiles.inc index 1ec1ce109..bcbae9c54 100644 --- a/tools/build/mk/OptionalObsoleteFiles.inc +++ b/tools/build/mk/OptionalObsoleteFiles.inc @@ -671,6 +671,44 @@ OLD_FILES+=usr/share/man/man1/clang-cpp.1.gz OLD_FILES+=usr/share/man/man1/tblgen.1.gz .endif +.if ${MK_CLANG_EXTRAS} == no +OLD_FILES+=usr/bin/bugpoint +OLD_FILES+=usr/bin/llc +OLD_FILES+=usr/bin/lli +OLD_FILES+=usr/bin/llvm-ar +OLD_FILES+=usr/bin/llvm-as +OLD_FILES+=usr/bin/llvm-bcanalyzer +OLD_FILES+=usr/bin/llvm-diff +OLD_FILES+=usr/bin/llvm-dis +OLD_FILES+=usr/bin/llvm-extract +OLD_FILES+=usr/bin/llvm-ld +OLD_FILES+=usr/bin/llvm-link +OLD_FILES+=usr/bin/llvm-mc +OLD_FILES+=usr/bin/llvm-nm +OLD_FILES+=usr/bin/llvm-objdump +OLD_FILES+=usr/bin/llvm-prof +OLD_FILES+=usr/bin/llvm-ranlib +OLD_FILES+=usr/bin/llvm-rtdyld +OLD_FILES+=usr/bin/llvm-stub +OLD_FILES+=usr/bin/macho-dump +OLD_FILES+=usr/bin/opt +OLD_FILES+=usr/share/man/man1/bugpoint.1.gz +OLD_FILES+=usr/share/man/man1/llc.1.gz +OLD_FILES+=usr/share/man/man1/lli.1.gz +OLD_FILES+=usr/share/man/man1/llvm-ar.1.gz +OLD_FILES+=usr/share/man/man1/llvm-as.1.gz +OLD_FILES+=usr/share/man/man1/llvm-bcanalyzer.1.gz +OLD_FILES+=usr/share/man/man1/llvm-diff.1.gz +OLD_FILES+=usr/share/man/man1/llvm-dis.1.gz +OLD_FILES+=usr/share/man/man1/llvm-extract.1.gz +OLD_FILES+=usr/share/man/man1/llvm-ld.1.gz +OLD_FILES+=usr/share/man/man1/llvm-link.1.gz +OLD_FILES+=usr/share/man/man1/llvm-nm.1.gz +OLD_FILES+=usr/share/man/man1/llvm-prof.1.gz +OLD_FILES+=usr/share/man/man1/llvm-ranlib.1.gz +OLD_FILES+=usr/share/man/man1/opt.1.gz +.endif + .if ${MK_CPP} == no OLD_FILES+=usr/bin/cpp OLD_FILES+=usr/share/man/man1/cpp.1.gz diff --git a/tools/build/options/WITH_CLANG_EXTRAS b/tools/build/options/WITH_CLANG_EXTRAS new file mode 100644 index 000000000..87da08fef --- /dev/null +++ b/tools/build/options/WITH_CLANG_EXTRAS @@ -0,0 +1,2 @@ +.\" $FreeBSD$ +Set to build additional clang and llvm tools, such as bugpoint. diff --git a/usr.bin/clang/Makefile b/usr.bin/clang/Makefile index ef5a0e794..1cfa57ffa 100644 --- a/usr.bin/clang/Makefile +++ b/usr.bin/clang/Makefile @@ -1,5 +1,30 @@ # $FreeBSD$ +.include + SUBDIR= clang clang-tblgen tblgen +.if ${MK_CLANG_EXTRAS} != "no" +SUBDIR+=bugpoint \ + llc \ + lli \ + llvm-ar \ + llvm-as \ + llvm-bcanalyzer \ + llvm-diff \ + llvm-dis \ + llvm-extract \ + llvm-ld \ + llvm-link \ + llvm-mc \ + llvm-nm \ + llvm-objdump \ + llvm-prof \ + llvm-ranlib \ + llvm-rtdyld \ + llvm-stub \ + macho-dump \ + opt +.endif + .include diff --git a/usr.bin/clang/bugpoint/Makefile b/usr.bin/clang/bugpoint/Makefile new file mode 100644 index 000000000..6fc25f371 --- /dev/null +++ b/usr.bin/clang/bugpoint/Makefile @@ -0,0 +1,34 @@ +# $FreeBSD$ + +PROG_CXX=bugpoint + +SRCDIR= tools/bugpoint +SRCS= BugDriver.cpp \ + CrashDebugger.cpp \ + ExecutionDriver.cpp \ + ExtractFunction.cpp \ + FindBugs.cpp \ + Miscompilation.cpp \ + OptimizerDriver.cpp \ + ToolRunner.cpp \ + bugpoint.cpp + +TGHDRS= Intrinsics +LIBDEPS=llvmbitwriter \ + llvmlinker \ + llvmarchive \ + llvmbitreader \ + llvmipo \ + llvmscalaropts \ + llvminstcombine \ + llvminstrumentation \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmasmparser \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/bugpoint/bugpoint.1 b/usr.bin/clang/bugpoint/bugpoint.1 new file mode 100644 index 000000000..9d86c100a --- /dev/null +++ b/usr.bin/clang/bugpoint/bugpoint.1 @@ -0,0 +1,291 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "BUGPOINT 1" +.TH BUGPOINT 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +bugpoint \- automatic test case reduction tool +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBbugpoint\fR [\fIoptions\fR] [\fIinput \s-1LLVM\s0 ll/bc files\fR] [\fI\s-1LLVM\s0 passes\fR] \fB\-\-args\fR +\&\fIprogram arguments\fR +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +\&\fBbugpoint\fR narrows down the source of problems in \s-1LLVM\s0 tools and passes. It +can be used to debug three types of failures: optimizer crashes, miscompilations +by optimizers, or bad native code generation (including problems in the static +and \s-1JIT\s0 compilers). It aims to reduce large test cases to small, useful ones. +For more information on the design and inner workings of \fBbugpoint\fR, as well as +advice for using bugpoint, see \fIllvm/docs/Bugpoint.html\fR in the \s-1LLVM\s0 +distribution. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-\-additional\-so\fR \fIlibrary\fR" 4 +.IX Item "--additional-so library" +Load the dynamic shared object \fIlibrary\fR into the test program whenever it is +run. This is useful if you are debugging programs which depend on non-LLVM +libraries (such as the X or curses libraries) to run. +.IP "\fB\-\-append\-exit\-code\fR=\fI{true,false}\fR" 4 +.IX Item "--append-exit-code={true,false}" +Append the test programs exit code to the output file so that a change in exit +code is considered a test failure. Defaults to false. +.IP "\fB\-\-args\fR \fIprogram args\fR" 4 +.IX Item "--args program args" +Pass all arguments specified after \-args to the test program whenever it runs. +Note that if any of the \fIprogram args\fR start with a '\-', you should use: +.Sp +.Vb 1 +\& bugpoint [bugpoint args] \-\-args \-\- [program args] +.Ve +.Sp +The \*(L"\-\-\*(R" right after the \fB\-\-args\fR option tells \fBbugpoint\fR to consider any +options starting with \f(CW\*(C`\-\*(C'\fR to be part of the \fB\-\-args\fR option, not as options to +\&\fBbugpoint\fR itself. +.IP "\fB\-\-tool\-args\fR \fItool args\fR" 4 +.IX Item "--tool-args tool args" +Pass all arguments specified after \-\-tool\-args to the \s-1LLVM\s0 tool under test +(\fBllc\fR, \fBlli\fR, etc.) whenever it runs. You should use this option in the +following way: +.Sp +.Vb 1 +\& bugpoint [bugpoint args] \-\-tool\-args \-\- [tool args] +.Ve +.Sp +The \*(L"\-\-\*(R" right after the \fB\-\-tool\-args\fR option tells \fBbugpoint\fR to consider any +options starting with \f(CW\*(C`\-\*(C'\fR to be part of the \fB\-\-tool\-args\fR option, not as +options to \fBbugpoint\fR itself. (See \fB\-\-args\fR, above.) +.IP "\fB\-\-safe\-tool\-args\fR \fItool args\fR" 4 +.IX Item "--safe-tool-args tool args" +Pass all arguments specified after \fB\-\-safe\-tool\-args\fR to the \*(L"safe\*(R" execution +tool. +.IP "\fB\-\-gcc\-tool\-args\fR \fIgcc tool args\fR" 4 +.IX Item "--gcc-tool-args gcc tool args" +Pass all arguments specified after \fB\-\-gcc\-tool\-args\fR to the invocation of +\&\fBgcc\fR. +.IP "\fB\-\-opt\-args\fR \fIopt args\fR" 4 +.IX Item "--opt-args opt args" +Pass all arguments specified after \fB\-\-opt\-args\fR to the invocation of \fBopt\fR. +.IP "\fB\-\-disable\-{dce,simplifycfg}\fR" 4 +.IX Item "--disable-{dce,simplifycfg}" +Do not run the specified passes to clean up and reduce the size of the test +program. By default, \fBbugpoint\fR uses these passes internally when attempting to +reduce test programs. If you're trying to find a bug in one of these passes, +\&\fBbugpoint\fR may crash. +.IP "\fB\-\-enable\-valgrind\fR" 4 +.IX Item "--enable-valgrind" +Use valgrind to find faults in the optimization phase. This will allow +bugpoint to find otherwise asymptomatic problems caused by memory +mis-management. +.IP "\fB\-find\-bugs\fR" 4 +.IX Item "-find-bugs" +Continually randomize the specified passes and run them on the test program +until a bug is found or the user kills \fBbugpoint\fR. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-\-input\fR \fIfilename\fR" 4 +.IX Item "--input filename" +Open \fIfilename\fR and redirect the standard input of the test program, whenever +it runs, to come from that file. +.IP "\fB\-\-load\fR \fIplugin\fR" 4 +.IX Item "--load plugin" +Load the dynamic object \fIplugin\fR into \fBbugpoint\fR itself. This object should +register new optimization passes. Once loaded, the object will add new command +line options to enable various optimizations. To see the new complete list of +optimizations, use the \fB\-help\fR and \fB\-\-load\fR options together; for example: +.Sp +.Vb 1 +\& bugpoint \-\-load myNewPass.so \-help +.Ve +.IP "\fB\-\-mlimit\fR \fImegabytes\fR" 4 +.IX Item "--mlimit megabytes" +Specifies an upper limit on memory usage of the optimization and codegen. Set +to zero to disable the limit. +.IP "\fB\-\-output\fR \fIfilename\fR" 4 +.IX Item "--output filename" +Whenever the test program produces output on its standard output stream, it +should match the contents of \fIfilename\fR (the \*(L"reference output\*(R"). If you +do not use this option, \fBbugpoint\fR will attempt to generate a reference output +by compiling the program with the \*(L"safe\*(R" backend and running it. +.IP "\fB\-\-profile\-info\-file\fR \fIfilename\fR" 4 +.IX Item "--profile-info-file filename" +Profile file loaded by \fB\-\-profile\-loader\fR. +.IP "\fB\-\-run\-{int,jit,llc,cbe,custom}\fR" 4 +.IX Item "--run-{int,jit,llc,cbe,custom}" +Whenever the test program is compiled, \fBbugpoint\fR should generate code for it +using the specified code generator. These options allow you to choose the +interpreter, the \s-1JIT\s0 compiler, the static native code compiler, the C +backend, or a custom command (see \fB\-\-exec\-command\fR) respectively. +.IP "\fB\-\-safe\-{llc,cbe,custom}\fR" 4 +.IX Item "--safe-{llc,cbe,custom}" +When debugging a code generator, \fBbugpoint\fR should use the specified code +generator as the \*(L"safe\*(R" code generator. This is a known-good code generator +used to generate the \*(L"reference output\*(R" if it has not been provided, and to +compile portions of the program that as they are excluded from the testcase. +These options allow you to choose the +static native code compiler, the C backend, or a custom command, +(see \fB\-\-exec\-command\fR) respectively. The interpreter and the \s-1JIT\s0 backends +cannot currently be used as the \*(L"safe\*(R" backends. +.IP "\fB\-\-exec\-command\fR \fIcommand\fR" 4 +.IX Item "--exec-command command" +This option defines the command to use with the \fB\-\-run\-custom\fR and +\&\fB\-\-safe\-custom\fR options to execute the bitcode testcase. This can +be useful for cross-compilation. +.IP "\fB\-\-compile\-command\fR \fIcommand\fR" 4 +.IX Item "--compile-command command" +This option defines the command to use with the \fB\-\-compile\-custom\fR +option to compile the bitcode testcase. This can be useful for +testing compiler output without running any link or execute stages. To +generate a reduced unit test, you may add \s-1CHECK\s0 directives to the +testcase and pass the name of an executable compile-command script in this form: +.Sp +.Vb 3 +\& #!/bin/sh +\& llc "$@" +\& not FileCheck [bugpoint input file].ll < bugpoint\-test\-program.s +.Ve +.Sp +This script will \*(L"fail\*(R" as long as FileCheck passes. So the result +will be the minimum bitcode that passes FileCheck. +.IP "\fB\-\-safe\-path\fR \fIpath\fR" 4 +.IX Item "--safe-path path" +This option defines the path to the command to execute with the +\&\fB\-\-safe\-{int,jit,llc,cbe,custom}\fR +option. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBbugpoint\fR succeeds in finding a problem, it will exit with 0. Otherwise, +if an error occurs, it will exit with a non-zero value. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +opt +.SH "AUTHOR" +.IX Header "AUTHOR" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llc/Makefile b/usr.bin/clang/llc/Makefile new file mode 100644 index 000000000..db5f12d1b --- /dev/null +++ b/usr.bin/clang/llc/Makefile @@ -0,0 +1,45 @@ +# $FreeBSD$ + +PROG_CXX=llc + +SRCDIR= tools/llc +SRCS= llc.cpp + +LIBDEPS=llvmasmparser \ + llvmbitreader \ + llvmarmdisassembler \ + llvmarmasmparser \ + llvmarmcodegen \ + llvmarmdesc \ + llvmarminstprinter \ + llvmarminfo \ + llvmmipscodegen \ + llvmmipsdesc \ + llvmmipsinstprinter \ + llvmmipsinfo \ + llvmpowerpccodegen \ + llvmpowerpcdesc \ + llvmpowerpcinstprinter \ + llvmpowerpcinfo \ + llvmx86disassembler \ + llvmx86asmparser \ + llvmx86codegen \ + llvmx86desc \ + llvmselectiondag \ + llvmasmprinter \ + llvmmcparser \ + llvmcodegen \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmx86instprinter \ + llvmx86utils \ + llvmcore \ + llvmx86info \ + llvmmc \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llc/llc.1 b/usr.bin/clang/llc/llc.1 new file mode 100644 index 000000000..3c422bc3b --- /dev/null +++ b/usr.bin/clang/llc/llc.1 @@ -0,0 +1,285 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLC 1" +.TH LLC 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llc \- LLVM static compiler +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllc\fR [\fIoptions\fR] [\fIfilename\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllc\fR command compiles \s-1LLVM\s0 source inputs into assembly language for a +specified architecture. The assembly language output can then be passed through +a native assembler and linker to generate a native executable. +.PP +The choice of architecture for the output assembly code is automatically +determined from the input file, unless the \fB\-march\fR option is used to override +the default. +.SH "OPTIONS" +.IX Header "OPTIONS" +If \fIfilename\fR is \- or omitted, \fBllc\fR reads from standard input. Otherwise, it +will from \fIfilename\fR. Inputs can be in either the \s-1LLVM\s0 assembly language +format (.ll) or the \s-1LLVM\s0 bitcode format (.bc). +.PP +If the \fB\-o\fR option is omitted, then \fBllc\fR will send its output to standard +output if the input is from standard input. If the \fB\-o\fR option specifies \-, +then the output will also be sent to standard output. +.PP +If no \fB\-o\fR option is specified and an input file other than \- is specified, +then \fBllc\fR creates the output filename by taking the input filename, +removing any existing \fI.bc\fR extension, and adding a \fI.s\fR suffix. +.PP +Other \fBllc\fR options are as follows: +.SS "End-user Options" +.IX Subsection "End-user Options" +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-O\fR=\fIuint\fR" 4 +.IX Item "-O=uint" +Generate code at different optimization levels. These correspond to the \fI\-O0\fR, +\&\fI\-O1\fR, \fI\-O2\fR, \fI\-O3\fR, and \fI\-O4\fR optimization levels used by \fBllvm-gcc\fR and +\&\fBclang\fR. +.IP "\fB\-mtriple\fR=\fItarget triple\fR" 4 +.IX Item "-mtriple=target triple" +Override the target triple specified in the input file with the specified +string. +.IP "\fB\-march\fR=\fIarch\fR" 4 +.IX Item "-march=arch" +Specify the architecture for which to generate assembly, overriding the target +encoded in the input file. See the output of \fBllc \-help\fR for a list of +valid architectures. By default this is inferred from the target triple or +autodetected to the current architecture. +.IP "\fB\-mcpu\fR=\fIcpuname\fR" 4 +.IX Item "-mcpu=cpuname" +Specify a specific chip in the current architecture to generate code for. +By default this is inferred from the target triple and autodetected to +the current architecture. For a list of available CPUs, use: +\&\fBllvm-as < /dev/null | llc \-march=xyz \-mcpu=help\fR +.IP "\fB\-mattr\fR=\fIa1,+a2,\-a3,...\fR" 4 +.IX Item "-mattr=a1,+a2,-a3,..." +Override or control specific attributes of the target, such as whether \s-1SIMD\s0 +operations are enabled or not. The default set of attributes is set by the +current \s-1CPU\s0. For a list of available attributes, use: +\&\fBllvm-as < /dev/null | llc \-march=xyz \-mattr=help\fR +.IP "\fB\-\-disable\-fp\-elim\fR" 4 +.IX Item "--disable-fp-elim" +Disable frame pointer elimination optimization. +.IP "\fB\-\-disable\-excess\-fp\-precision\fR" 4 +.IX Item "--disable-excess-fp-precision" +Disable optimizations that may produce excess precision for floating point. +Note that this option can dramatically slow down code on some systems +(e.g. X86). +.IP "\fB\-\-enable\-no\-infs\-fp\-math\fR" 4 +.IX Item "--enable-no-infs-fp-math" +Enable optimizations that assume no Inf values. +.IP "\fB\-\-enable\-no\-nans\-fp\-math\fR" 4 +.IX Item "--enable-no-nans-fp-math" +Enable optimizations that assume no \s-1NAN\s0 values. +.IP "\fB\-\-enable\-unsafe\-fp\-math\fR" 4 +.IX Item "--enable-unsafe-fp-math" +Enable optimizations that make unsafe assumptions about \s-1IEEE\s0 math (e.g. that +addition is associative) or may not work for all input ranges. These +optimizations allow the code generator to make use of some instructions which +would otherwise not be usable (such as fsin on X86). +.IP "\fB\-\-enable\-correct\-eh\-support\fR" 4 +.IX Item "--enable-correct-eh-support" +Instruct the \fBlowerinvoke\fR pass to insert code for correct exception handling +support. This is expensive and is by default omitted for efficiency. +.IP "\fB\-\-stats\fR" 4 +.IX Item "--stats" +Print statistics recorded by code-generation passes. +.IP "\fB\-\-time\-passes\fR" 4 +.IX Item "--time-passes" +Record the amount of time needed for each pass and print a report to standard +error. +.IP "\fB\-\-load\fR=\fIdso_path\fR" 4 +.IX Item "--load=dso_path" +Dynamically load \fIdso_path\fR (a path to a dynamically shared object) that +implements an \s-1LLVM\s0 target. This will permit the target name to be used with the +\&\fB\-march\fR option so that code can be generated for that target. +.SS "Tuning/Configuration Options" +.IX Subsection "Tuning/Configuration Options" +.IP "\fB\-\-print\-machineinstrs\fR" 4 +.IX Item "--print-machineinstrs" +Print generated machine code between compilation phases (useful for debugging). +.IP "\fB\-\-regalloc\fR=\fIallocator\fR" 4 +.IX Item "--regalloc=allocator" +Specify the register allocator to use. The default \fIallocator\fR is \fIlocal\fR. +Valid register allocators are: +.RS 4 +.IP "\fIsimple\fR" 4 +.IX Item "simple" +Very simple \*(L"always spill\*(R" register allocator +.IP "\fIlocal\fR" 4 +.IX Item "local" +Local register allocator +.IP "\fIlinearscan\fR" 4 +.IX Item "linearscan" +Linear scan global register allocator +.IP "\fIiterativescan\fR" 4 +.IX Item "iterativescan" +Iterative scan global register allocator +.RE +.RS 4 +.RE +.IP "\fB\-\-spiller\fR=\fIspiller\fR" 4 +.IX Item "--spiller=spiller" +Specify the spiller to use for register allocators that support it. Currently +this option is used only by the linear scan register allocator. The default +\&\fIspiller\fR is \fIlocal\fR. Valid spillers are: +.RS 4 +.IP "\fIsimple\fR" 4 +.IX Item "simple" +Simple spiller +.IP "\fIlocal\fR" 4 +.IX Item "local" +Local spiller +.RE +.RS 4 +.RE +.SS "Intel IA\-32\-specific Options" +.IX Subsection "Intel IA-32-specific Options" +.IP "\fB\-\-x86\-asm\-syntax=att|intel\fR" 4 +.IX Item "--x86-asm-syntax=att|intel" +Specify whether to emit assembly code in \s-1AT&T\s0 syntax (the default) or intel +syntax. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllc\fR succeeds, it will exit with 0. Otherwise, if an error occurs, +it will exit with a non-zero value. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +lli +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/lli/Makefile b/usr.bin/clang/lli/Makefile new file mode 100644 index 000000000..665c6d9af --- /dev/null +++ b/usr.bin/clang/lli/Makefile @@ -0,0 +1,35 @@ +# $FreeBSD$ + +PROG_CXX=lli + +SRCDIR= tools/lli +SRCS= lli.cpp + +LIBDEPS=llvmasmparser \ + llvmbitreader \ + llvmx86codegen \ + llvmx86desc \ + llvmx86info \ + llvmx86instprinter \ + llvmx86utils \ + llvmselectiondag \ + llvmasmprinter \ + llvmmcparser \ + llvminterpreter \ + llvmjit \ + llvmcodegen \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmmcjit \ + llvmruntimedyld \ + llvmobject \ + llvmexecutionengine \ + llvmtarget \ + llvmmc \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/lli/lli.1 b/usr.bin/clang/lli/lli.1 new file mode 100644 index 000000000..f90a6521d --- /dev/null +++ b/usr.bin/clang/lli/lli.1 @@ -0,0 +1,310 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLI 1" +.TH LLI 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +lli \- directly execute programs from LLVM bitcode +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBlli\fR [\fIoptions\fR] [\fIfilename\fR] [\fIprogram args\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +\&\fBlli\fR directly executes programs in \s-1LLVM\s0 bitcode format. It takes a program +in \s-1LLVM\s0 bitcode format and executes it using a just-in-time compiler, if one is +available for the current architecture, or an interpreter. \fBlli\fR takes all of +the same code generator options as llc, but they are only effective when +\&\fBlli\fR is using the just-in-time compiler. +.PP +If \fIfilename\fR is not specified, then \fBlli\fR reads the \s-1LLVM\s0 bitcode for the +program from standard input. +.PP +The optional \fIargs\fR specified on the command line are passed to the program as +arguments. +.SH "GENERAL OPTIONS" +.IX Header "GENERAL OPTIONS" +.IP "\fB\-fake\-argv0\fR=\fIexecutable\fR" 4 +.IX Item "-fake-argv0=executable" +Override the \f(CW\*(C`argv[0]\*(C'\fR value passed into the executing program. +.IP "\fB\-force\-interpreter\fR=\fI{false,true}\fR" 4 +.IX Item "-force-interpreter={false,true}" +If set to true, use the interpreter even if a just-in-time compiler is available +for this architecture. Defaults to false. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-load\fR=\fIpuginfilename\fR" 4 +.IX Item "-load=puginfilename" +Causes \fBlli\fR to load the plugin (shared object) named \fIpluginfilename\fR and use +it for optimization. +.IP "\fB\-stats\fR" 4 +.IX Item "-stats" +Print statistics from the code-generation passes. This is only meaningful for +the just-in-time compiler, at present. +.IP "\fB\-time\-passes\fR" 4 +.IX Item "-time-passes" +Record the amount of time needed for each code-generation pass and print it to +standard error. +.IP "\fB\-version\fR" 4 +.IX Item "-version" +Print out the version of \fBlli\fR and exit without doing anything else. +.SH "TARGET OPTIONS" +.IX Header "TARGET OPTIONS" +.IP "\fB\-mtriple\fR=\fItarget triple\fR" 4 +.IX Item "-mtriple=target triple" +Override the target triple specified in the input bitcode file with the +specified string. This may result in a crash if you pick an +architecture which is not compatible with the current system. +.IP "\fB\-march\fR=\fIarch\fR" 4 +.IX Item "-march=arch" +Specify the architecture for which to generate assembly, overriding the target +encoded in the bitcode file. See the output of \fBllc \-help\fR for a list of +valid architectures. By default this is inferred from the target triple or +autodetected to the current architecture. +.IP "\fB\-mcpu\fR=\fIcpuname\fR" 4 +.IX Item "-mcpu=cpuname" +Specify a specific chip in the current architecture to generate code for. +By default this is inferred from the target triple and autodetected to +the current architecture. For a list of available CPUs, use: +\&\fBllvm-as < /dev/null | llc \-march=xyz \-mcpu=help\fR +.IP "\fB\-mattr\fR=\fIa1,+a2,\-a3,...\fR" 4 +.IX Item "-mattr=a1,+a2,-a3,..." +Override or control specific attributes of the target, such as whether \s-1SIMD\s0 +operations are enabled or not. The default set of attributes is set by the +current \s-1CPU\s0. For a list of available attributes, use: +\&\fBllvm-as < /dev/null | llc \-march=xyz \-mattr=help\fR +.SH "FLOATING POINT OPTIONS" +.IX Header "FLOATING POINT OPTIONS" +.IP "\fB\-disable\-excess\-fp\-precision\fR" 4 +.IX Item "-disable-excess-fp-precision" +Disable optimizations that may increase floating point precision. +.IP "\fB\-enable\-no\-infs\-fp\-math\fR" 4 +.IX Item "-enable-no-infs-fp-math" +Enable optimizations that assume no Inf values. +.IP "\fB\-enable\-no\-nans\-fp\-math\fR" 4 +.IX Item "-enable-no-nans-fp-math" +Enable optimizations that assume no \s-1NAN\s0 values. +.IP "\fB\-enable\-unsafe\-fp\-math\fR" 4 +.IX Item "-enable-unsafe-fp-math" +Causes \fBlli\fR to enable optimizations that may decrease floating point +precision. +.IP "\fB\-soft\-float\fR" 4 +.IX Item "-soft-float" +Causes \fBlli\fR to generate software floating point library calls instead of +equivalent hardware instructions. +.SH "CODE GENERATION OPTIONS" +.IX Header "CODE GENERATION OPTIONS" +.IP "\fB\-code\-model\fR=\fImodel\fR" 4 +.IX Item "-code-model=model" +Choose the code model from: +.Sp +.Vb 5 +\& default: Target default code model +\& small: Small code model +\& kernel: Kernel code model +\& medium: Medium code model +\& large: Large code model +.Ve +.IP "\fB\-disable\-post\-RA\-scheduler\fR" 4 +.IX Item "-disable-post-RA-scheduler" +Disable scheduling after register allocation. +.IP "\fB\-disable\-spill\-fusing\fR" 4 +.IX Item "-disable-spill-fusing" +Disable fusing of spill code into instructions. +.IP "\fB\-enable\-correct\-eh\-support\fR" 4 +.IX Item "-enable-correct-eh-support" +Make the \-lowerinvoke pass insert expensive, but correct, \s-1EH\s0 code. +.IP "\fB\-jit\-enable\-eh\fR" 4 +.IX Item "-jit-enable-eh" +Exception handling should be enabled in the just-in-time compiler. +.IP "\fB\-join\-liveintervals\fR" 4 +.IX Item "-join-liveintervals" +Coalesce copies (default=true). +.IP "\fB\-nozero\-initialized\-in\-bss\fR Don't place zero-initialized symbols into the \s-1BSS\s0 section." 4 +.IX Item "-nozero-initialized-in-bss Don't place zero-initialized symbols into the BSS section." +.PD 0 +.IP "\fB\-pre\-RA\-sched\fR=\fIscheduler\fR" 4 +.IX Item "-pre-RA-sched=scheduler" +.PD +Instruction schedulers available (before register allocation): +.Sp +.Vb 7 +\& =default: Best scheduler for the target +\& =none: No scheduling: breadth first sequencing +\& =simple: Simple two pass scheduling: minimize critical path and maximize processor utilization +\& =simple\-noitin: Simple two pass scheduling: Same as simple except using generic latency +\& =list\-burr: Bottom\-up register reduction list scheduling +\& =list\-tdrr: Top\-down register reduction list scheduling +\& =list\-td: Top\-down list scheduler \-print\-machineinstrs \- Print generated machine code +.Ve +.IP "\fB\-regalloc\fR=\fIallocator\fR" 4 +.IX Item "-regalloc=allocator" +Register allocator to use (default=linearscan) +.Sp +.Vb 3 +\& =bigblock: Big\-block register allocator +\& =linearscan: linear scan register allocator =local \- local register allocator +\& =simple: simple register allocator +.Ve +.IP "\fB\-relocation\-model\fR=\fImodel\fR" 4 +.IX Item "-relocation-model=model" +Choose relocation model from: +.Sp +.Vb 3 +\& =default: Target default relocation model +\& =static: Non\-relocatable code =pic \- Fully relocatable, position independent code +\& =dynamic\-no\-pic: Relocatable external references, non\-relocatable code +.Ve +.IP "\fB\-spiller\fR" 4 +.IX Item "-spiller" +Spiller to use (default=local) +.Sp +.Vb 2 +\& =simple: simple spiller +\& =local: local spiller +.Ve +.IP "\fB\-x86\-asm\-syntax\fR=\fIsyntax\fR" 4 +.IX Item "-x86-asm-syntax=syntax" +Choose style of code to emit from X86 backend: +.Sp +.Vb 2 +\& =att: Emit AT&T\-style assembly +\& =intel: Emit Intel\-style assembly +.Ve +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBlli\fR fails to load the program, it will exit with an exit code of 1. +Otherwise, it will return the exit code of the program it executes. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llc +.SH "AUTHOR" +.IX Header "AUTHOR" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-ar/Makefile b/usr.bin/clang/llvm-ar/Makefile new file mode 100644 index 000000000..97ad05820 --- /dev/null +++ b/usr.bin/clang/llvm-ar/Makefile @@ -0,0 +1,14 @@ +# $FreeBSD$ + +PROG_CXX=llvm-ar + +SRCDIR= tools/llvm-ar +SRCS= llvm-ar.cpp +LLVM_REQUIRES_EH= + +LIBDEPS=llvmarchive \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-ar/llvm-ar.1 b/usr.bin/clang/llvm-ar/llvm-ar.1 new file mode 100644 index 000000000..4939f3393 --- /dev/null +++ b/usr.bin/clang/llvm-ar/llvm-ar.1 @@ -0,0 +1,461 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-AR 1" +.TH LLVM-AR 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-ar \- LLVM archiver +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-ar\fR [\-]{dmpqrtx}[Rabfikouz] [relpos] [count] [files...] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-ar\fR command is similar to the common Unix utility, \f(CW\*(C`ar\*(C'\fR. It +archives several files together into a single file. The intent for this is +to produce archive libraries by \s-1LLVM\s0 bitcode that can be linked into an +\&\s-1LLVM\s0 program. However, the archive can contain any kind of file. By default, +\&\fBllvm-ar\fR generates a symbol table that makes linking faster because +only the symbol table needs to be consulted, not each individual file member +of the archive. +.PP +The \fBllvm-ar\fR command can be used to \fIread\fR both \s-1SVR4\s0 and \s-1BSD\s0 style archive +files. However, it cannot be used to write them. While the \fBllvm-ar\fR command +produces files that are \fIalmost\fR identical to the format used by other \f(CW\*(C`ar\*(C'\fR +implementations, it has two significant departures in order to make the +archive appropriate for \s-1LLVM\s0. The first departure is that \fBllvm-ar\fR only +uses \s-1BSD4\s0.4 style long path names (stored immediately after the header) and +never contains a string table for long names. The second departure is that the +symbol table is formated for efficient construction of an in-memory data +structure that permits rapid (red-black tree) lookups. Consequently, archives +produced with \fBllvm-ar\fR usually won't be readable or editable with any +\&\f(CW\*(C`ar\*(C'\fR implementation or useful for linking. Using the \f(CW\*(C`f\*(C'\fR modifier to flatten +file names will make the archive readable by other \f(CW\*(C`ar\*(C'\fR implementations +but not for linking because the symbol table format for \s-1LLVM\s0 is unique. If an +\&\s-1SVR4\s0 or \s-1BSD\s0 style archive is used with the \f(CW\*(C`r\*(C'\fR (replace) or \f(CW\*(C`q\*(C'\fR (quick +update) operations, the archive will be reconstructed in \s-1LLVM\s0 format. This +means that the string table will be dropped (in deference to \s-1BSD\s0 4.4 long names) +and an \s-1LLVM\s0 symbol table will be added (by default). The system symbol table +will be retained. +.PP +Here's where \fBllvm-ar\fR departs from previous \f(CW\*(C`ar\*(C'\fR implementations: +.IP "\fISymbol Table\fR" 4 +.IX Item "Symbol Table" +Since \fBllvm-ar\fR is intended to archive bitcode files, the symbol table +won't make much sense to anything but \s-1LLVM\s0. Consequently, the symbol table's +format has been simplified. It consists simply of a sequence of pairs +of a file member index number as an \s-1LSB\s0 4byte integer and a null-terminated +string. +.IP "\fILong Paths\fR" 4 +.IX Item "Long Paths" +Some \f(CW\*(C`ar\*(C'\fR implementations (\s-1SVR4\s0) use a separate file member to record long +path names (> 15 characters). \fBllvm-ar\fR takes the \s-1BSD\s0 4.4 and Mac \s-1OS\s0 X +approach which is to simply store the full path name immediately preceding +the data for the file. The path name is null terminated and may contain the +slash (/) character. +.IP "\fICompression\fR" 4 +.IX Item "Compression" +\&\fBllvm-ar\fR can compress the members of an archive to save space. The +compression used depends on what's available on the platform and what choices +the \s-1LLVM\s0 Compressor utility makes. It generally favors bzip2 but will select +between \*(L"no compression\*(R" or bzip2 depending on what makes sense for the +file's content. +.IP "\fIDirectory Recursion\fR" 4 +.IX Item "Directory Recursion" +Most \f(CW\*(C`ar\*(C'\fR implementations do not recurse through directories but simply +ignore directories if they are presented to the program in the \fIfiles\fR +option. \fBllvm-ar\fR, however, can recurse through directory structures and +add all the files under a directory, if requested. +.IP "\fI\s-1TOC\s0 Verbose Output\fR" 4 +.IX Item "TOC Verbose Output" +When \fBllvm-ar\fR prints out the verbose table of contents (\f(CW\*(C`tv\*(C'\fR option), it +precedes the usual output with a character indicating the basic kind of +content in the file. A blank means the file is a regular file. A 'Z' means +the file is compressed. A 'B' means the file is an \s-1LLVM\s0 bitcode file. An +\&'S' means the file is the symbol table. +.SH "OPTIONS" +.IX Header "OPTIONS" +The options to \fBllvm-ar\fR are compatible with other \f(CW\*(C`ar\*(C'\fR implementations. +However, there are a few modifiers (\fIzR\fR) that are not found in other +\&\f(CW\*(C`ar\*(C'\fRs. The options to \fBllvm-ar\fR specify a single basic operation to +perform on the archive, a variety of modifiers for that operation, the +name of the archive file, and an optional list of file names. These options +are used to determine how \fBllvm-ar\fR should process the archive file. +.PP +The Operations and Modifiers are explained in the sections below. The minimal +set of options is at least one operator and the name of the archive. Typically +archive files end with a \f(CW\*(C`.a\*(C'\fR suffix, but this is not required. Following +the \fIarchive-name\fR comes a list of \fIfiles\fR that indicate the specific members +of the archive to operate on. If the \fIfiles\fR option is not specified, it +generally means either \*(L"none\*(R" or \*(L"all\*(R" members, depending on the operation. +.SS "Operations" +.IX Subsection "Operations" +.IP "d" 4 +.IX Item "d" +Delete files from the archive. No modifiers are applicable to this operation. +The \fIfiles\fR options specify which members should be removed from the +archive. It is not an error if a specified file does not appear in the archive. +If no \fIfiles\fR are specified, the archive is not modified. +.IP "m[abi]" 4 +.IX Item "m[abi]" +Move files from one location in the archive to another. The \fIa\fR, \fIb\fR, and +\&\fIi\fR modifiers apply to this operation. The \fIfiles\fR will all be moved +to the location given by the modifiers. If no modifiers are used, the files +will be moved to the end of the archive. If no \fIfiles\fR are specified, the +archive is not modified. +.IP "p[k]" 4 +.IX Item "p[k]" +Print files to the standard output. The \fIk\fR modifier applies to this +operation. This operation simply prints the \fIfiles\fR indicated to the +standard output. If no \fIfiles\fR are specified, the entire archive is printed. +Printing bitcode files is ill-advised as they might confuse your terminal +settings. The \fIp\fR operation never modifies the archive. +.IP "q[Rfz]" 4 +.IX Item "q[Rfz]" +Quickly append files to the end of the archive. The \fIR\fR, \fIf\fR, and \fIz\fR +modifiers apply to this operation. This operation quickly adds the +\&\fIfiles\fR to the archive without checking for duplicates that should be +removed first. If no \fIfiles\fR are specified, the archive is not modified. +Because of the way that \fBllvm-ar\fR constructs the archive file, its dubious +whether the \fIq\fR operation is any faster than the \fIr\fR operation. +.IP "r[Rabfuz]" 4 +.IX Item "r[Rabfuz]" +Replace or insert file members. The \fIR\fR, \fIa\fR, \fIb\fR, \fIf\fR, \fIu\fR, and \fIz\fR +modifiers apply to this operation. This operation will replace existing +\&\fIfiles\fR or insert them at the end of the archive if they do not exist. If no +\&\fIfiles\fR are specified, the archive is not modified. +.IP "t[v]" 4 +.IX Item "t[v]" +Print the table of contents. Without any modifiers, this operation just prints +the names of the members to the standard output. With the \fIv\fR modifier, +\&\fBllvm-ar\fR also prints out the file type (B=bitcode, Z=compressed, S=symbol +table, blank=regular file), the permission mode, the owner and group, the +size, and the date. If any \fIfiles\fR are specified, the listing is only for +those files. If no \fIfiles\fR are specified, the table of contents for the +whole archive is printed. +.IP "x[oP]" 4 +.IX Item "x[oP]" +Extract archive members back to files. The \fIo\fR modifier applies to this +operation. This operation retrieves the indicated \fIfiles\fR from the archive +and writes them back to the operating system's file system. If no +\&\fIfiles\fR are specified, the entire archive is extract. +.SS "Modifiers (operation specific)" +.IX Subsection "Modifiers (operation specific)" +The modifiers below are specific to certain operations. See the Operations +section (above) to determine which modifiers are applicable to which operations. +.IP "[a]" 4 +.IX Item "[a]" +When inserting or moving member files, this option specifies the destination of +the new files as being \f(CW\*(C`a\*(C'\fRfter the \fIrelpos\fR member. If \fIrelpos\fR is not found, +the files are placed at the end of the archive. +.IP "[b]" 4 +.IX Item "[b]" +When inserting or moving member files, this option specifies the destination of +the new files as being \f(CW\*(C`b\*(C'\fRefore the \fIrelpos\fR member. If \fIrelpos\fR is not +found, the files are placed at the end of the archive. This modifier is +identical to the the \fIi\fR modifier. +.IP "[f]" 4 +.IX Item "[f]" +Normally, \fBllvm-ar\fR stores the full path name to a file as presented to it on +the command line. With this option, truncated (15 characters max) names are +used. This ensures name compatibility with older versions of \f(CW\*(C`ar\*(C'\fR but may also +thwart correct extraction of the files (duplicates may overwrite). If used with +the \fIR\fR option, the directory recursion will be performed but the file names +will all be \f(CW\*(C`f\*(C'\fRlattened to simple file names. +.IP "[i]" 4 +.IX Item "[i]" +A synonym for the \fIb\fR option. +.IP "[k]" 4 +.IX Item "[k]" +Normally, \fBllvm-ar\fR will not print the contents of bitcode files when the +\&\fIp\fR operation is used. This modifier defeats the default and allows the +bitcode members to be printed. +.IP "[N]" 4 +.IX Item "[N]" +This option is ignored by \fBllvm-ar\fR but provided for compatibility. +.IP "[o]" 4 +.IX Item "[o]" +When extracting files, this option will cause \fBllvm-ar\fR to preserve the +original modification times of the files it writes. +.IP "[P]" 4 +.IX Item "[P]" +use full path names when matching +.IP "[R]" 4 +.IX Item "[R]" +This modifier instructions the \fIr\fR option to recursively process directories. +Without \fIR\fR, directories are ignored and only those \fIfiles\fR that refer to +files will be added to the archive. When \fIR\fR is used, any directories specified +with \fIfiles\fR will be scanned (recursively) to find files to be added to the +archive. Any file whose name begins with a dot will not be added. +.IP "[u]" 4 +.IX Item "[u]" +When replacing existing files in the archive, only replace those files that have +a time stamp than the time stamp of the member in the archive. +.IP "[z]" 4 +.IX Item "[z]" +When inserting or replacing any file in the archive, compress the file first. +This +modifier is safe to use when (previously) compressed bitcode files are added to +the archive; the compressed bitcode files will not be doubly compressed. +.SS "Modifiers (generic)" +.IX Subsection "Modifiers (generic)" +The modifiers below may be applied to any operation. +.IP "[c]" 4 +.IX Item "[c]" +For all operations, \fBllvm-ar\fR will always create the archive if it doesn't +exist. Normally, \fBllvm-ar\fR will print a warning message indicating that the +archive is being created. Using this modifier turns off that warning. +.IP "[s]" 4 +.IX Item "[s]" +This modifier requests that an archive index (or symbol table) be added to the +archive. This is the default mode of operation. The symbol table will contain +all the externally visible functions and global variables defined by all the +bitcode files in the archive. Using this modifier is more efficient that using +llvm-ranlib which also creates the symbol table. +.IP "[S]" 4 +.IX Item "[S]" +This modifier is the opposite of the \fIs\fR modifier. It instructs \fBllvm-ar\fR to +not build the symbol table. If both \fIs\fR and \fIS\fR are used, the last modifier to +occur in the options will prevail. +.IP "[v]" 4 +.IX Item "[v]" +This modifier instructs \fBllvm-ar\fR to be verbose about what it is doing. Each +editing operation taken against the archive will produce a line of output saying +what is being done. +.SH "STANDARDS" +.IX Header "STANDARDS" +The \fBllvm-ar\fR utility is intended to provide a superset of the \s-1IEEE\s0 Std 1003.2 +(\s-1POSIX\s0.2) functionality for \f(CW\*(C`ar\*(C'\fR. \fBllvm-ar\fR can read both \s-1SVR4\s0 and \s-1BSD4\s0.4 (or +Mac \s-1OS\s0 X) archives. If the \f(CW\*(C`f\*(C'\fR modifier is given to the \f(CW\*(C`x\*(C'\fR or \f(CW\*(C`r\*(C'\fR operations +then \fBllvm-ar\fR will write \s-1SVR4\s0 compatible archives. Without this modifier, +\&\fBllvm-ar\fR will write \s-1BSD4\s0.4 compatible archives that have long names +immediately after the header and indicated using the \*(L"#1/ddd\*(R" notation for the +name in the header. +.SH "FILE FORMAT" +.IX Header "FILE FORMAT" +The file format for \s-1LLVM\s0 Archive files is similar to that of \s-1BSD\s0 4.4 or Mac \s-1OSX\s0 +archive files. In fact, except for the symbol table, the \f(CW\*(C`ar\*(C'\fR commands on those +operating systems should be able to read \s-1LLVM\s0 archive files. The details of the +file format follow. +.PP +Each archive begins with the archive magic number which is the eight printable +characters \*(L"!\en\*(R" where \en represents the newline character (0x0A). +Following the magic number, the file is composed of even length members that +begin with an archive header and end with a \en padding character if necessary +(to make the length even). Each file member is composed of a header (defined +below), an optional newline-terminated \*(L"long file name\*(R" and the contents of +the file. +.PP +The fields of the header are described in the items below. All fields of the +header contain only \s-1ASCII\s0 characters, are left justified and are right padded +with space characters. +.IP "name \- char[16]" 4 +.IX Item "name - char[16]" +This field of the header provides the name of the archive member. If the name is +longer than 15 characters or contains a slash (/) character, then this field +contains \f(CW\*(C`#1/nnn\*(C'\fR where \f(CW\*(C`nnn\*(C'\fR provides the length of the name and the \f(CW\*(C`#1/\*(C'\fR +is literal. In this case, the actual name of the file is provided in the \f(CW\*(C`nnn\*(C'\fR +bytes immediately following the header. If the name is 15 characters or less, it +is contained directly in this field and terminated with a slash (/) character. +.IP "date \- char[12]" 4 +.IX Item "date - char[12]" +This field provides the date of modification of the file in the form of a +decimal encoded number that provides the number of seconds since the epoch +(since 00:00:00 Jan 1, 1970) per Posix specifications. +.IP "uid \- char[6]" 4 +.IX Item "uid - char[6]" +This field provides the user id of the file encoded as a decimal \s-1ASCII\s0 string. +This field might not make much sense on non-Unix systems. On Unix, it is the +same value as the st_uid field of the stat structure returned by the \fIstat\fR\|(2) +operating system call. +.IP "gid \- char[6]" 4 +.IX Item "gid - char[6]" +This field provides the group id of the file encoded as a decimal \s-1ASCII\s0 string. +This field might not make much sense on non-Unix systems. On Unix, it is the +same value as the st_gid field of the stat structure returned by the \fIstat\fR\|(2) +operating system call. +.IP "mode \- char[8]" 4 +.IX Item "mode - char[8]" +This field provides the access mode of the file encoded as an octal \s-1ASCII\s0 +string. This field might not make much sense on non-Unix systems. On Unix, it +is the same value as the st_mode field of the stat structure returned by the +\&\fIstat\fR\|(2) operating system call. +.IP "size \- char[10]" 4 +.IX Item "size - char[10]" +This field provides the size of the file, in bytes, encoded as a decimal \s-1ASCII\s0 +string. If the size field is negative (starts with a minus sign, 0x02D), then +the archive member is stored in compressed form. The first byte of the archive +member's data indicates the compression type used. A value of 0 (0x30) indicates +that no compression was used. A value of 2 (0x32) indicates that bzip2 +compression was used. +.IP "fmag \- char[2]" 4 +.IX Item "fmag - char[2]" +This field is the archive file member magic number. Its content is always the +two characters back tick (0x60) and newline (0x0A). This provides some measure +utility in identifying archive files that have been corrupted. +.PP +The \s-1LLVM\s0 symbol table has the special name \*(L"#_LLVM_SYM_TAB_#\*(R". It is presumed +that no regular archive member file will want this name. The \s-1LLVM\s0 symbol table +is simply composed of a sequence of triplets: byte offset, length of symbol, +and the symbol itself. Symbols are not null or newline terminated. Here are +the details on each of these items: +.IP "offset \- vbr encoded 32\-bit integer" 4 +.IX Item "offset - vbr encoded 32-bit integer" +The offset item provides the offset into the archive file where the bitcode +member is stored that is associated with the symbol. The offset value is 0 +based at the start of the first \*(L"normal\*(R" file member. To derive the actual +file offset of the member, you must add the number of bytes occupied by the file +signature (8 bytes) and the symbol tables. The value of this item is encoded +using variable bit rate encoding to reduce the size of the symbol table. +Variable bit rate encoding uses the high bit (0x80) of each byte to indicate +if there are more bytes to follow. The remaining 7 bits in each byte carry bits +from the value. The final byte does not have the high bit set. +.IP "length \- vbr encoded 32\-bit integer" 4 +.IX Item "length - vbr encoded 32-bit integer" +The length item provides the length of the symbol that follows. Like this +\&\fIoffset\fR item, the length is variable bit rate encoded. +.IP "symbol \- character array" 4 +.IX Item "symbol - character array" +The symbol item provides the text of the symbol that is associated with the +\&\fIoffset\fR. The symbol is not terminated by any character. Its length is provided +by the \fIlength\fR field. Note that is allowed (but unwise) to use non-printing +characters (even 0x00) in the symbol. This allows for multiple encodings of +symbol names. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-ar\fR succeeds, it will exit with 0. A usage error, results +in an exit code of 1. A hard (file system typically) error results in an +exit code of 2. Miscellaneous or unknown errors result in an +exit code of 3. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-ranlib, \fIar\fR\|(1) +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-as/Makefile b/usr.bin/clang/llvm-as/Makefile new file mode 100644 index 000000000..7e90878bb --- /dev/null +++ b/usr.bin/clang/llvm-as/Makefile @@ -0,0 +1,14 @@ +# $FreeBSD$ + +PROG_CXX=llvm-as + +SRCDIR= tools/llvm-as +SRCS= llvm-as.cpp +LLVM_REQUIRES_EH= + +LIBDEPS=llvmbitwriter \ + llvmasmparser \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-as/llvm-as.1 b/usr.bin/clang/llvm-as/llvm-as.1 new file mode 100644 index 000000000..662d06a3b --- /dev/null +++ b/usr.bin/clang/llvm-as/llvm-as.1 @@ -0,0 +1,182 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-AS 1" +.TH LLVM-AS 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-as \- LLVM assembler +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-as\fR [\fIoptions\fR] [\fIfilename\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +\&\fBllvm-as\fR is the \s-1LLVM\s0 assembler. It reads a file containing human-readable +\&\s-1LLVM\s0 assembly language, translates it to \s-1LLVM\s0 bitcode, and writes the result +into a file or to standard output. +.PP +If \fIfilename\fR is omitted or is \f(CW\*(C`\-\*(C'\fR, then \fBllvm-as\fR reads its input from +standard input. +.PP +If an output file is not specified with the \fB\-o\fR option, then +\&\fBllvm-as\fR sends its output to a file or standard output by following +these rules: +.IP "\(bu" 4 +If the input is standard input, then the output is standard output. +.IP "\(bu" 4 +If the input is a file that ends with \f(CW\*(C`.ll\*(C'\fR, then the output file is of +the same name, except that the suffix is changed to \f(CW\*(C`.bc\*(C'\fR. +.IP "\(bu" 4 +If the input is a file that does not end with the \f(CW\*(C`.ll\*(C'\fR suffix, then the +output file has the same name as the input file, except that the \f(CW\*(C`.bc\*(C'\fR +suffix is appended. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-f\fR" 4 +.IX Item "-f" +Enable binary output on terminals. Normally, \fBllvm-as\fR will refuse to +write raw bitcode output if the output stream is a terminal. With this option, +\&\fBllvm-as\fR will write raw bitcode regardless of the output device. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-o\fR \fIfilename\fR" 4 +.IX Item "-o filename" +Specify the output file name. If \fIfilename\fR is \f(CW\*(C`\-\*(C'\fR, then \fBllvm-as\fR +sends its output to standard output. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-as\fR succeeds, it will exit with 0. Otherwise, if an error +occurs, it will exit with a non-zero value. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-dis, gccas +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-bcanalyzer/Makefile b/usr.bin/clang/llvm-bcanalyzer/Makefile new file mode 100644 index 000000000..27788cc0a --- /dev/null +++ b/usr.bin/clang/llvm-bcanalyzer/Makefile @@ -0,0 +1,13 @@ +# $FreeBSD$ + +PROG_CXX=llvm-bcanalyzer + +SRCDIR= tools/llvm-bcanalyzer +SRCS= llvm-bcanalyzer.cpp +LLVM_REQUIRES_EH= + +LIBDEPS=llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-bcanalyzer/llvm-bcanalyzer.1 b/usr.bin/clang/llvm-bcanalyzer/llvm-bcanalyzer.1 new file mode 100644 index 000000000..26777abaa --- /dev/null +++ b/usr.bin/clang/llvm-bcanalyzer/llvm-bcanalyzer.1 @@ -0,0 +1,370 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-BCANALYZER 1" +.TH LLVM-BCANALYZER 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-bcanalyzer \- LLVM bitcode analyzer +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-bcanalyzer\fR [\fIoptions\fR] [\fIfilename\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-bcanalyzer\fR command is a small utility for analyzing bitcode files. +The tool reads a bitcode file (such as generated with the \fBllvm-as\fR tool) and +produces a statistical report on the contents of the bitcode file. The tool +can also dump a low level but human readable version of the bitcode file. +This tool is probably not of much interest or utility except for those working +directly with the bitcode file format. Most \s-1LLVM\s0 users can just ignore +this tool. +.PP +If \fIfilename\fR is omitted or is \f(CW\*(C`\-\*(C'\fR, then \fBllvm-bcanalyzer\fR reads its input +from standard input. This is useful for combining the tool into a pipeline. +Output is written to the standard output. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-nodetails\fR" 4 +.IX Item "-nodetails" +Causes \fBllvm-bcanalyzer\fR to abbreviate its output by writing out only a module +level summary. The details for individual functions are not displayed. +.IP "\fB\-dump\fR" 4 +.IX Item "-dump" +Causes \fBllvm-bcanalyzer\fR to dump the bitcode in a human readable format. This +format is significantly different from \s-1LLVM\s0 assembly and provides details about +the encoding of the bitcode file. +.IP "\fB\-verify\fR" 4 +.IX Item "-verify" +Causes \fBllvm-bcanalyzer\fR to verify the module produced by reading the +bitcode. This ensures that the statistics generated are based on a consistent +module. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-bcanalyzer\fR succeeds, it will exit with 0. Otherwise, if an error +occurs, it will exit with a non-zero value, usually 1. +.SH "SUMMARY OUTPUT DEFINITIONS" +.IX Header "SUMMARY OUTPUT DEFINITIONS" +The following items are always printed by llvm-bcanalyzer. They comprize the +summary output. +.IP "\fBBitcode Analysis Of Module\fR" 4 +.IX Item "Bitcode Analysis Of Module" +This just provides the name of the module for which bitcode analysis is being +generated. +.IP "\fBBitcode Version Number\fR" 4 +.IX Item "Bitcode Version Number" +The bitcode version (not \s-1LLVM\s0 version) of the file read by the analyzer. +.IP "\fBFile Size\fR" 4 +.IX Item "File Size" +The size, in bytes, of the entire bitcode file. +.IP "\fBModule Bytes\fR" 4 +.IX Item "Module Bytes" +The size, in bytes, of the module block. Percentage is relative to File Size. +.IP "\fBFunction Bytes\fR" 4 +.IX Item "Function Bytes" +The size, in bytes, of all the function blocks. Percentage is relative to File +Size. +.IP "\fBGlobal Types Bytes\fR" 4 +.IX Item "Global Types Bytes" +The size, in bytes, of the Global Types Pool. Percentage is relative to File +Size. This is the size of the definitions of all types in the bitcode file. +.IP "\fBConstant Pool Bytes\fR" 4 +.IX Item "Constant Pool Bytes" +The size, in bytes, of the Constant Pool Blocks Percentage is relative to File +Size. +.IP "\fBModule Globals Bytes\fR" 4 +.IX Item "Module Globals Bytes" +Ths size, in bytes, of the Global Variable Definitions and their initializers. +Percentage is relative to File Size. +.IP "\fBInstruction List Bytes\fR" 4 +.IX Item "Instruction List Bytes" +The size, in bytes, of all the instruction lists in all the functions. +Percentage is relative to File Size. Note that this value is also included in +the Function Bytes. +.IP "\fBCompaction Table Bytes\fR" 4 +.IX Item "Compaction Table Bytes" +The size, in bytes, of all the compaction tables in all the functions. +Percentage is relative to File Size. Note that this value is also included in +the Function Bytes. +.IP "\fBSymbol Table Bytes\fR" 4 +.IX Item "Symbol Table Bytes" +The size, in bytes, of all the symbol tables in all the functions. Percentage is +relative to File Size. Note that this value is also included in the Function +Bytes. +.IP "\fBDependent Libraries Bytes\fR" 4 +.IX Item "Dependent Libraries Bytes" +The size, in bytes, of the list of dependent libraries in the module. Percentage +is relative to File Size. Note that this value is also included in the Module +Global Bytes. +.IP "\fBNumber Of Bitcode Blocks\fR" 4 +.IX Item "Number Of Bitcode Blocks" +The total number of blocks of any kind in the bitcode file. +.IP "\fBNumber Of Functions\fR" 4 +.IX Item "Number Of Functions" +The total number of function definitions in the bitcode file. +.IP "\fBNumber Of Types\fR" 4 +.IX Item "Number Of Types" +The total number of types defined in the Global Types Pool. +.IP "\fBNumber Of Constants\fR" 4 +.IX Item "Number Of Constants" +The total number of constants (of any type) defined in the Constant Pool. +.IP "\fBNumber Of Basic Blocks\fR" 4 +.IX Item "Number Of Basic Blocks" +The total number of basic blocks defined in all functions in the bitcode file. +.IP "\fBNumber Of Instructions\fR" 4 +.IX Item "Number Of Instructions" +The total number of instructions defined in all functions in the bitcode file. +.IP "\fBNumber Of Long Instructions\fR" 4 +.IX Item "Number Of Long Instructions" +The total number of long instructions defined in all functions in the bitcode +file. Long instructions are those taking greater than 4 bytes. Typically long +instructions are GetElementPtr with several indices, \s-1PHI\s0 nodes, and calls to +functions with large numbers of arguments. +.IP "\fBNumber Of Operands\fR" 4 +.IX Item "Number Of Operands" +The total number of operands used in all instructions in the bitcode file. +.IP "\fBNumber Of Compaction Tables\fR" 4 +.IX Item "Number Of Compaction Tables" +The total number of compaction tables in all functions in the bitcode file. +.IP "\fBNumber Of Symbol Tables\fR" 4 +.IX Item "Number Of Symbol Tables" +The total number of symbol tables in all functions in the bitcode file. +.IP "\fBNumber Of Dependent Libs\fR" 4 +.IX Item "Number Of Dependent Libs" +The total number of dependent libraries found in the bitcode file. +.IP "\fBTotal Instruction Size\fR" 4 +.IX Item "Total Instruction Size" +The total size of the instructions in all functions in the bitcode file. +.IP "\fBAverage Instruction Size\fR" 4 +.IX Item "Average Instruction Size" +The average number of bytes per instruction across all functions in the bitcode +file. This value is computed by dividing Total Instruction Size by Number Of +Instructions. +.IP "\fBMaximum Type Slot Number\fR" 4 +.IX Item "Maximum Type Slot Number" +The maximum value used for a type's slot number. Larger slot number values take +more bytes to encode. +.IP "\fBMaximum Value Slot Number\fR" 4 +.IX Item "Maximum Value Slot Number" +The maximum value used for a value's slot number. Larger slot number values take +more bytes to encode. +.IP "\fBBytes Per Value\fR" 4 +.IX Item "Bytes Per Value" +The average size of a Value definition (of any type). This is computed by +dividing File Size by the total number of values of any type. +.IP "\fBBytes Per Global\fR" 4 +.IX Item "Bytes Per Global" +The average size of a global definition (constants and global variables). +.IP "\fBBytes Per Function\fR" 4 +.IX Item "Bytes Per Function" +The average number of bytes per function definition. This is computed by +dividing Function Bytes by Number Of Functions. +.IP "\fB# of \s-1VBR\s0 32\-bit Integers\fR" 4 +.IX Item "# of VBR 32-bit Integers" +The total number of 32\-bit integers encoded using the Variable Bit Rate +encoding scheme. +.IP "\fB# of \s-1VBR\s0 64\-bit Integers\fR" 4 +.IX Item "# of VBR 64-bit Integers" +The total number of 64\-bit integers encoded using the Variable Bit Rate encoding +scheme. +.IP "\fB# of \s-1VBR\s0 Compressed Bytes\fR" 4 +.IX Item "# of VBR Compressed Bytes" +The total number of bytes consumed by the 32\-bit and 64\-bit integers that use +the Variable Bit Rate encoding scheme. +.IP "\fB# of \s-1VBR\s0 Expanded Bytes\fR" 4 +.IX Item "# of VBR Expanded Bytes" +The total number of bytes that would have been consumed by the 32\-bit and 64\-bit +integers had they not been compressed with the Variable Bit Rage encoding +scheme. +.IP "\fBBytes Saved With \s-1VBR\s0\fR" 4 +.IX Item "Bytes Saved With VBR" +The total number of bytes saved by using the Variable Bit Rate encoding scheme. +The percentage is relative to # of \s-1VBR\s0 Expanded Bytes. +.SH "DETAILED OUTPUT DEFINITIONS" +.IX Header "DETAILED OUTPUT DEFINITIONS" +The following definitions occur only if the \-nodetails option was not given. +The detailed output provides additional information on a per-function basis. +.IP "\fBType\fR" 4 +.IX Item "Type" +The type signature of the function. +.IP "\fBByte Size\fR" 4 +.IX Item "Byte Size" +The total number of bytes in the function's block. +.IP "\fBBasic Blocks\fR" 4 +.IX Item "Basic Blocks" +The number of basic blocks defined by the function. +.IP "\fBInstructions\fR" 4 +.IX Item "Instructions" +The number of instructions defined by the function. +.IP "\fBLong Instructions\fR" 4 +.IX Item "Long Instructions" +The number of instructions using the long instruction format in the function. +.IP "\fBOperands\fR" 4 +.IX Item "Operands" +The number of operands used by all instructions in the function. +.IP "\fBInstruction Size\fR" 4 +.IX Item "Instruction Size" +The number of bytes consumed by instructions in the function. +.IP "\fBAverage Instruction Size\fR" 4 +.IX Item "Average Instruction Size" +The average number of bytes consumed by the instructions in the function. This +value is computed by dividing Instruction Size by Instructions. +.IP "\fBBytes Per Instruction\fR" 4 +.IX Item "Bytes Per Instruction" +The average number of bytes used by the function per instruction. This value is +computed by dividing Byte Size by Instructions. Note that this is not the same +as Average Instruction Size. It computes a number relative to the total function +size not just the size of the instruction list. +.IP "\fBNumber of \s-1VBR\s0 32\-bit Integers\fR" 4 +.IX Item "Number of VBR 32-bit Integers" +The total number of 32\-bit integers found in this function (for any use). +.IP "\fBNumber of \s-1VBR\s0 64\-bit Integers\fR" 4 +.IX Item "Number of VBR 64-bit Integers" +The total number of 64\-bit integers found in this function (for any use). +.IP "\fBNumber of \s-1VBR\s0 Compressed Bytes\fR" 4 +.IX Item "Number of VBR Compressed Bytes" +The total number of bytes in this function consumed by the 32\-bit and 64\-bit +integers that use the Variable Bit Rate encoding scheme. +.IP "\fBNumber of \s-1VBR\s0 Expanded Bytes\fR" 4 +.IX Item "Number of VBR Expanded Bytes" +The total number of bytes in this function that would have been consumed by +the 32\-bit and 64\-bit integers had they not been compressed with the Variable +Bit Rate encoding scheme. +.IP "\fBBytes Saved With \s-1VBR\s0\fR" 4 +.IX Item "Bytes Saved With VBR" +The total number of bytes saved in this function by using the Variable Bit +Rate encoding scheme. The percentage is relative to # of \s-1VBR\s0 Expanded Bytes. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-dis, +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-diff/Makefile b/usr.bin/clang/llvm-diff/Makefile new file mode 100644 index 000000000..aedaff02a --- /dev/null +++ b/usr.bin/clang/llvm-diff/Makefile @@ -0,0 +1,16 @@ +# $FreeBSD$ + +PROG_CXX=llvm-diff + +SRCDIR= tools/llvm-diff +SRCS= llvm-diff.cpp \ + DiffConsumer.cpp \ + DiffLog.cpp \ + DifferenceEngine.cpp + +LIBDEPS=llvmbitreader \ + llvmasmparser \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-diff/llvm-diff.1 b/usr.bin/clang/llvm-diff/llvm-diff.1 new file mode 100644 index 000000000..00110ff6b --- /dev/null +++ b/usr.bin/clang/llvm-diff/llvm-diff.1 @@ -0,0 +1,175 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-DIFF 1" +.TH LLVM-DIFF 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-diff \- LLVM structural 'diff' +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-diff\fR [\fIoptions\fR] \fImodule 1\fR \fImodule 2\fR [\fIglobal name ...\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +\&\fBllvm-diff\fR compares the structure of two \s-1LLVM\s0 modules, primarily +focusing on differences in function definitions. Insignificant +differences, such as changes in the ordering of globals or in the +names of local values, are ignored. +.PP +An input module will be interpreted as an assembly file if its name +ends in '.ll'; otherwise it will be read in as a bitcode file. +.PP +If a list of global names is given, just the values with those names +are compared; otherwise, all global values are compared, and +diagnostics are produced for globals which only appear in one module +or the other. +.PP +\&\fBllvm-diff\fR compares two functions by comparing their basic blocks, +beginning with the entry blocks. If the terminators seem to match, +then the corresponding successors are compared; otherwise they are +ignored. This algorithm is very sensitive to changes in control flow, +which tend to stop any downstream changes from being detected. +.PP +\&\fBllvm-diff\fR is intended as a debugging tool for writers of \s-1LLVM\s0 +passes and frontends. It does not have a stable output format. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-diff\fR finds no differences between the modules, it will exit +with 0 and produce no output. Otherwise it will exit with a non-zero +value. +.SH "BUGS" +.IX Header "BUGS" +Many important differences, like changes in linkage or function +attributes, are not diagnosed. +.PP +Changes in memory behavior (for example, coalescing loads) can cause +massive detected differences in blocks. +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-dis/Makefile b/usr.bin/clang/llvm-dis/Makefile new file mode 100644 index 000000000..831bdbe3a --- /dev/null +++ b/usr.bin/clang/llvm-dis/Makefile @@ -0,0 +1,17 @@ +# $FreeBSD$ + +PROG_CXX=llvm-dis + +SRCDIR= tools/llvm-dis +SRCS= llvm-dis.cpp +LLVM_REQUIRES_EH= + +TGHDRS= Intrinsics +LIBDEPS=llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-dis/llvm-dis.1 b/usr.bin/clang/llvm-dis/llvm-dis.1 new file mode 100644 index 000000000..ee119c0dd --- /dev/null +++ b/usr.bin/clang/llvm-dis/llvm-dis.1 @@ -0,0 +1,175 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-DIS 1" +.TH LLVM-DIS 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-dis \- LLVM disassembler +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-dis\fR [\fIoptions\fR] [\fIfilename\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-dis\fR command is the \s-1LLVM\s0 disassembler. It takes an \s-1LLVM\s0 +bitcode file and converts it into human-readable \s-1LLVM\s0 assembly language. +.PP +If filename is omitted or specified as \f(CW\*(C`\-\*(C'\fR, \fBllvm-dis\fR reads its +input from standard input. +.PP +If the input is being read from standard input, then \fBllvm-dis\fR +will send its output to standard output by default. Otherwise, the +output will be written to a file named after the input file, with +a \f(CW\*(C`.ll\*(C'\fR suffix added (any existing \f(CW\*(C`.bc\*(C'\fR suffix will first be +removed). You can override the choice of output file using the +\&\fB\-o\fR option. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-f\fR" 4 +.IX Item "-f" +Enable binary output on terminals. Normally, \fBllvm-dis\fR will refuse to +write raw bitcode output if the output stream is a terminal. With this option, +\&\fBllvm-dis\fR will write raw bitcode regardless of the output device. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-o\fR \fIfilename\fR" 4 +.IX Item "-o filename" +Specify the output file name. If \fIfilename\fR is \-, then the output is sent +to standard output. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-dis\fR succeeds, it will exit with 0. Otherwise, if an error +occurs, it will exit with a non-zero value. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-as +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-extract/Makefile b/usr.bin/clang/llvm-extract/Makefile new file mode 100644 index 000000000..ffedee406 --- /dev/null +++ b/usr.bin/clang/llvm-extract/Makefile @@ -0,0 +1,22 @@ +# $FreeBSD$ + +PROG_CXX=llvm-extract + +SRCDIR= tools/llvm-extract +SRCS= llvm-extract.cpp + +LIBDEPS=llvmasmparser \ + llvmbitwriter \ + llvmbitreader \ + llvmipo \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-extract/llvm-extract.1 b/usr.bin/clang/llvm-extract/llvm-extract.1 new file mode 100644 index 000000000..de8d34227 --- /dev/null +++ b/usr.bin/clang/llvm-extract/llvm-extract.1 @@ -0,0 +1,195 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-EXTRACT 1" +.TH LLVM-EXTRACT 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-extract \- extract a function from an LLVM module +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-extract\fR [\fIoptions\fR] \fB\-\-func\fR \fIfunction-name\fR [\fIfilename\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-extract\fR command takes the name of a function and extracts it from +the specified \s-1LLVM\s0 bitcode file. It is primarily used as a debugging tool to +reduce test cases from larger programs that are triggering a bug. +.PP +In addition to extracting the bitcode of the specified function, +\&\fBllvm-extract\fR will also remove unreachable global variables, prototypes, and +unused types. +.PP +The \fBllvm-extract\fR command reads its input from standard input if filename is +omitted or if filename is \-. The output is always written to standard output, +unless the \fB\-o\fR option is specified (see below). +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-f\fR" 4 +.IX Item "-f" +Enable binary output on terminals. Normally, \fBllvm-extract\fR will refuse to +write raw bitcode output if the output stream is a terminal. With this option, +\&\fBllvm-extract\fR will write raw bitcode regardless of the output device. +.IP "\fB\-\-func\fR \fIfunction-name\fR" 4 +.IX Item "--func function-name" +Extract the function named \fIfunction-name\fR from the \s-1LLVM\s0 bitcode. May be +specified multiple times to extract multiple functions at once. +.IP "\fB\-\-rfunc\fR \fIfunction-regular-expr\fR" 4 +.IX Item "--rfunc function-regular-expr" +Extract the function(s) matching \fIfunction-regular-expr\fR from the \s-1LLVM\s0 bitcode. +All functions matching the regular expression will be extracted. May be +specified multiple times. +.IP "\fB\-\-glob\fR \fIglobal-name\fR" 4 +.IX Item "--glob global-name" +Extract the global variable named \fIglobal-name\fR from the \s-1LLVM\s0 bitcode. May be +specified multiple times to extract multiple global variables at once. +.IP "\fB\-\-rglob\fR \fIglob-regular-expr\fR" 4 +.IX Item "--rglob glob-regular-expr" +Extract the global variable(s) matching \fIglobal-regular-expr\fR from the \s-1LLVM\s0 +bitcode. All global variables matching the regular expression will be extracted. +May be specified multiple times. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-o\fR \fIfilename\fR" 4 +.IX Item "-o filename" +Specify the output filename. If filename is \*(L"\-\*(R" (the default), then +\&\fBllvm-extract\fR sends its output to standard output. +.IP "\fB\-S\fR" 4 +.IX Item "-S" +Write output in \s-1LLVM\s0 intermediate language (instead of bitcode). +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-extract\fR succeeds, it will exit with 0. Otherwise, if an error +occurs, it will exit with a non-zero value. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +bugpoint +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-ld/Makefile b/usr.bin/clang/llvm-ld/Makefile new file mode 100644 index 000000000..d9c0bbae9 --- /dev/null +++ b/usr.bin/clang/llvm-ld/Makefile @@ -0,0 +1,25 @@ +# $FreeBSD$ + +PROG_CXX=llvm-ld + +SRCDIR= tools/llvm-ld +SRCS= Optimize.cpp \ + llvm-ld.cpp + +TGHDRS= Intrinsics +LIBDEPS=llvmbitwriter \ + llvmlinker \ + llvmarchive \ + llvmbitreader \ + llvmipo \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-ld/llvm-ld.1 b/usr.bin/clang/llvm-ld/llvm-ld.1 new file mode 100644 index 000000000..451c446b0 --- /dev/null +++ b/usr.bin/clang/llvm-ld/llvm-ld.1 @@ -0,0 +1,319 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-LD 1" +.TH LLVM-LD 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-ld \- LLVM linker +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-ld\fR +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-ld\fR tool takes a set of \s-1LLVM\s0 bitcode files and links them +together into a single \s-1LLVM\s0 bitcode file. The output bitcode file can be +another bitcode file or an executable bitcode program. Using additional +options, \fBllvm-ld\fR is able to produce native code executables. +.PP +The \fBllvm-ld\fR tool is the main linker for \s-1LLVM\s0. It is used to link together +the output of \s-1LLVM\s0 front-end compilers and run \*(L"link time\*(R" optimizations (mostly +the inter-procedural kind). +.PP +The \fBllvm-ld\fR tools attempts to mimic the interface provided by the default +system linker so that it can act as a \fIdrop-in\fR replacement. +.SS "Search Order" +.IX Subsection "Search Order" +When looking for objects specified on the command line, \fBllvm-ld\fR will search +for the object first in the current directory and then in the directory +specified by the \fB\s-1LLVM_LIB_SEARCH_PATH\s0\fR environment variable. If it cannot +find the object, it fails. +.PP +When looking for a library specified with the \fB\-l\fR option, \fBllvm-ld\fR first +attempts to load a file with that name from the current directory. If that +fails, it looks for lib\fIlibrary\fR.bc, lib\fIlibrary\fR.a, or lib\fIlibrary\fR.\fIshared +library extension\fR, in that order, in each directory added to the library search +path with the \fB\-L\fR option. These directories are searched in the order they +are specified. If the library cannot be located, then \fBllvm-ld\fR looks in the +directory specified by the \fB\s-1LLVM_LIB_SEARCH_PATH\s0\fR environment variable. If it +does not find a library there, it fails. +.PP +The \fIshared library extension\fR may be \fI.so\fR, \fI.dyld\fR, \fI.dll\fR, or something +different, depending upon the system. +.PP +The \fB\-L\fR option is global. It does not matter where it is specified in the +list of command line arguments; the directory is simply added to the search path +and is applied to all libraries, preceding or succeeding, in the command line. +.SS "Link order" +.IX Subsection "Link order" +All object and bitcode files are linked first in the order they were +specified on the command line. All library files are linked next. +Some libraries may not be linked into the object program; see below. +.SS "Library Linkage" +.IX Subsection "Library Linkage" +Object files and static bitcode objects are always linked into the output +file. Library archives (.a files) load only the objects within the archive +that define symbols needed by the output file. Hence, libraries should be +listed after the object files and libraries which need them; otherwise, the +library may not be linked in, and the dependent library will not have its +undefined symbols defined. +.SS "Native code generation" +.IX Subsection "Native code generation" +The \fBllvm-ld\fR program has limited support for native code generation, when +using the \fB\-native\fR or \fB\-native\-cbe\fR options. Native code generation is +performed by converting the linked bitcode into native assembly (.s) or C code +and running the system compiler (typically gcc) on the result. +.SH "OPTIONS" +.IX Header "OPTIONS" +.SS "General Options" +.IX Subsection "General Options" +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-v\fR" 4 +.IX Item "-v" +Specifies verbose mode. In this mode the linker will print additional +information about the actions it takes, programs it executes, etc. +.IP "\fB\-stats\fR" 4 +.IX Item "-stats" +Print statistics. +.IP "\fB\-time\-passes\fR" 4 +.IX Item "-time-passes" +Record the amount of time needed for each pass and print it to standard +error. +.SS "Input/Output Options" +.IX Subsection "Input/Output Options" +.IP "\fB\-o\fR \fIfilename\fR" 4 +.IX Item "-o filename" +This overrides the default output file and specifies the name of the file that +should be generated by the linker. By default, \fBllvm-ld\fR generates a file named +\&\fIa.out\fR for compatibility with \fBld\fR. The output will be written to +\&\fIfilename\fR. +.IP "\fB\-b\fR \fIfilename\fR" 4 +.IX Item "-b filename" +This option can be used to override the output bitcode file name. By default, +the name of the bitcode output file is one more \*(L".bc\*(R" suffix added to the name +specified by \fB\-o filename\fR option. +.IP "\fB\-l\fR\fIname\fR" 4 +.IX Item "-lname" +This option specifies the \fIname\fR of a library to search when resolving symbols +for the program. Only the base name should be specified as \fIname\fR, without a +\&\fIlib\fR prefix or any suffix. +.IP "\fB\-L\fR\fIPath\fR" 4 +.IX Item "-LPath" +This option tells \fBllvm-ld\fR to look in \fIPath\fR to find any library subsequently +specified with the \fB\-l\fR option. The paths will be searched in the order in +which they are specified on the command line. If the library is still not found, +a small set of system specific directories will also be searched. Note that +libraries specified with the \fB\-l\fR option that occur \fIbefore\fR any \fB\-L\fR options +will not search the paths given by the \fB\-L\fR options following it. +.IP "\fB\-link\-as\-library\fR" 4 +.IX Item "-link-as-library" +Link the bitcode files together as a library, not an executable. In this mode, +undefined symbols will be permitted. +.IP "\fB\-r\fR" 4 +.IX Item "-r" +An alias for \-link\-as\-library. +.IP "\fB\-native\fR" 4 +.IX Item "-native" +Generate a native machine code executable. +.Sp +When generating native executables, \fBllvm-ld\fR first checks for a bitcode +version of the library and links it in, if necessary. If the library is +missing, \fBllvm-ld\fR skips it. Then, \fBllvm-ld\fR links in the same +libraries as native code. +.Sp +In this way, \fBllvm-ld\fR should be able to link in optimized bitcode +subsets of common libraries and then link in any part of the library that +hasn't been converted to bitcode. +.IP "\fB\-native\-cbe\fR" 4 +.IX Item "-native-cbe" +Generate a native machine code executable with the \s-1LLVM\s0 C backend. +.Sp +This option is identical to the \fB\-native\fR option, but uses the +C backend to generate code for the program instead of an \s-1LLVM\s0 native +code generator. +.SS "Optimization Options" +.IX Subsection "Optimization Options" +.IP "\fB\-disable\-inlining\fR" 4 +.IX Item "-disable-inlining" +Do not run the inlining pass. Functions will not be inlined into other +functions. +.IP "\fB\-disable\-opt\fR" 4 +.IX Item "-disable-opt" +Completely disable optimization. +.IP "\fB\-disable\-internalize\fR" 4 +.IX Item "-disable-internalize" +Do not mark all symbols as internal. +.IP "\fB\-verify\-each\fR" 4 +.IX Item "-verify-each" +Run the verification pass after each of the passes to verify intermediate +results. +.IP "\fB\-strip\-all\fR" 4 +.IX Item "-strip-all" +Strip all debug and symbol information from the executable to make it smaller. +.IP "\fB\-strip\-debug\fR" 4 +.IX Item "-strip-debug" +Strip all debug information from the executable to make it smaller. +.IP "\fB\-s\fR" 4 +.IX Item "-s" +An alias for \fB\-strip\-all\fR. +.IP "\fB\-S\fR" 4 +.IX Item "-S" +An alias for \fB\-strip\-debug\fR. +.IP "\fB\-export\-dynamic\fR" 4 +.IX Item "-export-dynamic" +An alias for \fB\-disable\-internalize\fR +.IP "\fB\-post\-link\-opt\fR\fIPath\fR" 4 +.IX Item "-post-link-optPath" +Run post-link optimization program. After linking is completed a bitcode file +will be generated. It will be passed to the program specified by \fIPath\fR as the +first argument. The second argument to the program will be the name of a +temporary file into which the program should place its optimized output. For +example, the \*(L"no-op optimization\*(R" would be a simple shell script: +.Sp +.Vb 2 +\& #!/bin/bash +\& cp $1 $2 +.Ve +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-ld\fR succeeds, it will exit with 0 return code. If an error occurs, +it will exit with a non-zero return code. +.SH "ENVIRONMENT" +.IX Header "ENVIRONMENT" +The \f(CW\*(C`LLVM_LIB_SEARCH_PATH\*(C'\fR environment variable is used to find bitcode +libraries. Any paths specified in this variable will be searched after the \f(CW\*(C`\-L\*(C'\fR +options. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-link +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-link/Makefile b/usr.bin/clang/llvm-link/Makefile new file mode 100644 index 000000000..25d33131b --- /dev/null +++ b/usr.bin/clang/llvm-link/Makefile @@ -0,0 +1,21 @@ +# $FreeBSD$ + +PROG_CXX=llvm-link + +SRCDIR= tools/llvm-link +SRCS= llvm-link.cpp + +LIBDEPS=llvmasmparser \ + llvmbitwriter \ + llvmlinker \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmarchive \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-link/llvm-link.1 b/usr.bin/clang/llvm-link/llvm-link.1 new file mode 100644 index 000000000..fec826aea --- /dev/null +++ b/usr.bin/clang/llvm-link/llvm-link.1 @@ -0,0 +1,190 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-LINK 1" +.TH LLVM-LINK 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-link \- LLVM linker +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-link\fR [\fIoptions\fR] \fIfilename ...\fR +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +\&\fBllvm-link\fR takes several \s-1LLVM\s0 bitcode files and links them together into a +single \s-1LLVM\s0 bitcode file. It writes the output file to standard output, unless +the \fB\-o\fR option is used to specify a filename. +.PP +\&\fBllvm-link\fR attempts to load the input files from the current directory. If +that fails, it looks for each file in each of the directories specified by the +\&\fB\-L\fR options on the command line. The library search paths are global; each +one is searched for every input file if necessary. The directories are searched +in the order they were specified on the command line. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-L\fR \fIdirectory\fR" 4 +.IX Item "-L directory" +Add the specified \fIdirectory\fR to the library search path. When looking for +libraries, \fBllvm-link\fR will look in path name for libraries. This option can be +specified multiple times; \fBllvm-link\fR will search inside these directories in +the order in which they were specified on the command line. +.IP "\fB\-f\fR" 4 +.IX Item "-f" +Enable binary output on terminals. Normally, \fBllvm-link\fR will refuse to +write raw bitcode output if the output stream is a terminal. With this option, +\&\fBllvm-link\fR will write raw bitcode regardless of the output device. +.IP "\fB\-o\fR \fIfilename\fR" 4 +.IX Item "-o filename" +Specify the output file name. If \fIfilename\fR is \f(CW\*(C`\-\*(C'\fR, then \fBllvm-link\fR will +write its output to standard output. +.IP "\fB\-S\fR" 4 +.IX Item "-S" +Write output in \s-1LLVM\s0 intermediate language (instead of bitcode). +.IP "\fB\-d\fR" 4 +.IX Item "-d" +If specified, \fBllvm-link\fR prints a human-readable version of the output +bitcode file to standard error. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-v\fR" 4 +.IX Item "-v" +Verbose mode. Print information about what \fBllvm-link\fR is doing. This +typically includes a message for each bitcode file linked in and for each +library found. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-link\fR succeeds, it will exit with 0. Otherwise, if an error +occurs, it will exit with a non-zero value. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +gccld +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-mc/Makefile b/usr.bin/clang/llvm-mc/Makefile new file mode 100644 index 000000000..337218264 --- /dev/null +++ b/usr.bin/clang/llvm-mc/Makefile @@ -0,0 +1,46 @@ +# $FreeBSD$ + +PROG_CXX=llvm-mc +NO_MAN= + +SRCDIR= tools/llvm-mc +SRCS= llvm-mc.cpp \ + Disassembler.cpp + +LIBDEPS=llvmmcdisassembler \ + llvmarmdisassembler \ + llvmarmasmparser \ + llvmarmcodegen \ + llvmarmdesc \ + llvmarminstprinter \ + llvmarminfo \ + llvmmipscodegen \ + llvmmipsdesc \ + llvmmipsinstprinter \ + llvmmipsinfo \ + llvmpowerpccodegen \ + llvmpowerpcdesc \ + llvmpowerpcinstprinter \ + llvmpowerpcinfo \ + llvmx86disassembler \ + llvmx86asmparser \ + llvmx86codegen \ + llvmx86desc \ + llvmselectiondag \ + llvmasmprinter \ + llvmmcparser \ + llvmcodegen \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmx86instprinter \ + llvmx86utils \ + llvmcore \ + llvmx86info \ + llvmmc \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-nm/Makefile b/usr.bin/clang/llvm-nm/Makefile new file mode 100644 index 000000000..1ac3a3d5d --- /dev/null +++ b/usr.bin/clang/llvm-nm/Makefile @@ -0,0 +1,14 @@ +# $FreeBSD$ + +PROG_CXX=llvm-nm + +SRCDIR= tools/llvm-nm +SRCS= llvm-nm.cpp + +LIBDEPS=llvmobject \ + llvmarchive \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-nm/llvm-nm.1 b/usr.bin/clang/llvm-nm/llvm-nm.1 new file mode 100644 index 000000000..c72e3bb33 --- /dev/null +++ b/usr.bin/clang/llvm-nm/llvm-nm.1 @@ -0,0 +1,219 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-NM 1" +.TH LLVM-NM 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-nm \- list LLVM bitcode file's symbol table +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-nm\fR [\fIoptions\fR] [\fIfilenames...\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-nm\fR utility lists the names of symbols from the \s-1LLVM\s0 bitcode files, +or \fBar\fR archives containing \s-1LLVM\s0 bitcode files, named on the command line. +Each symbol is listed along with some simple information about its provenance. +If no file name is specified, or \fI\-\fR is used as a file name, \fBllvm-nm\fR will +process a bitcode file on its standard input stream. +.PP +\&\fBllvm-nm\fR's default output format is the traditional \s-1BSD\s0 \fBnm\fR output format. +Each such output record consists of an (optional) 8\-digit hexadecimal address, +followed by a type code character, followed by a name, for each symbol. One +record is printed per line; fields are separated by spaces. When the address is +omitted, it is replaced by 8 spaces. +.PP +Type code characters currently supported, and their meanings, are as follows: +.IP "U" 4 +.IX Item "U" +Named object is referenced but undefined in this bitcode file +.IP "C" 4 +.IX Item "C" +Common (multiple definitions link together into one def) +.IP "W" 4 +.IX Item "W" +Weak reference (multiple definitions link together into zero or one definitions) +.IP "t" 4 +.IX Item "t" +Local function (text) object +.IP "T" 4 +.IX Item "T" +Global function (text) object +.IP "d" 4 +.IX Item "d" +Local data object +.IP "D" 4 +.IX Item "D" +Global data object +.IP "?" 4 +Something unrecognizable +.PP +Because \s-1LLVM\s0 bitcode files typically contain objects that are not considered to +have addresses until they are linked into an executable image or dynamically +compiled \*(L"just-in-time\*(R", \fBllvm-nm\fR does not print an address for any symbol, +even symbols which are defined in the bitcode file. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-P\fR" 4 +.IX Item "-P" +Use \s-1POSIX\s0.2 output format. Alias for \fB\-\-format=posix\fR. +.IP "\fB\-B\fR (default)" 4 +.IX Item "-B (default)" +Use \s-1BSD\s0 output format. Alias for \fB\-\-format=bsd\fR. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command-line options and their meanings. +.IP "\fB\-\-defined\-only\fR" 4 +.IX Item "--defined-only" +Print only symbols defined in this bitcode file (as opposed to +symbols which may be referenced by objects in this file, but not +defined in this file.) +.IP "\fB\-\-extern\-only\fR, \fB\-g\fR" 4 +.IX Item "--extern-only, -g" +Print only symbols whose definitions are external; that is, accessible +from other bitcode files. +.IP "\fB\-\-undefined\-only\fR, \fB\-u\fR" 4 +.IX Item "--undefined-only, -u" +Print only symbols referenced but not defined in this bitcode file. +.IP "\fB\-\-format=\fR\fIfmt\fR, \fB\-f\fR" 4 +.IX Item "--format=fmt, -f" +Select an output format; \fIfmt\fR may be \fIsysv\fR, \fIposix\fR, or \fIbsd\fR. The +default is \fIbsd\fR. +.SH "BUGS" +.IX Header "BUGS" +\&\fBllvm-nm\fR cannot demangle \*(C+ mangled names, like \s-1GNU\s0 \fBnm\fR can. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +\&\fBllvm-nm\fR exits with an exit code of zero. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-dis, \fIar\fR\|(1), \fInm\fR\|(1) +.SH "AUTHOR" +.IX Header "AUTHOR" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-objdump/Makefile b/usr.bin/clang/llvm-objdump/Makefile new file mode 100644 index 000000000..a98cae506 --- /dev/null +++ b/usr.bin/clang/llvm-objdump/Makefile @@ -0,0 +1,49 @@ +# $FreeBSD$ + +PROG_CXX=llvm-objdump +NO_MAN= + +SRCDIR= tools/llvm-objdump +SRCS= llvm-objdump.cpp \ + MachODump.cpp \ + MCFunction.cpp \ + +LIBDEPS=llvmobject \ + llvmmcdisassembler \ + llvmdebuginfo \ + llvmarmdisassembler \ + llvmarmasmparser \ + llvmarmcodegen \ + llvmarmdesc \ + llvmarminstprinter \ + llvmarminfo \ + llvmmipscodegen \ + llvmmipsdesc \ + llvmmipsinstprinter \ + llvmmipsinfo \ + llvmpowerpccodegen \ + llvmpowerpcdesc \ + llvmpowerpcinstprinter \ + llvmpowerpcinfo \ + llvmx86disassembler \ + llvmx86asmparser \ + llvmx86codegen \ + llvmx86desc \ + llvmselectiondag \ + llvmasmprinter \ + llvmmcparser \ + llvmcodegen \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmx86instprinter \ + llvmx86utils \ + llvmcore \ + llvmx86info \ + llvmmc \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-prof/Makefile b/usr.bin/clang/llvm-prof/Makefile new file mode 100644 index 000000000..fbe5df957 --- /dev/null +++ b/usr.bin/clang/llvm-prof/Makefile @@ -0,0 +1,15 @@ +# $FreeBSD$ + +PROG_CXX=llvm-prof + +SRCDIR= tools/llvm-prof +SRCS= llvm-prof.cpp + +LIBDEPS=llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-prof/llvm-prof.1 b/usr.bin/clang/llvm-prof/llvm-prof.1 new file mode 100644 index 000000000..f7659fb62 --- /dev/null +++ b/usr.bin/clang/llvm-prof/llvm-prof.1 @@ -0,0 +1,173 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-PROF 1" +.TH LLVM-PROF 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-prof \- print execution profile of LLVM program +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-prof\fR [\fIoptions\fR] [\fIbitcode file\fR] [\fIllvmprof.out\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-prof\fR tool reads in an \fIllvmprof.out\fR file (which can +optionally use a specific file with the third program argument), a bitcode file +for the program, and produces a human readable report, suitable for determining +where the program hotspots are. +.PP +This program is often used in conjunction with the \fIutils/profile.pl\fR +script. This script automatically instruments a program, runs it with the \s-1JIT\s0, +then runs \fBllvm-prof\fR to format a report. To get more information about +\&\fIutils/profile.pl\fR, execute it with the \fB\-help\fR option. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-\-annotated\-llvm\fR or \fB\-A\fR" 4 +.IX Item "--annotated-llvm or -A" +In addition to the normal report printed, print out the code for the +program, annotated with execution frequency information. This can be +particularly useful when trying to visualize how frequently basic blocks +are executed. This is most useful with basic block profiling +information or better. +.IP "\fB\-\-print\-all\-code\fR" 4 +.IX Item "--print-all-code" +Using this option enables the \fB\-\-annotated\-llvm\fR option, but it +prints the entire module, instead of just the most commonly executed +functions. +.IP "\fB\-\-time\-passes\fR" 4 +.IX Item "--time-passes" +Record the amount of time needed for each pass and print it to standard +error. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +\&\fBllvm-prof\fR returns 1 if it cannot load the bitcode file or the profile +information. Otherwise, it exits with zero. +.SH "AUTHOR" +.IX Header "AUTHOR" +\&\fBllvm-prof\fR is maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-ranlib/Makefile b/usr.bin/clang/llvm-ranlib/Makefile new file mode 100644 index 000000000..80e495cfb --- /dev/null +++ b/usr.bin/clang/llvm-ranlib/Makefile @@ -0,0 +1,14 @@ +# $FreeBSD$ + +PROG_CXX=llvm-ranlib + +SRCDIR= tools/llvm-ranlib +SRCS= llvm-ranlib.cpp +LLVM_REQUIRES_EH= + +LIBDEPS=llvmarchive \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-ranlib/llvm-ranlib.1 b/usr.bin/clang/llvm-ranlib/llvm-ranlib.1 new file mode 100644 index 000000000..08a3ef0ea --- /dev/null +++ b/usr.bin/clang/llvm-ranlib/llvm-ranlib.1 @@ -0,0 +1,167 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "LLVM-RANLIB 1" +.TH LLVM-RANLIB 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +llvm\-ranlib \- Generate index for LLVM archive +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBllvm-ranlib\fR [\-\-version] [\-help] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBllvm-ranlib\fR command is similar to the common Unix utility, \f(CW\*(C`ranlib\*(C'\fR. It +adds or updates the symbol table in an \s-1LLVM\s0 archive file. Note that using the +\&\fBllvm-ar\fR modifier \fIs\fR is usually more efficient than running \fBllvm-ranlib\fR +which is only provided only for completness and compatibility. Unlike other +implementations of \f(CW\*(C`ranlib\*(C'\fR, \fBllvm-ranlib\fR indexes \s-1LLVM\s0 bitcode files, not +native object modules. You can list the contents of the symbol table with the +\&\f(CW\*(C`llvm\-nm \-s\*(C'\fR command. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fIarchive-file\fR" 4 +.IX Item "archive-file" +Specifies the archive-file to which the symbol table is added or updated. +.IP "\fI\-\-version\fR" 4 +.IX Item "--version" +Print the version of \fBllvm-ranlib\fR and exit without building a symbol table. +.IP "\fI\-help\fR" 4 +.IX Item "-help" +Print usage help for \fBllvm-ranlib\fR and exit without building a symbol table. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBllvm-ranlib\fR succeeds, it will exit with 0. If an error occurs, a non-zero +exit code will be returned. +.SH "SEE ALSO" +.IX Header "SEE ALSO" +llvm-ar, \fIranlib\fR\|(1) +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). diff --git a/usr.bin/clang/llvm-rtdyld/Makefile b/usr.bin/clang/llvm-rtdyld/Makefile new file mode 100644 index 000000000..489307a86 --- /dev/null +++ b/usr.bin/clang/llvm-rtdyld/Makefile @@ -0,0 +1,48 @@ +# $FreeBSD$ + +PROG_CXX=llvm-rtdyld +NO_MAN= + +SRCDIR= tools/llvm-rtdyld +SRCS= llvm-rtdyld.cpp + +LIBDEPS=llvmjit \ + llvmexecutionengine \ + llvmruntimedyld \ + llvmobject \ + llvmarmdisassembler \ + llvmarmasmparser \ + llvmarmcodegen \ + llvmarmdesc \ + llvmarminstprinter \ + llvmarminfo \ + llvmmipscodegen \ + llvmmipsdesc \ + llvmmipsinstprinter \ + llvmmipsinfo \ + llvmpowerpccodegen \ + llvmpowerpcdesc \ + llvmpowerpcinstprinter \ + llvmpowerpcinfo \ + llvmx86disassembler \ + llvmx86asmparser \ + llvmx86codegen \ + llvmx86desc \ + llvmselectiondag \ + llvmasmprinter \ + llvmmcparser \ + llvmcodegen \ + llvmscalaropts \ + llvminstcombine \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmx86instprinter \ + llvmx86utils \ + llvmcore \ + llvmx86info \ + llvmmc \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/llvm-stub/Makefile b/usr.bin/clang/llvm-stub/Makefile new file mode 100644 index 000000000..56e76605a --- /dev/null +++ b/usr.bin/clang/llvm-stub/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +PROG= llvm-stub +NO_MAN= + +SRCDIR= tools/llvm-stub +SRCS= llvm-stub.c + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/macho-dump/Makefile b/usr.bin/clang/macho-dump/Makefile new file mode 100644 index 000000000..cb170d651 --- /dev/null +++ b/usr.bin/clang/macho-dump/Makefile @@ -0,0 +1,12 @@ +# $FreeBSD$ + +PROG_CXX=macho-dump +NO_MAN= + +SRCDIR= tools/macho-dump +SRCS= macho-dump.cpp + +LIBDEPS=llvmobject \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/opt/Makefile b/usr.bin/clang/opt/Makefile new file mode 100644 index 000000000..50d0c8831 --- /dev/null +++ b/usr.bin/clang/opt/Makefile @@ -0,0 +1,27 @@ +# $FreeBSD$ + +PROG_CXX=opt + +SRCDIR= tools/opt +SRCS= AnalysisWrappers.cpp \ + GraphPrinters.cpp \ + PrintSCC.cpp \ + opt.cpp + +TGHDRS= Intrinsics +LIBDEPS=llvmipo \ + llvmscalaropts \ + llvminstcombine \ + llvminstrumentation \ + llvmtransformutils \ + llvmipa \ + llvmanalysis \ + llvmtarget \ + llvmmc \ + llvmasmparser \ + llvmbitwriter \ + llvmbitreader \ + llvmcore \ + llvmsupport + +.include "../clang.prog.mk" diff --git a/usr.bin/clang/opt/opt.1 b/usr.bin/clang/opt/opt.1 new file mode 100644 index 000000000..95fdd076e --- /dev/null +++ b/usr.bin/clang/opt/opt.1 @@ -0,0 +1,250 @@ +.\" $FreeBSD$ +.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sp \" Vertical space (when we can't use .PP) +.if t .sp .5v +.if n .sp +.. +.de Vb \" Begin verbatim text +.ft CW +.nf +.ne \\$1 +.. +.de Ve \" End verbatim text +.ft R +.fi +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. \*(C+ will +.\" give a nicer C++. Capital omega is used to do unbreakable dashes and +.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, +.\" nothing in troff, for use with C<>. +.tr \(*W- +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" Escape single quotes in literal strings from groff's Unicode transform. +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.ie \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.el \{\ +. de IX +.. +.\} +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "OPT 1" +.TH OPT 1 "2011-10-17" "LLVM 3.0" "LLVM Command Guide" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.if n .ad l +.nh +.SH "NAME" +opt \- LLVM optimizer +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +\&\fBopt\fR [\fIoptions\fR] [\fIfilename\fR] +.SH "DESCRIPTION" +.IX Header "DESCRIPTION" +The \fBopt\fR command is the modular \s-1LLVM\s0 optimizer and analyzer. It takes \s-1LLVM\s0 +source files as input, runs the specified optimizations or analyses on it, and then +outputs the optimized file or the analysis results. The function of +\&\fBopt\fR depends on whether the \fB\-analyze\fR option is given. +.PP +When \fB\-analyze\fR is specified, \fBopt\fR performs various analyses of the input +source. It will usually print the results on standard output, but in a few +cases, it will print output to standard error or generate a file with the +analysis output, which is usually done when the output is meant for another +program. +.PP +While \fB\-analyze\fR is \fInot\fR given, \fBopt\fR attempts to produce an optimized +output file. The optimizations available via \fBopt\fR depend upon what +libraries were linked into it as well as any additional libraries that have +been loaded with the \fB\-load\fR option. Use the \fB\-help\fR option to determine +what optimizations you can use. +.PP +If \fIfilename\fR is omitted from the command line or is \fI\-\fR, \fBopt\fR reads its +input from standard input. Inputs can be in either the \s-1LLVM\s0 assembly language +format (.ll) or the \s-1LLVM\s0 bitcode format (.bc). +.PP +If an output filename is not specified with the \fB\-o\fR option, \fBopt\fR +writes its output to the standard output. +.SH "OPTIONS" +.IX Header "OPTIONS" +.IP "\fB\-f\fR" 4 +.IX Item "-f" +Enable binary output on terminals. Normally, \fBopt\fR will refuse to +write raw bitcode output if the output stream is a terminal. With this option, +\&\fBopt\fR will write raw bitcode regardless of the output device. +.IP "\fB\-help\fR" 4 +.IX Item "-help" +Print a summary of command line options. +.IP "\fB\-o\fR \fIfilename\fR" 4 +.IX Item "-o filename" +Specify the output filename. +.IP "\fB\-S\fR" 4 +.IX Item "-S" +Write output in \s-1LLVM\s0 intermediate language (instead of bitcode). +.IP "\fB\-{passname}\fR" 4 +.IX Item "-{passname}" +\&\fBopt\fR provides the ability to run any of \s-1LLVM\s0's optimization or analysis passes +in any order. The \fB\-help\fR option lists all the passes available. The order in +which the options occur on the command line are the order in which they are +executed (within pass constraints). +.IP "\fB\-std\-compile\-opts\fR" 4 +.IX Item "-std-compile-opts" +This is short hand for a standard list of \fIcompile time optimization\fR passes. +This is typically used to optimize the output from the llvm-gcc front end. It +might be useful for other front end compilers as well. To discover the full set +of options available, use the following command: +.Sp +.Vb 1 +\& llvm\-as < /dev/null | opt \-std\-compile\-opts \-disable\-output \-debug\-pass=Arguments +.Ve +.IP "\fB\-disable\-inlining\fR" 4 +.IX Item "-disable-inlining" +This option is only meaningful when \fB\-std\-compile\-opts\fR is given. It simply +removes the inlining pass from the standard list. +.IP "\fB\-disable\-opt\fR" 4 +.IX Item "-disable-opt" +This option is only meaningful when \fB\-std\-compile\-opts\fR is given. It disables +most, but not all, of the \fB\-std\-compile\-opts\fR. The ones that remain are +\&\fB\-verify\fR, \fB\-lower\-setjmp\fR, and \fB\-funcresolve\fR. +.IP "\fB\-strip\-debug\fR" 4 +.IX Item "-strip-debug" +This option causes opt to strip debug information from the module before +applying other optimizations. It is essentially the same as \fB\-strip\fR but it +ensures that stripping of debug information is done first. +.IP "\fB\-verify\-each\fR" 4 +.IX Item "-verify-each" +This option causes opt to add a verify pass after every pass otherwise specified +on the command line (including \fB\-verify\fR). This is useful for cases where it +is suspected that a pass is creating an invalid module but it is not clear which +pass is doing it. The combination of \fB\-std\-compile\-opts\fR and \fB\-verify\-each\fR +can quickly track down this kind of problem. +.IP "\fB\-profile\-info\-file\fR \fIfilename\fR" 4 +.IX Item "-profile-info-file filename" +Specify the name of the file loaded by the \-profile\-loader option. +.IP "\fB\-stats\fR" 4 +.IX Item "-stats" +Print statistics. +.IP "\fB\-time\-passes\fR" 4 +.IX Item "-time-passes" +Record the amount of time needed for each pass and print it to standard +error. +.IP "\fB\-debug\fR" 4 +.IX Item "-debug" +If this is a debug build, this option will enable debug printouts +from passes which use the \fI\s-1\fIDEBUG\s0()\fI\fR macro. See the \fB\s-1LLVM\s0 Programmer's +Manual\fR, section \fI#DEBUG\fR for more information. +.IP "\fB\-load\fR=\fIplugin\fR" 4 +.IX Item "-load=plugin" +Load the dynamic object \fIplugin\fR. This object should register new optimization +or analysis passes. Once loaded, the object will add new command line options to +enable various optimizations or analyses. To see the new complete list of +optimizations, use the \fB\-help\fR and \fB\-load\fR options together. For example: +.Sp +.Vb 1 +\& opt \-load=plugin.so \-help +.Ve +.IP "\fB\-p\fR" 4 +.IX Item "-p" +Print module after each transformation. +.SH "EXIT STATUS" +.IX Header "EXIT STATUS" +If \fBopt\fR succeeds, it will exit with 0. Otherwise, if an error +occurs, it will exit with a non-zero value. +.SH "AUTHORS" +.IX Header "AUTHORS" +Maintained by the \s-1LLVM\s0 Team (). -- 2.45.0