blob: 1b6c71080af7f231e3976f61682f04ed46fe21b1 [file] [log] [blame]
Chris Lattner5b836c42003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere737c7a2001-09-07 22:20:50 +00009//
Brian Gaekee40eae72004-03-16 21:47:20 +000010// This is the llc code generator driver. It provides a convenient
Misha Brukman3da94ae2005-04-22 00:00:37 +000011// command-line interface for generating native assembly-language code
Brian Gaekee40eae72004-03-16 21:47:20 +000012// or C code, given LLVM bytecode.
Chris Lattnere737c7a2001-09-07 22:20:50 +000013//
Chris Lattnerb79757c2001-10-04 01:40:53 +000014//===----------------------------------------------------------------------===//
Vikram S. Advecb465fc2001-07-21 12:42:29 +000015
Vikram S. Advecb465fc2001-07-21 12:42:29 +000016#include "llvm/Bytecode/Reader.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000017#include "llvm/Target/SubtargetFeature.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000019#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000020#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000021#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000022#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000023#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/PluginLoader.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000026#include "llvm/Support/PassNameParser.h"
27#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000029#include "llvm/Config/config.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000030#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000031#include <iostream>
32#include <memory>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000033
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000036// General options for llc. Other pass-specific options are specified
37// within the corresponding llc passes, and target-specific options
38// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000039//
Chris Lattnerb5881f12003-04-25 05:26:11 +000040static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000041InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
42
Chris Lattnerb5881f12003-04-25 05:26:11 +000043static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000044OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
45
46static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
47
Chris Lattner178e0c42005-11-08 02:12:17 +000048static cl::opt<bool> Fast("fast",
49 cl::desc("Generate code quickly, potentially sacrificing code quality"));
50
Chris Lattnerf33b8662005-12-16 04:59:57 +000051static cl::opt<std::string>
52TargetTriple("triple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000053
Chris Lattnere45110e2004-07-11 04:03:24 +000054static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000055MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000056
Jim Laskeyb1e11802005-09-01 21:38:21 +000057static cl::opt<std::string>
58MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000059 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000060 cl::value_desc("cpu-name"),
61 cl::init(""));
62
63static cl::list<std::string>
64MAttrs("mattr",
65 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000066 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000067 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000068
Chris Lattner812125a2005-06-25 03:32:05 +000069cl::opt<TargetMachine::CodeGenFileType>
70FileType("filetype", cl::init(TargetMachine::AssemblyFile),
71 cl::desc("Choose a file type (not all types are supported by all targets):"),
72 cl::values(
73 clEnumValN(TargetMachine::AssemblyFile, "asm",
74 " Emit an assembly ('.s') file"),
75 clEnumValN(TargetMachine::ObjectFile, "obj",
Chris Lattner07be8d42005-10-22 22:00:45 +000076 " Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000077 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
78 " Emit a native dynamic library ('.so') file"),
79 clEnumValEnd));
80
Reid Spencer4418c2b2005-07-28 02:25:30 +000081// The LLCPassList is populated with passes that were registered using
82// PassInfo::LLC by the FilteredPassNameParser:
83cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::LLC> >
84LLCPassList(cl::desc("Passes Available"));
85
86cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000087 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +000088
Chris Lattner812125a2005-06-25 03:32:05 +000089
90// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000091static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000092GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000093 std::string IFN = InputFilename;
94 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000095 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000096 if ((Len > 2) &&
97 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000098 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000099 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000100 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000101 }
102 return outputFilename;
103}
104
Vikram S. Adve805eb962001-09-18 13:10:45 +0000105
Chris Lattner5b836c42003-06-20 15:49:04 +0000106// main - Entry point for the llc compiler.
107//
108int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000109 try {
110 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
111 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +0000112
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000113 // Load the module to be compiled...
114 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
115 if (M.get() == 0) {
116 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000117 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000118 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119 Module &mod = *M.get();
Chris Lattnere45110e2004-07-11 04:03:24 +0000120
Chris Lattnerf33b8662005-12-16 04:59:57 +0000121 // If we are supposed to override the target triple, do so now.
122 if (!TargetTriple.empty())
123 mod.setTargetTriple(TargetTriple);
124
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000125 // Allocate target machine. First, check whether the user has
126 // explicitly specified an architecture to compile for.
127 TargetMachine* (*TargetMachineAllocator)(const Module&,
128 IntrinsicLowering *) = 0;
129 if (MArch == 0) {
130 std::string Err;
131 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
132 if (MArch == 0) {
133 std::cerr << argv[0] << ": error auto-selecting target for module '"
134 << Err << "'. Please use the -march option to explicitly "
135 << "pick a target.\n";
Misha Brukman878ba7c2004-07-12 22:58:07 +0000136 return 1;
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000137 }
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000138 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000139
Jim Laskeyb1e11802005-09-01 21:38:21 +0000140 // Package up features to be passed to target/subtarget
141 std::string FeaturesStr;
142 if (MCPU.size() || MAttrs.size()) {
143 SubtargetFeatures Features;
144 Features.setCPU(MCPU);
145 for (unsigned i = 0; i != MAttrs.size(); ++i)
146 Features.AddFeature(MAttrs[i]);
147 FeaturesStr = Features.getString();
148 }
149
150 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0, FeaturesStr));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 assert(target.get() && "Could not allocate target machine!");
152 TargetMachine &Target = *target.get();
153 const TargetData &TD = Target.getTargetData();
154
155 // Build up all of the passes that we want to do to the module...
156 PassManager Passes;
Chris Lattnercbb34a72005-06-25 03:00:34 +0000157 Passes.add(new TargetData(TD));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000158
Reid Spencer4418c2b2005-07-28 02:25:30 +0000159 // Create a new pass for each one specified on the command line
160 for (unsigned i = 0; i < LLCPassList.size(); ++i) {
161 const PassInfo *aPass = LLCPassList[i];
162
163 if (aPass->getNormalCtor()) {
164 Pass *P = aPass->getNormalCtor()();
165 Passes.add(P);
166 } else {
167 std::cerr << argv[0] << ": cannot create pass: "
168 << aPass->getPassName() << "\n";
169 }
170 }
171
Reid Spencer540f7d62005-07-28 04:00:49 +0000172#ifndef NDEBUG
173 if(!NoVerify)
174 Passes.add(createVerifierPass());
175#endif
Reid Spencer4418c2b2005-07-28 02:25:30 +0000176
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000177 // Figure out where we are going to send the output...
178 std::ostream *Out = 0;
179 if (OutputFilename != "") {
180 if (OutputFilename != "-") {
181 // Specified an output filename?
182 if (!Force && std::ifstream(OutputFilename.c_str())) {
183 // If force is not specified, make sure not to overwrite a file!
184 std::cerr << argv[0] << ": error opening '" << OutputFilename
185 << "': file exists!\n"
186 << "Use -f command line argument to force output\n";
187 return 1;
188 }
189 Out = new std::ofstream(OutputFilename.c_str());
190
191 // Make sure that the Out file gets unlinked from the disk if we get a
192 // SIGINT
193 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
194 } else {
195 Out = &std::cout;
196 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000197 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000198 if (InputFilename == "-") {
199 OutputFilename = "-";
200 Out = &std::cout;
201 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000202 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000203
Chris Lattner812125a2005-06-25 03:32:05 +0000204 switch (FileType) {
205 case TargetMachine::AssemblyFile:
206 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
207 OutputFilename += ".s";
208 else
209 OutputFilename += ".cbe.c";
210 break;
211 case TargetMachine::ObjectFile:
212 OutputFilename += ".o";
213 break;
214 case TargetMachine::DynamicLibrary:
215 OutputFilename += LTDL_SHLIB_EXT;
216 break;
217 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000218
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000219 if (!Force && std::ifstream(OutputFilename.c_str())) {
220 // If force is not specified, make sure not to overwrite a file!
221 std::cerr << argv[0] << ": error opening '" << OutputFilename
222 << "': file exists!\n"
223 << "Use -f command line argument to force output\n";
224 return 1;
225 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000226
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000227 Out = new std::ofstream(OutputFilename.c_str());
228 if (!Out->good()) {
229 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
230 delete Out;
231 return 1;
232 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000233
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000234 // Make sure that the Out file gets unlinked from the disk if we get a
235 // SIGINT
236 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner697954c2002-01-20 22:54:45 +0000237 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000238 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000239
Chris Lattnercbb34a72005-06-25 03:00:34 +0000240 // Ask the target to add backend passes as necessary.
Chris Lattner178e0c42005-11-08 02:12:17 +0000241 if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000242 std::cerr << argv[0] << ": target '" << Target.getName()
Chris Lattner812125a2005-06-25 03:32:05 +0000243 << "' does not support generation of this file type!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000244 if (Out != &std::cout) delete Out;
245 // And the Out file is empty and useless, so remove it now.
246 std::remove(OutputFilename.c_str());
247 return 1;
248 } else {
249 // Run our queue of passes all at once now, efficiently.
250 Passes.run(*M.get());
251 }
252
253 // Delete the ostream if it's not a stdout stream
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000254 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000255
256 return 0;
257 } catch (const std::string& msg) {
258 std::cerr << argv[0] << ": " << msg << "\n";
259 } catch (...) {
260 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner63342052002-10-29 21:12:46 +0000261 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000262 return 1;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000263}