blob: fd7afa9d1f98635c2e2fa27b8700137f41cf8c1b [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"
Chris Lattnerd3a680a2006-08-01 22:34:35 +000017#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000018#include "llvm/Target/SubtargetFeature.h"
Owen Anderson07000c62006-05-12 06:33:49 +000019#include "llvm/Target/TargetData.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000021#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000022#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000023#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000024#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000025#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/PluginLoader.h"
Duraid Madinaea8aed72005-12-30 02:47:21 +000029#include "llvm/Support/FileUtilities.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000030#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000031#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000032#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000033#include "llvm/LinkAllVMCore.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000034#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000035#include <iostream>
36#include <memory>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000037
Brian Gaeked0fde302003-11-11 22:41:34 +000038using namespace llvm;
39
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000040// General options for llc. Other pass-specific options are specified
41// within the corresponding llc passes, and target-specific options
42// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000043//
Chris Lattnerb5881f12003-04-25 05:26:11 +000044static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000045InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
46
Chris Lattnerb5881f12003-04-25 05:26:11 +000047static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000048OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
49
50static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
51
Chris Lattner178e0c42005-11-08 02:12:17 +000052static cl::opt<bool> Fast("fast",
53 cl::desc("Generate code quickly, potentially sacrificing code quality"));
54
Chris Lattnerf33b8662005-12-16 04:59:57 +000055static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000056TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000057
Chris Lattnere45110e2004-07-11 04:03:24 +000058static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000059MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000060
Jim Laskeyb1e11802005-09-01 21:38:21 +000061static cl::opt<std::string>
62MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000063 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000064 cl::value_desc("cpu-name"),
65 cl::init(""));
66
67static cl::list<std::string>
68MAttrs("mattr",
69 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000070 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000071 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000072
Chris Lattner812125a2005-06-25 03:32:05 +000073cl::opt<TargetMachine::CodeGenFileType>
74FileType("filetype", cl::init(TargetMachine::AssemblyFile),
75 cl::desc("Choose a file type (not all types are supported by all targets):"),
76 cl::values(
77 clEnumValN(TargetMachine::AssemblyFile, "asm",
78 " Emit an assembly ('.s') file"),
79 clEnumValN(TargetMachine::ObjectFile, "obj",
Chris Lattner07be8d42005-10-22 22:00:45 +000080 " Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000081 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Nate Begeman712b8352006-08-23 21:29:52 +000082 " Emit a native dynamic library ('.so') file"
83 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000084 clEnumValEnd));
85
Reid Spencer4418c2b2005-07-28 02:25:30 +000086cl::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
Chris Lattner1911fd42006-09-04 04:14:57 +0000105static std::ostream *GetOutputStream(const char *ProgName) {
106 if (OutputFilename != "") {
107 if (OutputFilename == "-")
108 return &std::cout;
109
110 // Specified an output filename?
111 if (!Force && std::ifstream(OutputFilename.c_str())) {
112 // If force is not specified, make sure not to overwrite a file!
113 std::cerr << ProgName << ": error opening '" << OutputFilename
114 << "': file exists!\n"
115 << "Use -f command line argument to force output\n";
116 return 0;
117 }
118 // Make sure that the Out file gets unlinked from the disk if we get a
119 // SIGINT
120 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
121
122 return new std::ofstream(OutputFilename.c_str());
123 }
124
125 if (InputFilename == "-") {
126 OutputFilename = "-";
127 return &std::cout;
128 }
129
130 OutputFilename = GetFileNameRoot(InputFilename);
131
132 switch (FileType) {
133 case TargetMachine::AssemblyFile:
134 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
135 OutputFilename += ".s";
136 else
137 OutputFilename += ".cbe.c";
138 break;
139 case TargetMachine::ObjectFile:
140 OutputFilename += ".o";
141 break;
142 case TargetMachine::DynamicLibrary:
143 OutputFilename += LTDL_SHLIB_EXT;
144 break;
145 }
146
147 if (!Force && std::ifstream(OutputFilename.c_str())) {
148 // If force is not specified, make sure not to overwrite a file!
149 std::cerr << ProgName << ": error opening '" << OutputFilename
150 << "': file exists!\n"
151 << "Use -f command line argument to force output\n";
152 return 0;
153 }
154
155 // Make sure that the Out file gets unlinked from the disk if we get a
156 // SIGINT
157 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
158
159 std::ostream *Out = new std::ofstream(OutputFilename.c_str());
160 if (!Out->good()) {
161 std::cerr << ProgName << ": error opening " << OutputFilename << "!\n";
162 delete Out;
163 return 0;
164 }
165
166 return Out;
167}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000168
Chris Lattner5b836c42003-06-20 15:49:04 +0000169// main - Entry point for the llc compiler.
170//
171int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000172 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000173 try {
174 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
175 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +0000176
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000177 // Load the module to be compiled...
178 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
179 if (M.get() == 0) {
180 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000181 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000182 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000183 Module &mod = *M.get();
Chris Lattnere45110e2004-07-11 04:03:24 +0000184
Chris Lattnerf33b8662005-12-16 04:59:57 +0000185 // If we are supposed to override the target triple, do so now.
186 if (!TargetTriple.empty())
187 mod.setTargetTriple(TargetTriple);
188
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000189 // Allocate target machine. First, check whether the user has
190 // explicitly specified an architecture to compile for.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000191 if (MArch == 0) {
192 std::string Err;
193 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
194 if (MArch == 0) {
195 std::cerr << argv[0] << ": error auto-selecting target for module '"
196 << Err << "'. Please use the -march option to explicitly "
197 << "pick a target.\n";
Misha Brukman878ba7c2004-07-12 22:58:07 +0000198 return 1;
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000199 }
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000200 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000201
Jim Laskeyb1e11802005-09-01 21:38:21 +0000202 // Package up features to be passed to target/subtarget
203 std::string FeaturesStr;
204 if (MCPU.size() || MAttrs.size()) {
205 SubtargetFeatures Features;
206 Features.setCPU(MCPU);
207 for (unsigned i = 0; i != MAttrs.size(); ++i)
208 Features.AddFeature(MAttrs[i]);
209 FeaturesStr = Features.getString();
210 }
211
Chris Lattneref986912006-03-23 05:28:02 +0000212 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000213 assert(target.get() && "Could not allocate target machine!");
214 TargetMachine &Target = *target.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000215
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000216 // Figure out where we are going to send the output...
Chris Lattner1911fd42006-09-04 04:14:57 +0000217 std::ostream *Out = GetOutputStream(argv[0]);
218 if (Out == 0) return 1;
219
220 // If this target requires addPassesToEmitWholeFile, do it now. This is
221 // used by strange things like the C backend.
222 if (Target.WantsWholeFile()) {
223 PassManager PM;
224 PM.add(new TargetData(*Target.getTargetData()));
225 if (!NoVerify)
226 PM.add(createVerifierPass());
227
228 // Ask the target to add backend passes as necessary.
229 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
230 std::cerr << argv[0] << ": target does not support generation of this"
231 << " file type!\n";
232 if (Out != &std::cout) delete Out;
233 // And the Out file is empty and useless, so remove it now.
234 sys::Path(OutputFilename).eraseFromDisk();
235 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000236 }
Chris Lattner1911fd42006-09-04 04:14:57 +0000237 PM.run(mod);
Chris Lattnercccc28c2003-06-18 18:46:08 +0000238 } else {
Chris Lattner1911fd42006-09-04 04:14:57 +0000239 // Build up all of the passes that we want to do to the module.
240 FunctionPassManager Passes(new ExistingModuleProvider(M.get()));
241 Passes.add(new TargetData(*Target.getTargetData()));
242
243#ifndef NDEBUG
244 if (!NoVerify)
245 Passes.add(createVerifierPass());
246#endif
247
248 // Ask the target to add backend passes as necessary.
249 if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
250 std::cerr << argv[0] << ": target does not support generation of this"
251 << " file type!\n";
252 if (Out != &std::cout) delete Out;
253 // And the Out file is empty and useless, so remove it now.
254 sys::Path(OutputFilename).eraseFromDisk();
255 return 1;
Chris Lattner697954c2002-01-20 22:54:45 +0000256 }
Chris Lattner1911fd42006-09-04 04:14:57 +0000257
258 Passes.doInitialization();
259
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000260 // Run our queue of passes all at once now, efficiently.
Chris Lattner1911fd42006-09-04 04:14:57 +0000261 // TODO: this could lazily stream functions out of the module.
262 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
263 if (!I->isExternal())
264 Passes.run(*I);
265
266 Passes.doFinalization();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000267 }
Chris Lattner1911fd42006-09-04 04:14:57 +0000268
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000269 // Delete the ostream if it's not a stdout stream
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000270 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000271
272 return 0;
273 } catch (const std::string& msg) {
274 std::cerr << argv[0] << ": " << msg << "\n";
275 } catch (...) {
276 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner63342052002-10-29 21:12:46 +0000277 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000278 return 1;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000279}