blob: 087a77097be2b951fc81dd4135569a9b6de879d5 [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"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000034#include "llvm/Support/RegistryParser.h"
Owen Andersoncb371882008-08-21 00:14:44 +000035#include "llvm/Support/raw_ostream.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000036#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000037#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000038#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000039#include "llvm/LinkAllVMCore.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000040#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000041#include <iostream>
42#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000043using namespace llvm;
44
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000045// General options for llc. Other pass-specific options are specified
46// within the corresponding llc passes, and target-specific options
47// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000048//
Chris Lattnerb5881f12003-04-25 05:26:11 +000049static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000050InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000051
Chris Lattnerb5881f12003-04-25 05:26:11 +000052static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000053OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
54
55static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
56
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000057static cl::opt<bool> Fast("fast",
Chris Lattner178e0c42005-11-08 02:12:17 +000058 cl::desc("Generate code quickly, potentially sacrificing code quality"));
59
Chris Lattnerf33b8662005-12-16 04:59:57 +000060static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000061TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000062
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000063static cl::opt<const TargetMachineRegistry::entry*, false,
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000064 RegistryParser<TargetMachine> >
Chris Lattnercbb34a72005-06-25 03:00:34 +000065MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000066
Jim Laskeyb1e11802005-09-01 21:38:21 +000067static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000068MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000069 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000070 cl::value_desc("cpu-name"),
71 cl::init(""));
72
73static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000074MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000075 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000076 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000077 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000078
Chris Lattner812125a2005-06-25 03:32:05 +000079cl::opt<TargetMachine::CodeGenFileType>
80FileType("filetype", cl::init(TargetMachine::AssemblyFile),
81 cl::desc("Choose a file type (not all types are supported by all targets):"),
82 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000083 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000084 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000085 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000086 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000087 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000088 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000089 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000090 clEnumValEnd));
91
Reid Spencer4418c2b2005-07-28 02:25:30 +000092cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000093 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +000094
Chris Lattner812125a2005-06-25 03:32:05 +000095
96// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000097static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000098GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000099 std::string IFN = InputFilename;
100 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000101 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000102 if ((Len > 2) &&
103 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000104 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000105 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000106 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000107 }
108 return outputFilename;
109}
110
Owen Andersoncb371882008-08-21 00:14:44 +0000111static raw_ostream *GetOutputStream(const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000112 if (OutputFilename != "") {
113 if (OutputFilename == "-")
Owen Andersoncb371882008-08-21 00:14:44 +0000114 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000115
116 // Specified an output filename?
117 if (!Force && std::ifstream(OutputFilename.c_str())) {
118 // If force is not specified, make sure not to overwrite a file!
119 std::cerr << ProgName << ": error opening '" << OutputFilename
120 << "': file exists!\n"
121 << "Use -f command line argument to force output\n";
122 return 0;
123 }
124 // Make sure that the Out file gets unlinked from the disk if we get a
125 // SIGINT
126 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
127
Owen Andersoncb371882008-08-21 00:14:44 +0000128 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000129 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), true, error);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000130 if (!error.empty()) {
131 std::cerr << error << '\n';
132 delete Out;
133 return 0;
134 }
135
136 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000137 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000138
Chris Lattner1911fd42006-09-04 04:14:57 +0000139 if (InputFilename == "-") {
140 OutputFilename = "-";
Owen Andersoncb371882008-08-21 00:14:44 +0000141 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000142 }
143
144 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000145
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000146 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000147 switch (FileType) {
148 case TargetMachine::AssemblyFile:
Anton Korobeynikov50276522008-04-23 22:29:24 +0000149 if (MArch->Name[0] == 'c') {
150 if (MArch->Name[1] == 0)
151 OutputFilename += ".cbe.c";
152 else if (MArch->Name[1] == 'p' && MArch->Name[2] == 'p')
153 OutputFilename += ".cpp";
154 else
155 OutputFilename += ".s";
156 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000157 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000158 break;
159 case TargetMachine::ObjectFile:
160 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000161 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000162 break;
163 case TargetMachine::DynamicLibrary:
164 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000165 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000166 break;
167 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000168
Chris Lattner1911fd42006-09-04 04:14:57 +0000169 if (!Force && std::ifstream(OutputFilename.c_str())) {
170 // If force is not specified, make sure not to overwrite a file!
171 std::cerr << ProgName << ": error opening '" << OutputFilename
172 << "': file exists!\n"
173 << "Use -f command line argument to force output\n";
174 return 0;
175 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000176
Chris Lattner1911fd42006-09-04 04:14:57 +0000177 // Make sure that the Out file gets unlinked from the disk if we get a
178 // SIGINT
179 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000180
Owen Andersoncb371882008-08-21 00:14:44 +0000181 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000182 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Binary, error);
Owen Andersoncb371882008-08-21 00:14:44 +0000183 if (!error.empty()) {
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000184 std::cerr << error << '\n';
Chris Lattner1911fd42006-09-04 04:14:57 +0000185 delete Out;
186 return 0;
187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000188
Chris Lattner1911fd42006-09-04 04:14:57 +0000189 return Out;
190}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000191
Chris Lattner5b836c42003-06-20 15:49:04 +0000192// main - Entry point for the llc compiler.
193//
194int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000195 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman82a13c92007-10-08 15:45:12 +0000196 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner1a735402007-05-06 04:55:19 +0000197 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +0000198
Chris Lattner1a735402007-05-06 04:55:19 +0000199 // Load the module to be compiled...
200 std::string ErrorMessage;
201 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000202
Chris Lattner744879e2007-05-06 09:32:02 +0000203 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000204 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner744879e2007-05-06 09:32:02 +0000205 if (Buffer.get())
206 M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage));
Chris Lattner1a735402007-05-06 04:55:19 +0000207 if (M.get() == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000208 std::cerr << argv[0] << ": bitcode didn't read correctly.\n";
Chris Lattner1a735402007-05-06 04:55:19 +0000209 std::cerr << "Reason: " << ErrorMessage << "\n";
210 return 1;
211 }
212 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000213
Chris Lattner1a735402007-05-06 04:55:19 +0000214 // If we are supposed to override the target triple, do so now.
215 if (!TargetTriple.empty())
216 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000217
Chris Lattner1a735402007-05-06 04:55:19 +0000218 // Allocate target machine. First, check whether the user has
219 // explicitly specified an architecture to compile for.
220 if (MArch == 0) {
221 std::string Err;
222 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
223 if (MArch == 0) {
224 std::cerr << argv[0] << ": error auto-selecting target for module '"
225 << Err << "'. Please use the -march option to explicitly "
226 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000227 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000228 }
Chris Lattner63342052002-10-29 21:12:46 +0000229 }
Chris Lattner1a735402007-05-06 04:55:19 +0000230
231 // Package up features to be passed to target/subtarget
232 std::string FeaturesStr;
233 if (MCPU.size() || MAttrs.size()) {
234 SubtargetFeatures Features;
235 Features.setCPU(MCPU);
236 for (unsigned i = 0; i != MAttrs.size(); ++i)
237 Features.AddFeature(MAttrs[i]);
238 FeaturesStr = Features.getString();
239 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000240
Chris Lattner1a735402007-05-06 04:55:19 +0000241 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
242 assert(target.get() && "Could not allocate target machine!");
243 TargetMachine &Target = *target.get();
244
245 // Figure out where we are going to send the output...
Owen Andersoncb371882008-08-21 00:14:44 +0000246 raw_ostream *Out = GetOutputStream(argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000247 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000248
Chris Lattner1a735402007-05-06 04:55:19 +0000249 // If this target requires addPassesToEmitWholeFile, do it now. This is
250 // used by strange things like the C backend.
251 if (Target.WantsWholeFile()) {
252 PassManager PM;
253 PM.add(new TargetData(*Target.getTargetData()));
254 if (!NoVerify)
255 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000256
Chris Lattner1a735402007-05-06 04:55:19 +0000257 // Ask the target to add backend passes as necessary.
258 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
259 std::cerr << argv[0] << ": target does not support generation of this"
260 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000261 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000262 // And the Out file is empty and useless, so remove it now.
263 sys::Path(OutputFilename).eraseFromDisk();
264 return 1;
265 }
266 PM.run(mod);
267 } else {
268 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000269 ExistingModuleProvider Provider(M.release());
270 FunctionPassManager Passes(&Provider);
Chris Lattner1a735402007-05-06 04:55:19 +0000271 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000272
Chris Lattner1a735402007-05-06 04:55:19 +0000273#ifndef NDEBUG
274 if (!NoVerify)
275 Passes.add(createVerifierPass());
276#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000277
Chris Lattner1a735402007-05-06 04:55:19 +0000278 // Ask the target to add backend passes as necessary.
279 MachineCodeEmitter *MCE = 0;
280
281 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
282 default:
283 assert(0 && "Invalid file model!");
284 return 1;
285 case FileModel::Error:
286 std::cerr << argv[0] << ": target does not support generation of this"
287 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000288 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000289 // And the Out file is empty and useless, so remove it now.
290 sys::Path(OutputFilename).eraseFromDisk();
291 return 1;
292 case FileModel::AsmFile:
293 break;
294 case FileModel::MachOFile:
295 MCE = AddMachOWriter(Passes, *Out, Target);
296 break;
297 case FileModel::ElfFile:
298 MCE = AddELFWriter(Passes, *Out, Target);
299 break;
300 }
301
302 if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) {
303 std::cerr << argv[0] << ": target does not support generation of this"
304 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000305 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000306 // And the Out file is empty and useless, so remove it now.
307 sys::Path(OutputFilename).eraseFromDisk();
308 return 1;
309 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000310
Chris Lattner1a735402007-05-06 04:55:19 +0000311 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000312
Chris Lattner1a735402007-05-06 04:55:19 +0000313 // Run our queue of passes all at once now, efficiently.
314 // TODO: this could lazily stream functions out of the module.
315 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
316 if (!I->isDeclaration())
317 Passes.run(*I);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000318
Chris Lattner1a735402007-05-06 04:55:19 +0000319 Passes.doFinalization();
320 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000321
Chris Lattner1a735402007-05-06 04:55:19 +0000322 // Delete the ostream if it's not a stdout stream
Owen Andersoncb371882008-08-21 00:14:44 +0000323 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000324
325 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000326}