blob: bf7de31a233df13fbae0cdc575e1a709e8c412d8 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- 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 Stichnothde4ca712014-06-29 08:13:48 -070016#include "IceCfg.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070017#include "IceClFlags.h"
Karl Schimpfe1e013c2014-06-27 09:15:29 -070018#include "IceConverter.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070019#include "PNaClTranslator.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070020
Jim Stichnothf7c9a142014-04-29 10:52:43 -070021#include "llvm/IR/LLVMContext.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070022#include "llvm/IRReader/IRReader.h"
23#include "llvm/Support/CommandLine.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070024#include "llvm/Support/raw_os_ostream.h"
25#include "llvm/Support/SourceMgr.h"
26
27#include <fstream>
28#include <iostream>
29
30using namespace llvm;
31
Jim Stichnothf7c9a142014-04-29 10:52:43 -070032static 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 Stichnoth5bc2b1d2014-05-22 13:38:48 -070050static 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));
62static cl::opt<Ice::OptLevel>
63OptLevel(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 Stichnothf7c9a142014-04-29 10:52:43 -070070static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"),
Karl Schimpfa667fb82014-05-19 14:56:51 -070071 cl::init("-"));
Jim Stichnothf7c9a142014-04-29 10:52:43 -070072static cl::opt<std::string> OutputFilename("o",
73 cl::desc("Override output filename"),
74 cl::init("-"),
75 cl::value_desc("filename"));
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070076static cl::opt<std::string> LogFilename("log", cl::desc("Set log filename"),
77 cl::init("-"),
78 cl::value_desc("filename"));
Jim Stichnothf7c9a142014-04-29 10:52:43 -070079static cl::opt<std::string>
80TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"),
81 cl::init(""), cl::value_desc("prefix"));
82static cl::opt<bool>
83DisableInternal("external",
84 cl::desc("Disable 'internal' linkage type for testing"));
85static cl::opt<bool>
86DisableTranslation("notranslate", cl::desc("Disable Subzero translation"));
87
88static cl::opt<bool> SubzeroTimingEnabled(
89 "timing", cl::desc("Enable breakdown timing of Subzero translation"));
90
Jim Stichnothf61d5b22014-05-23 13:31:24 -070091static 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 Schimpfa667fb82014-05-19 14:56:51 -070096 cl::init(LLVMFormat));
97
Jim Stichnothde4ca712014-06-29 08:13:48 -070098static cl::opt<bool>
99BuildOnRead("build-on-read",
100 cl::desc("Build ICE instructions when reading bitcode"),
101 cl::init(false));
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700102
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700103int main(int argc, char **argv) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700104
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700105 cl::ParseCommandLineOptions(argc, argv);
106
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700107 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 Stichnoth5bc2b1d2014-05-22 13:38:48 -0700118 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 Stichnothf7c9a142014-04-29 10:52:43 -0700124
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700125 Ice::GlobalContext Ctx(Ls, Os, VMask, TargetArch, OptLevel, TestPrefix);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700126
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700127 Ice::ClFlags Flags;
128 Flags.DisableInternal = DisableInternal;
129 Flags.SubzeroTimingEnabled = SubzeroTimingEnabled;
130 Flags.DisableTranslation = DisableTranslation;
131
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700132 if (BuildOnRead) {
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700133 Ice::PNaClTranslator Translator(&Ctx, Flags);
134 Translator.translate(IRFilename);
Karl Schimpfb164d202014-07-11 10:26:34 -0700135 return Translator.getErrorStatus();
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700136 } else {
137 // Parse the input LLVM IR file into a module.
138 SMDiagnostic Err;
139 Ice::Timer T;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700140 Module *Mod =
141 NaClParseIRFile(IRFilename, InputFileFormat, Err, getGlobalContext());
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700142
143 if (SubzeroTimingEnabled) {
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700144 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec()
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700145 << " sec\n";
146 }
147
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700148 if (!Mod) {
149 Err.print(argv[0], errs());
150 return 1;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700151 }
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700152
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700153 Ice::Converter Converter(&Ctx, Flags);
Karl Schimpfb164d202014-07-11 10:26:34 -0700154 Converter.convertToIce(Mod);
155 return Converter.getErrorStatus();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700156 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700157}