blob: 376ad768b0f20c25c8ba98ed16290339b5aacbd7 [file] [log] [blame]
Chris Lattner67e08422003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2cf137b2001-09-07 22:20:50 +00009//
Brian Gaeke33e83b62004-03-16 21:47:20 +000010// This is the llc code generator driver. It provides a convenient
Misha Brukman650ba8e2005-04-22 00:00:37 +000011// command-line interface for generating native assembly-language code
Brian Gaeke33e83b62004-03-16 21:47:20 +000012// or C code, given LLVM bytecode.
Chris Lattner2cf137b2001-09-07 22:20:50 +000013//
Chris Lattnerb27d4742001-10-04 01:40:53 +000014//===----------------------------------------------------------------------===//
Vikram S. Adve2d94a342001-07-21 12:42:29 +000015
Vikram S. Adve2d94a342001-07-21 12:42:29 +000016#include "llvm/Bytecode/Reader.h"
Bill Wendlingfe5ee142007-02-08 01:41:07 +000017#include "llvm/CodeGen/FileWriters.h"
Chris Lattner5af6a3f2006-08-01 22:34:35 +000018#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Jim Laskey19058c32005-09-01 21:38:21 +000019#include "llvm/Target/SubtargetFeature.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000020#include "llvm/Target/TargetData.h"
Vikram S. Adve9d409352001-09-18 13:10:45 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner6142ca82004-07-11 04:03:24 +000022#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner89a20ef2002-05-07 20:03:27 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnered226062001-09-07 21:26:31 +000024#include "llvm/Module.h"
Chris Lattner7139f282002-01-31 00:46:45 +000025#include "llvm/PassManager.h"
Vikram S. Adveeb818692002-09-16 16:35:34 +000026#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
Chris Lattnera0e49f22007-02-07 21:41:02 +000028#include "llvm/Support/Compressor.h"
Chris Lattner76d46322006-12-06 01:18:01 +000029#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/Support/PluginLoader.h"
Duraid Madinace551ba2005-12-30 02:47:21 +000031#include "llvm/Support/FileUtilities.h"
Reid Spencer8e3830d2005-07-28 02:25:30 +000032#include "llvm/Analysis/Verifier.h"
Chris Lattner278f5152004-05-27 05:41:36 +000033#include "llvm/System/Signals.h"
Chris Lattner06fcc4c2005-06-25 03:32:05 +000034#include "llvm/Config/config.h"
Reid Spencer5113dc52006-06-07 23:03:13 +000035#include "llvm/LinkAllVMCore.h"
Chris Lattner46f1b612001-09-19 16:52:09 +000036#include <fstream>
Reid Spencerf0ebb252004-07-04 12:20:55 +000037#include <iostream>
38#include <memory>
Vikram S. Adveeb818692002-09-16 16:35:34 +000039
Brian Gaeke960707c2003-11-11 22:41:34 +000040using namespace llvm;
41
Vikram S. Adveeb818692002-09-16 16:35:34 +000042// General options for llc. Other pass-specific options are specified
43// within the corresponding llc passes, and target-specific options
44// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000045//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000046static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000047InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
48
Chris Lattnerd64b2de2003-04-25 05:26:11 +000049static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000050OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
51
52static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
53
Chris Lattner731055e2005-11-08 02:12:17 +000054static cl::opt<bool> Fast("fast",
55 cl::desc("Generate code quickly, potentially sacrificing code quality"));
56
Chris Lattnere568b882005-12-16 04:59:57 +000057static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000058TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000059
Chris Lattner6142ca82004-07-11 04:03:24 +000060static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattner09b0eb32005-06-25 03:00:34 +000061MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000062
Jim Laskey19058c32005-09-01 21:38:21 +000063static cl::opt<std::string>
64MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000065 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000066 cl::value_desc("cpu-name"),
67 cl::init(""));
68
69static cl::list<std::string>
70MAttrs("mattr",
71 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000072 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000073 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000074
Chris Lattner06fcc4c2005-06-25 03:32:05 +000075cl::opt<TargetMachine::CodeGenFileType>
76FileType("filetype", cl::init(TargetMachine::AssemblyFile),
77 cl::desc("Choose a file type (not all types are supported by all targets):"),
78 cl::values(
79 clEnumValN(TargetMachine::AssemblyFile, "asm",
80 " Emit an assembly ('.s') file"),
81 clEnumValN(TargetMachine::ObjectFile, "obj",
Chris Lattner711a5fe2005-10-22 22:00:45 +000082 " Emit a native object ('.o') file [experimental]"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000083 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Nate Begemana147cbf2006-08-23 21:29:52 +000084 " Emit a native dynamic library ('.so') file"
85 " [experimental]"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000086 clEnumValEnd));
87
Reid Spencer8e3830d2005-07-28 02:25:30 +000088cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +000089 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +000090
Chris Lattner06fcc4c2005-06-25 03:32:05 +000091
92// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +000093static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +000094GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000095 std::string IFN = InputFilename;
96 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000097 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +000098 if ((Len > 2) &&
99 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000100 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000101 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000102 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000103 }
104 return outputFilename;
105}
106
Chris Lattner12e97302006-09-04 04:14:57 +0000107static std::ostream *GetOutputStream(const char *ProgName) {
108 if (OutputFilename != "") {
109 if (OutputFilename == "-")
110 return &std::cout;
111
112 // Specified an output filename?
113 if (!Force && std::ifstream(OutputFilename.c_str())) {
114 // If force is not specified, make sure not to overwrite a file!
115 std::cerr << ProgName << ": error opening '" << OutputFilename
116 << "': file exists!\n"
117 << "Use -f command line argument to force output\n";
118 return 0;
119 }
120 // Make sure that the Out file gets unlinked from the disk if we get a
121 // SIGINT
122 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
123
124 return new std::ofstream(OutputFilename.c_str());
125 }
126
127 if (InputFilename == "-") {
128 OutputFilename = "-";
129 return &std::cout;
130 }
131
132 OutputFilename = GetFileNameRoot(InputFilename);
133
134 switch (FileType) {
135 case TargetMachine::AssemblyFile:
136 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
137 OutputFilename += ".s";
138 else
139 OutputFilename += ".cbe.c";
140 break;
141 case TargetMachine::ObjectFile:
142 OutputFilename += ".o";
143 break;
144 case TargetMachine::DynamicLibrary:
145 OutputFilename += LTDL_SHLIB_EXT;
146 break;
147 }
148
149 if (!Force && std::ifstream(OutputFilename.c_str())) {
150 // If force is not specified, make sure not to overwrite a file!
151 std::cerr << ProgName << ": error opening '" << OutputFilename
152 << "': file exists!\n"
153 << "Use -f command line argument to force output\n";
154 return 0;
155 }
156
157 // Make sure that the Out file gets unlinked from the disk if we get a
158 // SIGINT
159 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
160
161 std::ostream *Out = new std::ofstream(OutputFilename.c_str());
162 if (!Out->good()) {
163 std::cerr << ProgName << ": error opening " << OutputFilename << "!\n";
164 delete Out;
165 return 0;
166 }
167
168 return Out;
169}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000170
Chris Lattner67e08422003-06-20 15:49:04 +0000171// main - Entry point for the llc compiler.
172//
173int main(int argc, char **argv) {
Chris Lattner76d46322006-12-06 01:18:01 +0000174 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer996ec722004-12-30 05:36:08 +0000175 try {
176 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
177 sys::PrintStackTraceOnErrorSignal();
Chris Lattner69e896b2004-02-19 20:32:39 +0000178
Reid Spencer996ec722004-12-30 05:36:08 +0000179 // Load the module to be compiled...
Chris Lattnera0e49f22007-02-07 21:41:02 +0000180 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename,
181 Compressor::decompressToNewBuffer));
Reid Spencer996ec722004-12-30 05:36:08 +0000182 if (M.get() == 0) {
183 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerb1492402003-08-24 14:02:14 +0000184 return 1;
Chris Lattner6142ca82004-07-11 04:03:24 +0000185 }
Reid Spencer996ec722004-12-30 05:36:08 +0000186 Module &mod = *M.get();
Chris Lattner6142ca82004-07-11 04:03:24 +0000187
Chris Lattnere568b882005-12-16 04:59:57 +0000188 // If we are supposed to override the target triple, do so now.
189 if (!TargetTriple.empty())
190 mod.setTargetTriple(TargetTriple);
191
Reid Spencer996ec722004-12-30 05:36:08 +0000192 // Allocate target machine. First, check whether the user has
193 // explicitly specified an architecture to compile for.
Reid Spencer996ec722004-12-30 05:36:08 +0000194 if (MArch == 0) {
195 std::string Err;
196 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
197 if (MArch == 0) {
198 std::cerr << argv[0] << ": error auto-selecting target for module '"
199 << Err << "'. Please use the -march option to explicitly "
200 << "pick a target.\n";
Misha Brukmanb6ee6062004-07-12 22:58:07 +0000201 return 1;
Brian Gaekec39d16a2003-06-18 21:43:33 +0000202 }
Brian Gaekec39d16a2003-06-18 21:43:33 +0000203 }
Reid Spencer996ec722004-12-30 05:36:08 +0000204
Jim Laskey19058c32005-09-01 21:38:21 +0000205 // Package up features to be passed to target/subtarget
206 std::string FeaturesStr;
207 if (MCPU.size() || MAttrs.size()) {
208 SubtargetFeatures Features;
209 Features.setCPU(MCPU);
210 for (unsigned i = 0; i != MAttrs.size(); ++i)
211 Features.AddFeature(MAttrs[i]);
212 FeaturesStr = Features.getString();
213 }
214
Chris Lattner811dd8d2006-03-23 05:28:02 +0000215 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
Reid Spencer996ec722004-12-30 05:36:08 +0000216 assert(target.get() && "Could not allocate target machine!");
217 TargetMachine &Target = *target.get();
Reid Spencer996ec722004-12-30 05:36:08 +0000218
Reid Spencer996ec722004-12-30 05:36:08 +0000219 // Figure out where we are going to send the output...
Chris Lattner12e97302006-09-04 04:14:57 +0000220 std::ostream *Out = GetOutputStream(argv[0]);
221 if (Out == 0) return 1;
222
223 // If this target requires addPassesToEmitWholeFile, do it now. This is
224 // used by strange things like the C backend.
225 if (Target.WantsWholeFile()) {
226 PassManager PM;
227 PM.add(new TargetData(*Target.getTargetData()));
228 if (!NoVerify)
229 PM.add(createVerifierPass());
230
231 // Ask the target to add backend passes as necessary.
232 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
233 std::cerr << argv[0] << ": target does not support generation of this"
234 << " file type!\n";
235 if (Out != &std::cout) delete Out;
236 // And the Out file is empty and useless, so remove it now.
237 sys::Path(OutputFilename).eraseFromDisk();
238 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000239 }
Chris Lattner12e97302006-09-04 04:14:57 +0000240 PM.run(mod);
Chris Lattner422de252003-06-18 18:46:08 +0000241 } else {
Chris Lattner12e97302006-09-04 04:14:57 +0000242 // Build up all of the passes that we want to do to the module.
243 FunctionPassManager Passes(new ExistingModuleProvider(M.get()));
244 Passes.add(new TargetData(*Target.getTargetData()));
245
246#ifndef NDEBUG
247 if (!NoVerify)
248 Passes.add(createVerifierPass());
249#endif
250
251 // Ask the target to add backend passes as necessary.
Bill Wendlingfe5ee142007-02-08 01:41:07 +0000252 MachineCodeEmitter *MCE = 0;
253
254 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
255 default:
256 assert(0 && "Invalid file model!");
257 return 1;
258 case FileModel::Error:
259 std::cerr << argv[0] << ": target does not support generation of this"
260 << " file type!\n";
261 if (Out != &std::cout) delete Out;
262 // And the Out file is empty and useless, so remove it now.
263 sys::Path(OutputFilename).eraseFromDisk();
264 return 1;
265 case FileModel::AsmFile:
266 break;
267 case FileModel::MachOFile:
268 MCE = AddMachOWriter(Passes, *Out, Target);
269 break;
270 case FileModel::ElfFile:
271 MCE = AddELFWriter(Passes, *Out, Target);
272 break;
273 }
274
275 if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) {
Chris Lattner12e97302006-09-04 04:14:57 +0000276 std::cerr << argv[0] << ": target does not support generation of this"
277 << " file type!\n";
278 if (Out != &std::cout) delete Out;
279 // And the Out file is empty and useless, so remove it now.
280 sys::Path(OutputFilename).eraseFromDisk();
281 return 1;
Chris Lattner7f74a562002-01-20 22:54:45 +0000282 }
Chris Lattner12e97302006-09-04 04:14:57 +0000283
284 Passes.doInitialization();
285
Reid Spencer996ec722004-12-30 05:36:08 +0000286 // Run our queue of passes all at once now, efficiently.
Chris Lattner12e97302006-09-04 04:14:57 +0000287 // TODO: this could lazily stream functions out of the module.
288 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000289 if (!I->isDeclaration())
Chris Lattner12e97302006-09-04 04:14:57 +0000290 Passes.run(*I);
291
292 Passes.doFinalization();
Reid Spencer996ec722004-12-30 05:36:08 +0000293 }
Chris Lattner12e97302006-09-04 04:14:57 +0000294
Reid Spencer996ec722004-12-30 05:36:08 +0000295 // Delete the ostream if it's not a stdout stream
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000296 if (Out != &std::cout) delete Out;
Reid Spencer996ec722004-12-30 05:36:08 +0000297
298 return 0;
299 } catch (const std::string& msg) {
300 std::cerr << argv[0] << ": " << msg << "\n";
301 } catch (...) {
302 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner5667f0e2002-10-29 21:12:46 +0000303 }
Reid Spencer996ec722004-12-30 05:36:08 +0000304 return 1;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000305}