blob: c80e9296e831f7483b3be27c8e96becdad9a806a [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Gabor Greifa99be512007-07-05 17:07:56 +000012// or C code, given LLVM bitcode.
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
Chris Lattner1a735402007-05-06 04:55:19 +000016#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendling546d0fb2007-02-08 01:41:07 +000017#include "llvm/CodeGen/FileWriters.h"
Chris Lattnerd3a680a2006-08-01 22:34:35 +000018#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Anton Korobeynikov2b110042008-08-17 14:33:01 +000019#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000020#include "llvm/Target/SubtargetFeature.h"
Owen Anderson07000c62006-05-12 06:33:49 +000021#include "llvm/Target/TargetData.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000023#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000025#include "llvm/Module.h"
Chris Lattner744879e2007-05-06 09:32:02 +000026#include "llvm/ModuleProvider.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000027#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000028#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000030#include "llvm/Support/FileUtilities.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000031#include "llvm/Support/ManagedStatic.h"
Chris Lattner1a735402007-05-06 04:55:19 +000032#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000034#include "llvm/Support/PrettyStackTrace.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000035#include "llvm/Support/RegistryParser.h"
Owen Andersoncb371882008-08-21 00:14:44 +000036#include "llvm/Support/raw_ostream.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000037#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000038#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000039#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000040#include "llvm/LinkAllVMCore.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000041#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000042#include <iostream>
43#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000044using namespace llvm;
45
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000046// General options for llc. Other pass-specific options are specified
47// within the corresponding llc passes, and target-specific options
48// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000049//
Chris Lattnerb5881f12003-04-25 05:26:11 +000050static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000051InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Chris Lattnerb5881f12003-04-25 05:26:11 +000053static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000054OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
55
56static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
57
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000058static cl::opt<bool> Fast("fast",
Chris Lattner178e0c42005-11-08 02:12:17 +000059 cl::desc("Generate code quickly, potentially sacrificing code quality"));
60
Chris Lattnerf33b8662005-12-16 04:59:57 +000061static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000062TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000063
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000064static cl::opt<const TargetMachineRegistry::entry*, false,
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000065 RegistryParser<TargetMachine> >
Chris Lattnercbb34a72005-06-25 03:00:34 +000066MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000067
Jim Laskeyb1e11802005-09-01 21:38:21 +000068static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000069MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000070 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000071 cl::value_desc("cpu-name"),
72 cl::init(""));
73
74static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000075MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000076 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000077 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000078 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000079
Chris Lattner812125a2005-06-25 03:32:05 +000080cl::opt<TargetMachine::CodeGenFileType>
81FileType("filetype", cl::init(TargetMachine::AssemblyFile),
82 cl::desc("Choose a file type (not all types are supported by all targets):"),
83 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000084 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000085 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000086 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000087 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000088 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000089 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000090 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000091 clEnumValEnd));
92
Reid Spencer4418c2b2005-07-28 02:25:30 +000093cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000094 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +000095
Chris Lattner812125a2005-06-25 03:32:05 +000096
97// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000098static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000099GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000100 std::string IFN = InputFilename;
101 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000102 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000103 if ((Len > 2) &&
104 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000105 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000106 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000107 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000108 }
109 return outputFilename;
110}
111
Owen Andersoncb371882008-08-21 00:14:44 +0000112static raw_ostream *GetOutputStream(const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000113 if (OutputFilename != "") {
114 if (OutputFilename == "-")
Owen Andersoncb371882008-08-21 00:14:44 +0000115 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000116
117 // Specified an output filename?
118 if (!Force && std::ifstream(OutputFilename.c_str())) {
119 // If force is not specified, make sure not to overwrite a file!
120 std::cerr << ProgName << ": error opening '" << OutputFilename
121 << "': file exists!\n"
122 << "Use -f command line argument to force output\n";
123 return 0;
124 }
125 // Make sure that the Out file gets unlinked from the disk if we get a
126 // SIGINT
127 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
128
Owen Andersoncb371882008-08-21 00:14:44 +0000129 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000130 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), true, error);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000131 if (!error.empty()) {
132 std::cerr << error << '\n';
133 delete Out;
134 return 0;
135 }
136
137 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000138 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000139
Chris Lattner1911fd42006-09-04 04:14:57 +0000140 if (InputFilename == "-") {
141 OutputFilename = "-";
Owen Andersoncb371882008-08-21 00:14:44 +0000142 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000143 }
144
145 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000146
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000147 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000148 switch (FileType) {
149 case TargetMachine::AssemblyFile:
Anton Korobeynikov50276522008-04-23 22:29:24 +0000150 if (MArch->Name[0] == 'c') {
151 if (MArch->Name[1] == 0)
152 OutputFilename += ".cbe.c";
153 else if (MArch->Name[1] == 'p' && MArch->Name[2] == 'p')
154 OutputFilename += ".cpp";
155 else
156 OutputFilename += ".s";
157 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000158 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000159 break;
160 case TargetMachine::ObjectFile:
161 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000162 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000163 break;
164 case TargetMachine::DynamicLibrary:
165 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000166 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000167 break;
168 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000169
Chris Lattner1911fd42006-09-04 04:14:57 +0000170 if (!Force && std::ifstream(OutputFilename.c_str())) {
171 // If force is not specified, make sure not to overwrite a file!
172 std::cerr << ProgName << ": error opening '" << OutputFilename
173 << "': file exists!\n"
174 << "Use -f command line argument to force output\n";
175 return 0;
176 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000177
Chris Lattner1911fd42006-09-04 04:14:57 +0000178 // Make sure that the Out file gets unlinked from the disk if we get a
179 // SIGINT
180 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000181
Owen Andersoncb371882008-08-21 00:14:44 +0000182 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000183 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Binary, error);
Owen Andersoncb371882008-08-21 00:14:44 +0000184 if (!error.empty()) {
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000185 std::cerr << error << '\n';
Chris Lattner1911fd42006-09-04 04:14:57 +0000186 delete Out;
187 return 0;
188 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000189
Chris Lattner1911fd42006-09-04 04:14:57 +0000190 return Out;
191}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000192
Chris Lattner5b836c42003-06-20 15:49:04 +0000193// main - Entry point for the llc compiler.
194//
195int main(int argc, char **argv) {
Chris Lattner1a735402007-05-06 04:55:19 +0000196 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000197 PrettyStackTraceProgram X(argc, argv);
198 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
199 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner364d1202004-02-19 20:32:39 +0000200
Chris Lattner1a735402007-05-06 04:55:19 +0000201 // Load the module to be compiled...
202 std::string ErrorMessage;
203 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000204
Chris Lattner744879e2007-05-06 09:32:02 +0000205 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000206 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner744879e2007-05-06 09:32:02 +0000207 if (Buffer.get())
208 M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage));
Chris Lattner1a735402007-05-06 04:55:19 +0000209 if (M.get() == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000210 std::cerr << argv[0] << ": bitcode didn't read correctly.\n";
Chris Lattner1a735402007-05-06 04:55:19 +0000211 std::cerr << "Reason: " << ErrorMessage << "\n";
212 return 1;
213 }
214 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000215
Chris Lattner1a735402007-05-06 04:55:19 +0000216 // If we are supposed to override the target triple, do so now.
217 if (!TargetTriple.empty())
218 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000219
Chris Lattner1a735402007-05-06 04:55:19 +0000220 // Allocate target machine. First, check whether the user has
221 // explicitly specified an architecture to compile for.
222 if (MArch == 0) {
223 std::string Err;
224 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
225 if (MArch == 0) {
226 std::cerr << argv[0] << ": error auto-selecting target for module '"
227 << Err << "'. Please use the -march option to explicitly "
228 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000229 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000230 }
Chris Lattner63342052002-10-29 21:12:46 +0000231 }
Chris Lattner1a735402007-05-06 04:55:19 +0000232
233 // Package up features to be passed to target/subtarget
234 std::string FeaturesStr;
235 if (MCPU.size() || MAttrs.size()) {
236 SubtargetFeatures Features;
237 Features.setCPU(MCPU);
238 for (unsigned i = 0; i != MAttrs.size(); ++i)
239 Features.AddFeature(MAttrs[i]);
240 FeaturesStr = Features.getString();
241 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000242
Chris Lattner1a735402007-05-06 04:55:19 +0000243 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
244 assert(target.get() && "Could not allocate target machine!");
245 TargetMachine &Target = *target.get();
246
247 // Figure out where we are going to send the output...
Owen Andersoncb371882008-08-21 00:14:44 +0000248 raw_ostream *Out = GetOutputStream(argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000249 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000250
Chris Lattner1a735402007-05-06 04:55:19 +0000251 // If this target requires addPassesToEmitWholeFile, do it now. This is
252 // used by strange things like the C backend.
253 if (Target.WantsWholeFile()) {
254 PassManager PM;
255 PM.add(new TargetData(*Target.getTargetData()));
256 if (!NoVerify)
257 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000258
Chris Lattner1a735402007-05-06 04:55:19 +0000259 // Ask the target to add backend passes as necessary.
260 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
261 std::cerr << argv[0] << ": target does not support generation of this"
262 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000263 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000264 // And the Out file is empty and useless, so remove it now.
265 sys::Path(OutputFilename).eraseFromDisk();
266 return 1;
267 }
268 PM.run(mod);
269 } else {
270 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000271 ExistingModuleProvider Provider(M.release());
272 FunctionPassManager Passes(&Provider);
Chris Lattner1a735402007-05-06 04:55:19 +0000273 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000274
Chris Lattner1a735402007-05-06 04:55:19 +0000275#ifndef NDEBUG
276 if (!NoVerify)
277 Passes.add(createVerifierPass());
278#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000279
Chris Lattner1a735402007-05-06 04:55:19 +0000280 // Ask the target to add backend passes as necessary.
281 MachineCodeEmitter *MCE = 0;
282
283 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
284 default:
285 assert(0 && "Invalid file model!");
286 return 1;
287 case FileModel::Error:
288 std::cerr << argv[0] << ": target does not support generation of this"
289 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000290 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000291 // And the Out file is empty and useless, so remove it now.
292 sys::Path(OutputFilename).eraseFromDisk();
293 return 1;
294 case FileModel::AsmFile:
295 break;
296 case FileModel::MachOFile:
297 MCE = AddMachOWriter(Passes, *Out, Target);
298 break;
299 case FileModel::ElfFile:
300 MCE = AddELFWriter(Passes, *Out, Target);
301 break;
302 }
303
304 if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) {
305 std::cerr << argv[0] << ": target does not support generation of this"
306 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000307 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000308 // And the Out file is empty and useless, so remove it now.
309 sys::Path(OutputFilename).eraseFromDisk();
310 return 1;
311 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000312
Chris Lattner1a735402007-05-06 04:55:19 +0000313 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000314
Chris Lattner1a735402007-05-06 04:55:19 +0000315 // Run our queue of passes all at once now, efficiently.
316 // TODO: this could lazily stream functions out of the module.
317 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
318 if (!I->isDeclaration())
319 Passes.run(*I);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000320
Chris Lattner1a735402007-05-06 04:55:19 +0000321 Passes.doFinalization();
322 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000323
Chris Lattner1a735402007-05-06 04:55:19 +0000324 // Delete the ostream if it's not a stdout stream
Owen Andersoncb371882008-08-21 00:14:44 +0000325 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000326
327 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000328}