blob: 4cb3cf1cb96bbee9127edfa88a0ef38a01e8b473 [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//
Chris Lattner345353d2007-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 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
Gabor Greife16561c2007-07-05 17:07:56 +000012// or C code, given LLVM bitcode.
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
Owen Anderson6773d382009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Chris Lattnered226062001-09-07 21:26:31 +000017#include "llvm/Module.h"
Chris Lattner7139f282002-01-31 00:46:45 +000018#include "llvm/PassManager.h"
Vikram S. Adveeb818692002-09-16 16:35:34 +000019#include "llvm/Pass.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000020#include "llvm/ADT/Triple.h"
Dan Gohmanc76bfb72009-09-02 19:35:19 +000021#include "llvm/Support/IRReader.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000022#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
23#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000024#include "llvm/Config/config.h"
Evan Cheng8264e272011-06-29 01:14:12 +000025#include "llvm/MC/SubtargetFeature.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
David Greene6e55be62010-01-05 01:30:21 +000027#include "llvm/Support/Debug.h"
David Greenea31f96c2009-07-14 20:18:05 +000028#include "llvm/Support/FormattedStream.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"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000031#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000032#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/Host.h"
34#include "llvm/Support/Signals.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000035#include "llvm/Support/TargetRegistry.h"
36#include "llvm/Support/TargetSelect.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000037#include "llvm/Target/TargetData.h"
38#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000039#include <memory>
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>
Gabor Greife16561c2007-07-05 17:07:56 +000047InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000048
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
Evan Cheng09bd0b12009-05-04 23:05:19 +000052// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000053static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000054OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000055 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
56 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000057 cl::Prefix,
58 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000059 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000060
Chris Lattnere568b882005-12-16 04:59:57 +000061static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000062TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000063
Daniel Dunbard3706452009-07-16 02:23:53 +000064static cl::opt<std::string>
65MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000066
Jim Laskey19058c32005-09-01 21:38:21 +000067static cl::opt<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000068MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000069 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000070 cl::value_desc("cpu-name"),
71 cl::init(""));
72
73static cl::list<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000074MAttrs("mattr",
Jim Laskey19058c32005-09-01 21:38:21 +000075 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000076 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000077 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000078
Evan Cheng2129f592011-07-19 06:37:02 +000079static cl::opt<Reloc::Model>
80RelocModel("relocation-model",
81 cl::desc("Choose relocation model"),
82 cl::init(Reloc::Default),
83 cl::values(
84 clEnumValN(Reloc::Default, "default",
85 "Target default relocation model"),
86 clEnumValN(Reloc::Static, "static",
87 "Non-relocatable code"),
88 clEnumValN(Reloc::PIC_, "pic",
89 "Fully relocatable, position independent code"),
90 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
91 "Relocatable external references, non-relocatable code"),
92 clEnumValEnd));
93
Evan Chengefd9b422011-07-20 07:51:56 +000094static cl::opt<llvm::CodeModel::Model>
95CMModel("code-model",
96 cl::desc("Choose code model"),
97 cl::init(CodeModel::Default),
98 cl::values(clEnumValN(CodeModel::Default, "default",
99 "Target default code model"),
100 clEnumValN(CodeModel::Small, "small",
101 "Small code model"),
102 clEnumValN(CodeModel::Kernel, "kernel",
103 "Kernel code model"),
104 clEnumValN(CodeModel::Medium, "medium",
105 "Medium code model"),
106 clEnumValN(CodeModel::Large, "large",
107 "Large code model"),
108 clEnumValEnd));
109
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000110static cl::opt<bool>
Michael J. Spencer4f50b972010-08-06 21:37:45 +0000111RelaxAll("mc-relax-all",
112 cl::desc("When used with filetype=obj, "
Michael J. Spencer4c089582010-08-08 23:26:49 +0000113 "relax all fixups in the emitted object file"));
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000114
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000115cl::opt<TargetMachine::CodeGenFileType>
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000116FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000117 cl::desc("Choose a file type (not all types are supported by all targets):"),
118 cl::values(
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000119 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000120 "Emit an assembly ('.s') file"),
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000121 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000122 "Emit a native object ('.o') file [experimental]"),
Chris Lattneredcf0652010-02-03 05:55:08 +0000123 clEnumValN(TargetMachine::CGFT_Null, "null",
124 "Emit nothing, for performance testing"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000125 clEnumValEnd));
126
Reid Spencer8e3830d2005-07-28 02:25:30 +0000127cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +0000128 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +0000129
Devang Patel85df0cc2010-12-01 15:36:49 +0000130cl::opt<bool> DisableDotLoc("disable-dot-loc", cl::Hidden,
131 cl::desc("Do not use .loc entries"));
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000132
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000133cl::opt<bool> DisableCFI("disable-cfi", cl::Hidden,
134 cl::desc("Do not use .cfi_* directives"));
135
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000136cl::opt<bool> DisableDwarfDirectory("disable-dwarf-directory", cl::Hidden,
137 cl::desc("Do not use file directives with an explicit directory."));
138
Devang Patel72a4d2f2009-06-04 22:05:33 +0000139static cl::opt<bool>
140DisableRedZone("disable-red-zone",
141 cl::desc("Do not emit code that uses the red zone."),
142 cl::init(false));
143
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000144// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000145static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +0000146GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000147 std::string IFN = InputFilename;
148 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000149 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000150 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +0000151 IFN[Len-3] == '.' &&
152 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
153 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000154 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000155 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000156 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000157 }
158 return outputFilename;
159}
160
Dan Gohmana2233f22010-09-01 14:20:41 +0000161static tool_output_file *GetOutputStream(const char *TargetName,
162 Triple::OSType OS,
163 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000164 // If we don't yet have an output filename, make one.
165 if (OutputFilename.empty()) {
166 if (InputFilename == "-")
167 OutputFilename = "-";
168 else {
169 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000170
Dan Gohman7ba6f222010-08-18 17:55:15 +0000171 switch (FileType) {
172 default: assert(0 && "Unknown file type");
173 case TargetMachine::CGFT_AssemblyFile:
174 if (TargetName[0] == 'c') {
175 if (TargetName[1] == 0)
176 OutputFilename += ".cbe.c";
177 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
178 OutputFilename += ".cpp";
179 else
180 OutputFilename += ".s";
181 } else
182 OutputFilename += ".s";
183 break;
184 case TargetMachine::CGFT_ObjectFile:
185 if (OS == Triple::Win32)
186 OutputFilename += ".obj";
187 else
188 OutputFilename += ".o";
189 break;
190 case TargetMachine::CGFT_Null:
191 OutputFilename += ".null";
192 break;
193 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000194 }
Chris Lattner12e97302006-09-04 04:14:57 +0000195 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000196
Dan Gohman7ba6f222010-08-18 17:55:15 +0000197 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000198 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000199 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000200 default: assert(0 && "Unknown file type");
201 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000202 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000203 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000204 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000205 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000206 break;
207 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000208
Dan Gohman7ba6f222010-08-18 17:55:15 +0000209 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000210 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000211 unsigned OpenFlags = 0;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000212 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
Dan Gohman268b0f42010-08-20 01:07:01 +0000213 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
214 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000215 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000216 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000217 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000218 return 0;
219 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000220
Dan Gohmana2233f22010-09-01 14:20:41 +0000221 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000222}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000223
Chris Lattner67e08422003-06-20 15:49:04 +0000224// main - Entry point for the llc compiler.
225//
226int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000227 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000228 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000229
230 // Enable debug stream buffering.
231 EnableDebugBuffering = true;
232
Owen Anderson19251ec2009-07-15 22:16:10 +0000233 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000234 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000235
Daniel Dunbar76628de2009-09-03 05:47:22 +0000236 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000237 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000238 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000239 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000240 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000241
Chandler Carruth2d71c422011-07-22 07:50:48 +0000242 // Register the target printer for --version.
243 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
244
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000245 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000246
Chris Lattner6dd290a2007-05-06 04:55:19 +0000247 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000248 SMDiagnostic Err;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000249 std::auto_ptr<Module> M;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000250
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000251 M.reset(ParseIRFile(InputFilename, Err, Context));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000252 if (M.get() == 0) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000253 Err.print(argv[0], errs());
Chris Lattner6dd290a2007-05-06 04:55:19 +0000254 return 1;
255 }
256 Module &mod = *M.get();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000257
Chris Lattner6dd290a2007-05-06 04:55:19 +0000258 // If we are supposed to override the target triple, do so now.
259 if (!TargetTriple.empty())
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000260 mod.setTargetTriple(Triple::normalize(TargetTriple));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000261
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000262 Triple TheTriple(mod.getTargetTriple());
263 if (TheTriple.getTriple().empty())
264 TheTriple.setTriple(sys::getHostTriple());
265
266 // Allocate target machine. First, check whether the user has explicitly
267 // specified an architecture to compile for. If so we have to look it up by
268 // name, because it might be a backend that has no mapping to a target triple.
Daniel Dunbard3706452009-07-16 02:23:53 +0000269 const Target *TheTarget = 0;
270 if (!MArch.empty()) {
271 for (TargetRegistry::iterator it = TargetRegistry::begin(),
272 ie = TargetRegistry::end(); it != ie; ++it) {
273 if (MArch == it->getName()) {
274 TheTarget = &*it;
275 break;
276 }
277 }
278
279 if (!TheTarget) {
280 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
281 return 1;
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000282 }
283
284 // Adjust the triple to match (if known), otherwise stick with the
285 // module/host triple.
286 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
287 if (Type != Triple::UnknownArch)
288 TheTriple.setArch(Type);
Daniel Dunbare8338102009-07-15 20:24:03 +0000289 } else {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000290 std::string Err;
Daniel Dunbar719d2352009-08-03 04:20:57 +0000291 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err);
Daniel Dunbare8338102009-07-15 20:24:03 +0000292 if (TheTarget == 0) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000293 errs() << argv[0] << ": error auto-selecting target for module '"
294 << Err << "'. Please use the -march option to explicitly "
295 << "pick a target.\n";
Chris Lattnerb1492402003-08-24 14:02:14 +0000296 return 1;
Chris Lattner6142ca82004-07-11 04:03:24 +0000297 }
Chris Lattner5667f0e2002-10-29 21:12:46 +0000298 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000299
300 // Package up features to be passed to target/subtarget
301 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000302 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000303 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000304 for (unsigned i = 0; i != MAttrs.size(); ++i)
305 Features.AddFeature(MAttrs[i]);
306 FeaturesStr = Features.getString();
307 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000308
Andrew Trickb826ae82011-04-05 18:41:31 +0000309 std::auto_ptr<TargetMachine>
Evan Chengefd9b422011-07-20 07:51:56 +0000310 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
311 MCPU, FeaturesStr,
312 RelocModel, CMModel));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000313 assert(target.get() && "Could not allocate target machine!");
314 TargetMachine &Target = *target.get();
315
Devang Patel85df0cc2010-12-01 15:36:49 +0000316 if (DisableDotLoc)
317 Target.setMCUseLoc(false);
Daniel Dunbared3d5492011-04-19 20:46:13 +0000318
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000319 if (DisableCFI)
320 Target.setMCUseCFI(false);
321
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000322 if (DisableDwarfDirectory)
323 Target.setMCUseDwarfDirectory(false);
324
Daniel Dunbared3d5492011-04-19 20:46:13 +0000325 // Disable .loc support for older OS X versions.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000326 if (TheTriple.isMacOSX() &&
Daniel Dunbarc7f2f142011-04-20 00:47:19 +0000327 TheTriple.isMacOSXVersionLT(10, 6))
Daniel Dunbared3d5492011-04-19 20:46:13 +0000328 Target.setMCUseLoc(false);
Devang Patel85df0cc2010-12-01 15:36:49 +0000329
Chris Lattner6dd290a2007-05-06 04:55:19 +0000330 // Figure out where we are going to send the output...
Dan Gohmana2233f22010-09-01 14:20:41 +0000331 OwningPtr<tool_output_file> Out
Dan Gohman268b0f42010-08-20 01:07:01 +0000332 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
333 if (!Out) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000334
Evan Cheng09bd0b12009-05-04 23:05:19 +0000335 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling026e5d72009-04-29 23:29:43 +0000336 switch (OptLevel) {
337 default:
Dan Gohmand8db3762009-07-15 16:35:29 +0000338 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendlingdad99192009-04-29 23:46:43 +0000339 return 1;
Bill Wendling026e5d72009-04-29 23:29:43 +0000340 case ' ': break;
341 case '0': OLvl = CodeGenOpt::None; break;
Evan Cheng5a286382009-10-16 21:02:20 +0000342 case '1': OLvl = CodeGenOpt::Less; break;
Bill Wendlingaeaf5a52009-04-30 00:57:51 +0000343 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000344 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000345 }
346
Dan Gohman4cfccb82010-05-11 19:57:55 +0000347 // Build up all of the passes that we want to do to the module.
348 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000349
Dan Gohman4cfccb82010-05-11 19:57:55 +0000350 // Add the target data from the target machine, if it exists, or the module.
351 if (const TargetData *TD = Target.getTargetData())
352 PM.add(new TargetData(*TD));
353 else
354 PM.add(new TargetData(&mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000355
Dan Gohman4cfccb82010-05-11 19:57:55 +0000356 // Override default to generate verbose assembly.
357 Target.setAsmVerbosityDefault(true);
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000358
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000359 if (RelaxAll) {
360 if (FileType != TargetMachine::CGFT_ObjectFile)
361 errs() << argv[0]
362 << ": warning: ignoring -mc-relax-all because filetype != obj";
363 else
364 Target.setMCRelaxAll(true);
365 }
366
Dan Gohmana2233f22010-09-01 14:20:41 +0000367 {
368 formatted_raw_ostream FOS(Out->os());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000369
Dan Gohmana2233f22010-09-01 14:20:41 +0000370 // Ask the target to add backend passes as necessary.
371 if (Target.addPassesToEmitFile(PM, FOS, FileType, OLvl, NoVerify)) {
372 errs() << argv[0] << ": target does not support generation of this"
373 << " file type!\n";
374 return 1;
375 }
376
Andrew Trick12004012011-04-05 18:54:36 +0000377 // Before executing passes, print the final values of the LLVM options.
378 cl::PrintOptionValues();
379
Dan Gohmana2233f22010-09-01 14:20:41 +0000380 PM.run(mod);
381 }
Dan Gohman4cfccb82010-05-11 19:57:55 +0000382
Dan Gohman268b0f42010-08-20 01:07:01 +0000383 // Declare success.
384 Out->keep();
Chris Lattner6dd290a2007-05-06 04:55:19 +0000385
386 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000387}