blob: 60adc8351418f434af8684997bff907357ff1beb [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"
Chris Lattnerc30598b2006-12-06 01:18:01 +000030#include "llvm/Support/ManagedStatic.h"
Chris Lattner1a735402007-05-06 04:55:19 +000031#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/PluginLoader.h"
Duraid Madinaea8aed72005-12-30 02:47:21 +000033#include "llvm/Support/FileUtilities.h"
Owen Andersoncb371882008-08-21 00:14:44 +000034#include "llvm/Support/raw_ostream.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000035#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000036#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000037#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000038#include "llvm/LinkAllVMCore.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000039#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000040#include <iostream>
41#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000042using namespace llvm;
43
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000044// General options for llc. Other pass-specific options are specified
45// within the corresponding llc passes, and target-specific options
46// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000047//
Chris Lattnerb5881f12003-04-25 05:26:11 +000048static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000049InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000050
Chris Lattnerb5881f12003-04-25 05:26:11 +000051static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000052OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
53
54static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
55
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000056static cl::opt<bool> Fast("fast",
Chris Lattner178e0c42005-11-08 02:12:17 +000057 cl::desc("Generate code quickly, potentially sacrificing code quality"));
58
Chris Lattnerf33b8662005-12-16 04:59:57 +000059static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000060TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000061
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000062static cl::opt<const TargetMachineRegistry::entry*, false,
63 TargetMachineRegistry::Parser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000064MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000065
Jim Laskeyb1e11802005-09-01 21:38:21 +000066static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000067MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000068 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000069 cl::value_desc("cpu-name"),
70 cl::init(""));
71
72static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000073MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000074 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000075 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000076 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000077
Chris Lattner812125a2005-06-25 03:32:05 +000078cl::opt<TargetMachine::CodeGenFileType>
79FileType("filetype", cl::init(TargetMachine::AssemblyFile),
80 cl::desc("Choose a file type (not all types are supported by all targets):"),
81 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000082 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000083 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000084 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000085 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000086 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000087 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000088 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000089 clEnumValEnd));
90
Reid Spencer4418c2b2005-07-28 02:25:30 +000091cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000092 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +000093
Chris Lattner812125a2005-06-25 03:32:05 +000094
95// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000096static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000097GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000098 std::string IFN = InputFilename;
99 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000100 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000101 if ((Len > 2) &&
102 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000103 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000104 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000105 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000106 }
107 return outputFilename;
108}
109
Owen Andersoncb371882008-08-21 00:14:44 +0000110static raw_ostream *GetOutputStream(const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000111 if (OutputFilename != "") {
112 if (OutputFilename == "-")
Owen Andersoncb371882008-08-21 00:14:44 +0000113 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000114
115 // Specified an output filename?
116 if (!Force && std::ifstream(OutputFilename.c_str())) {
117 // If force is not specified, make sure not to overwrite a file!
118 std::cerr << ProgName << ": error opening '" << OutputFilename
119 << "': file exists!\n"
120 << "Use -f command line argument to force output\n";
121 return 0;
122 }
123 // Make sure that the Out file gets unlinked from the disk if we get a
124 // SIGINT
125 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
126
Owen Andersoncb371882008-08-21 00:14:44 +0000127 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000128 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), true, error);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000129 if (!error.empty()) {
130 std::cerr << error << '\n';
131 delete Out;
132 return 0;
133 }
134
135 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000136 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000137
Chris Lattner1911fd42006-09-04 04:14:57 +0000138 if (InputFilename == "-") {
139 OutputFilename = "-";
Owen Andersoncb371882008-08-21 00:14:44 +0000140 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000141 }
142
143 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000144
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000145 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000146 switch (FileType) {
147 case TargetMachine::AssemblyFile:
Anton Korobeynikov50276522008-04-23 22:29:24 +0000148 if (MArch->Name[0] == 'c') {
149 if (MArch->Name[1] == 0)
150 OutputFilename += ".cbe.c";
151 else if (MArch->Name[1] == 'p' && MArch->Name[2] == 'p')
152 OutputFilename += ".cpp";
153 else
154 OutputFilename += ".s";
155 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000156 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000157 break;
158 case TargetMachine::ObjectFile:
159 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000160 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000161 break;
162 case TargetMachine::DynamicLibrary:
163 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000164 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000165 break;
166 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000167
Chris Lattner1911fd42006-09-04 04:14:57 +0000168 if (!Force && std::ifstream(OutputFilename.c_str())) {
169 // If force is not specified, make sure not to overwrite a file!
170 std::cerr << ProgName << ": error opening '" << OutputFilename
171 << "': file exists!\n"
172 << "Use -f command line argument to force output\n";
173 return 0;
174 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000175
Chris Lattner1911fd42006-09-04 04:14:57 +0000176 // Make sure that the Out file gets unlinked from the disk if we get a
177 // SIGINT
178 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000179
Owen Andersoncb371882008-08-21 00:14:44 +0000180 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000181 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Binary, error);
Owen Andersoncb371882008-08-21 00:14:44 +0000182 if (!error.empty()) {
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000183 std::cerr << error << '\n';
Chris Lattner1911fd42006-09-04 04:14:57 +0000184 delete Out;
185 return 0;
186 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000187
Chris Lattner1911fd42006-09-04 04:14:57 +0000188 return Out;
189}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000190
Chris Lattner5b836c42003-06-20 15:49:04 +0000191// main - Entry point for the llc compiler.
192//
193int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000194 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman82a13c92007-10-08 15:45:12 +0000195 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner1a735402007-05-06 04:55:19 +0000196 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +0000197
Chris Lattner1a735402007-05-06 04:55:19 +0000198 // Load the module to be compiled...
199 std::string ErrorMessage;
200 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000201
Chris Lattner744879e2007-05-06 09:32:02 +0000202 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000203 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner744879e2007-05-06 09:32:02 +0000204 if (Buffer.get())
205 M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage));
Chris Lattner1a735402007-05-06 04:55:19 +0000206 if (M.get() == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000207 std::cerr << argv[0] << ": bitcode didn't read correctly.\n";
Chris Lattner1a735402007-05-06 04:55:19 +0000208 std::cerr << "Reason: " << ErrorMessage << "\n";
209 return 1;
210 }
211 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000212
Chris Lattner1a735402007-05-06 04:55:19 +0000213 // If we are supposed to override the target triple, do so now.
214 if (!TargetTriple.empty())
215 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000216
Chris Lattner1a735402007-05-06 04:55:19 +0000217 // Allocate target machine. First, check whether the user has
218 // explicitly specified an architecture to compile for.
219 if (MArch == 0) {
220 std::string Err;
221 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
222 if (MArch == 0) {
223 std::cerr << argv[0] << ": error auto-selecting target for module '"
224 << Err << "'. Please use the -march option to explicitly "
225 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000226 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000227 }
Chris Lattner63342052002-10-29 21:12:46 +0000228 }
Chris Lattner1a735402007-05-06 04:55:19 +0000229
230 // Package up features to be passed to target/subtarget
231 std::string FeaturesStr;
232 if (MCPU.size() || MAttrs.size()) {
233 SubtargetFeatures Features;
234 Features.setCPU(MCPU);
235 for (unsigned i = 0; i != MAttrs.size(); ++i)
236 Features.AddFeature(MAttrs[i]);
237 FeaturesStr = Features.getString();
238 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000239
Chris Lattner1a735402007-05-06 04:55:19 +0000240 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
241 assert(target.get() && "Could not allocate target machine!");
242 TargetMachine &Target = *target.get();
243
244 // Figure out where we are going to send the output...
Owen Andersoncb371882008-08-21 00:14:44 +0000245 raw_ostream *Out = GetOutputStream(argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000246 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000247
Chris Lattner1a735402007-05-06 04:55:19 +0000248 // If this target requires addPassesToEmitWholeFile, do it now. This is
249 // used by strange things like the C backend.
250 if (Target.WantsWholeFile()) {
251 PassManager PM;
252 PM.add(new TargetData(*Target.getTargetData()));
253 if (!NoVerify)
254 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000255
Chris Lattner1a735402007-05-06 04:55:19 +0000256 // Ask the target to add backend passes as necessary.
257 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
258 std::cerr << argv[0] << ": target does not support generation of this"
259 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000260 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000261 // And the Out file is empty and useless, so remove it now.
262 sys::Path(OutputFilename).eraseFromDisk();
263 return 1;
264 }
265 PM.run(mod);
266 } else {
267 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000268 ExistingModuleProvider Provider(M.release());
269 FunctionPassManager Passes(&Provider);
Chris Lattner1a735402007-05-06 04:55:19 +0000270 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000271
Chris Lattner1a735402007-05-06 04:55:19 +0000272#ifndef NDEBUG
273 if (!NoVerify)
274 Passes.add(createVerifierPass());
275#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000276
Chris Lattner1a735402007-05-06 04:55:19 +0000277 // Ask the target to add backend passes as necessary.
278 MachineCodeEmitter *MCE = 0;
279
280 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
281 default:
282 assert(0 && "Invalid file model!");
283 return 1;
284 case FileModel::Error:
285 std::cerr << argv[0] << ": target does not support generation of this"
286 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000287 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000288 // And the Out file is empty and useless, so remove it now.
289 sys::Path(OutputFilename).eraseFromDisk();
290 return 1;
291 case FileModel::AsmFile:
292 break;
293 case FileModel::MachOFile:
294 MCE = AddMachOWriter(Passes, *Out, Target);
295 break;
296 case FileModel::ElfFile:
297 MCE = AddELFWriter(Passes, *Out, Target);
298 break;
299 }
300
301 if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) {
302 std::cerr << argv[0] << ": target does not support generation of this"
303 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000304 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000305 // And the Out file is empty and useless, so remove it now.
306 sys::Path(OutputFilename).eraseFromDisk();
307 return 1;
308 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000309
Chris Lattner1a735402007-05-06 04:55:19 +0000310 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000311
Chris Lattner1a735402007-05-06 04:55:19 +0000312 // Run our queue of passes all at once now, efficiently.
313 // TODO: this could lazily stream functions out of the module.
314 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
315 if (!I->isDeclaration())
316 Passes.run(*I);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000317
Chris Lattner1a735402007-05-06 04:55:19 +0000318 Passes.doFinalization();
319 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000320
Chris Lattner1a735402007-05-06 04:55:19 +0000321 // Delete the ostream if it's not a stdout stream
Owen Andersoncb371882008-08-21 00:14:44 +0000322 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000323
324 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000325}