Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/llvm2ice.cpp - Driver for testing ----------------------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a driver that uses LLVM capabilities to parse a |
| 11 | // bitcode file and build the LLVM IR, and then convert the LLVM basic |
| 12 | // blocks, instructions, and operands into their Subzero equivalents. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 16 | #include "IceCfg.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 17 | #include "IceClFlags.h" |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 18 | #include "IceConverter.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 19 | #include "PNaClTranslator.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 20 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 22 | #include "llvm/IRReader/IRReader.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 24 | #include "llvm/Support/raw_os_ostream.h" |
| 25 | #include "llvm/Support/SourceMgr.h" |
| 26 | |
| 27 | #include <fstream> |
| 28 | #include <iostream> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 32 | static cl::list<Ice::VerboseItem> VerboseList( |
| 33 | "verbose", cl::CommaSeparated, |
| 34 | cl::desc("Verbose options (can be comma-separated):"), |
| 35 | cl::values( |
| 36 | clEnumValN(Ice::IceV_Instructions, "inst", "Print basic instructions"), |
| 37 | clEnumValN(Ice::IceV_Deleted, "del", "Include deleted instructions"), |
| 38 | clEnumValN(Ice::IceV_InstNumbers, "instnum", |
| 39 | "Print instruction numbers"), |
| 40 | clEnumValN(Ice::IceV_Preds, "pred", "Show predecessors"), |
| 41 | clEnumValN(Ice::IceV_Succs, "succ", "Show successors"), |
| 42 | clEnumValN(Ice::IceV_Liveness, "live", "Liveness information"), |
| 43 | clEnumValN(Ice::IceV_RegManager, "rmgr", "Register manager status"), |
| 44 | clEnumValN(Ice::IceV_RegOrigins, "orig", "Physical register origins"), |
| 45 | clEnumValN(Ice::IceV_LinearScan, "regalloc", "Linear scan details"), |
| 46 | clEnumValN(Ice::IceV_Frame, "frame", "Stack frame layout details"), |
| 47 | clEnumValN(Ice::IceV_Timing, "time", "Pass timing details"), |
| 48 | clEnumValN(Ice::IceV_All, "all", "Use all verbose options"), |
| 49 | clEnumValN(Ice::IceV_None, "none", "No verbosity"), clEnumValEnd)); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 50 | static cl::opt<Ice::TargetArch> TargetArch( |
| 51 | "target", cl::desc("Target architecture:"), cl::init(Ice::Target_X8632), |
| 52 | cl::values( |
| 53 | clEnumValN(Ice::Target_X8632, "x8632", "x86-32"), |
| 54 | clEnumValN(Ice::Target_X8632, "x86-32", "x86-32 (same as x8632)"), |
| 55 | clEnumValN(Ice::Target_X8632, "x86_32", "x86-32 (same as x8632)"), |
| 56 | clEnumValN(Ice::Target_X8664, "x8664", "x86-64"), |
| 57 | clEnumValN(Ice::Target_X8664, "x86-64", "x86-64 (same as x8664)"), |
| 58 | clEnumValN(Ice::Target_X8664, "x86_64", "x86-64 (same as x8664)"), |
| 59 | clEnumValN(Ice::Target_ARM32, "arm", "arm32"), |
| 60 | clEnumValN(Ice::Target_ARM32, "arm32", "arm32 (same as arm)"), |
| 61 | clEnumValN(Ice::Target_ARM64, "arm64", "arm64"), clEnumValEnd)); |
| 62 | static cl::opt<Ice::OptLevel> |
| 63 | OptLevel(cl::desc("Optimization level"), cl::init(Ice::Opt_m1), |
| 64 | cl::value_desc("level"), |
| 65 | cl::values(clEnumValN(Ice::Opt_m1, "Om1", "-1"), |
| 66 | clEnumValN(Ice::Opt_m1, "O-1", "-1"), |
| 67 | clEnumValN(Ice::Opt_0, "O0", "0"), |
| 68 | clEnumValN(Ice::Opt_1, "O1", "1"), |
| 69 | clEnumValN(Ice::Opt_2, "O2", "2"), clEnumValEnd)); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 70 | static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"), |
Karl Schimpf | a667fb8 | 2014-05-19 14:56:51 -0700 | [diff] [blame] | 71 | cl::init("-")); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 72 | static cl::opt<std::string> OutputFilename("o", |
| 73 | cl::desc("Override output filename"), |
| 74 | cl::init("-"), |
| 75 | cl::value_desc("filename")); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 76 | static cl::opt<std::string> LogFilename("log", cl::desc("Set log filename"), |
| 77 | cl::init("-"), |
| 78 | cl::value_desc("filename")); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 79 | static cl::opt<std::string> |
| 80 | TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"), |
| 81 | cl::init(""), cl::value_desc("prefix")); |
| 82 | static cl::opt<bool> |
| 83 | DisableInternal("external", |
| 84 | cl::desc("Disable 'internal' linkage type for testing")); |
| 85 | static cl::opt<bool> |
| 86 | DisableTranslation("notranslate", cl::desc("Disable Subzero translation")); |
| 87 | |
| 88 | static cl::opt<bool> SubzeroTimingEnabled( |
| 89 | "timing", cl::desc("Enable breakdown timing of Subzero translation")); |
| 90 | |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 91 | static cl::opt<NaClFileFormat> InputFileFormat( |
| 92 | "bitcode-format", cl::desc("Define format of input file:"), |
| 93 | cl::values(clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"), |
| 94 | clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"), |
| 95 | clEnumValEnd), |
Karl Schimpf | a667fb8 | 2014-05-19 14:56:51 -0700 | [diff] [blame] | 96 | cl::init(LLVMFormat)); |
| 97 | |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 98 | static cl::opt<bool> |
| 99 | BuildOnRead("build-on-read", |
| 100 | cl::desc("Build ICE instructions when reading bitcode"), |
| 101 | cl::init(false)); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 102 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 103 | int main(int argc, char **argv) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 104 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 105 | cl::ParseCommandLineOptions(argc, argv); |
| 106 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 107 | Ice::VerboseMask VMask = Ice::IceV_None; |
| 108 | for (unsigned i = 0; i != VerboseList.size(); ++i) |
| 109 | VMask |= VerboseList[i]; |
| 110 | |
| 111 | std::ofstream Ofs; |
| 112 | if (OutputFilename != "-") { |
| 113 | Ofs.open(OutputFilename.c_str(), std::ofstream::out); |
| 114 | } |
| 115 | raw_os_ostream *Os = |
| 116 | new raw_os_ostream(OutputFilename == "-" ? std::cout : Ofs); |
| 117 | Os->SetUnbuffered(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 118 | std::ofstream Lfs; |
| 119 | if (LogFilename != "-") { |
| 120 | Lfs.open(LogFilename.c_str(), std::ofstream::out); |
| 121 | } |
| 122 | raw_os_ostream *Ls = new raw_os_ostream(LogFilename == "-" ? std::cout : Lfs); |
| 123 | Ls->SetUnbuffered(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 124 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 125 | Ice::GlobalContext Ctx(Ls, Os, VMask, TargetArch, OptLevel, TestPrefix); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 126 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 127 | Ice::ClFlags Flags; |
| 128 | Flags.DisableInternal = DisableInternal; |
| 129 | Flags.SubzeroTimingEnabled = SubzeroTimingEnabled; |
| 130 | Flags.DisableTranslation = DisableTranslation; |
| 131 | |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 132 | if (BuildOnRead) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 133 | Ice::PNaClTranslator Translator(&Ctx, Flags); |
| 134 | Translator.translate(IRFilename); |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 135 | return Translator.getErrorStatus(); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 136 | } else { |
| 137 | // Parse the input LLVM IR file into a module. |
| 138 | SMDiagnostic Err; |
| 139 | Ice::Timer T; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 140 | Module *Mod = |
| 141 | NaClParseIRFile(IRFilename, InputFileFormat, Err, getGlobalContext()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 142 | |
| 143 | if (SubzeroTimingEnabled) { |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 144 | std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec() |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 145 | << " sec\n"; |
| 146 | } |
| 147 | |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 148 | if (!Mod) { |
| 149 | Err.print(argv[0], errs()); |
| 150 | return 1; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 151 | } |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 152 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 153 | Ice::Converter Converter(&Ctx, Flags); |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 154 | Converter.convertToIce(Mod); |
| 155 | return Converter.getErrorStatus(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 156 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 157 | } |