blob: e08bad93b07d125e4ea96da39f243e7f8bd0f063 [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"
Evan Cheng8264e272011-06-29 01:14:12 +000024#include "llvm/MC/SubtargetFeature.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
David Greene6e55be62010-01-05 01:30:21 +000026#include "llvm/Support/Debug.h"
David Greenea31f96c2009-07-14 20:18:05 +000027#include "llvm/Support/FormattedStream.h"
Chris Lattner76d46322006-12-06 01:18:01 +000028#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000030#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000031#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000032#include "llvm/Support/Host.h"
33#include "llvm/Support/Signals.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/TargetSelect.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000036#include "llvm/Target/TargetData.h"
37#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000038#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000039using namespace llvm;
40
Vikram S. Adveeb818692002-09-16 16:35:34 +000041// General options for llc. Other pass-specific options are specified
42// within the corresponding llc passes, and target-specific options
43// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000044//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000045static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000046InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000047
Chris Lattnerd64b2de2003-04-25 05:26:11 +000048static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000049OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
50
Evan Cheng09bd0b12009-05-04 23:05:19 +000051// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000052static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000053OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000054 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
55 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000056 cl::Prefix,
57 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000058 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000059
Chris Lattnere568b882005-12-16 04:59:57 +000060static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000061TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000062
Daniel Dunbard3706452009-07-16 02:23:53 +000063static cl::opt<std::string>
64MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000065
Jim Laskey19058c32005-09-01 21:38:21 +000066static cl::opt<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000067MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000068 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000069 cl::value_desc("cpu-name"),
70 cl::init(""));
71
72static cl::list<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000073MAttrs("mattr",
Jim Laskey19058c32005-09-01 21:38:21 +000074 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000075 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000076 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000077
Evan Cheng2129f592011-07-19 06:37:02 +000078static cl::opt<Reloc::Model>
79RelocModel("relocation-model",
80 cl::desc("Choose relocation model"),
81 cl::init(Reloc::Default),
82 cl::values(
83 clEnumValN(Reloc::Default, "default",
84 "Target default relocation model"),
85 clEnumValN(Reloc::Static, "static",
86 "Non-relocatable code"),
87 clEnumValN(Reloc::PIC_, "pic",
88 "Fully relocatable, position independent code"),
89 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
90 "Relocatable external references, non-relocatable code"),
91 clEnumValEnd));
92
Evan Chengefd9b422011-07-20 07:51:56 +000093static cl::opt<llvm::CodeModel::Model>
94CMModel("code-model",
95 cl::desc("Choose code model"),
96 cl::init(CodeModel::Default),
97 cl::values(clEnumValN(CodeModel::Default, "default",
98 "Target default code model"),
99 clEnumValN(CodeModel::Small, "small",
100 "Small code model"),
101 clEnumValN(CodeModel::Kernel, "kernel",
102 "Kernel code model"),
103 clEnumValN(CodeModel::Medium, "medium",
104 "Medium code model"),
105 clEnumValN(CodeModel::Large, "large",
106 "Large code model"),
107 clEnumValEnd));
108
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000109static cl::opt<bool>
Michael J. Spencer4f50b972010-08-06 21:37:45 +0000110RelaxAll("mc-relax-all",
111 cl::desc("When used with filetype=obj, "
Michael J. Spencer4c089582010-08-08 23:26:49 +0000112 "relax all fixups in the emitted object file"));
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000113
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000114cl::opt<TargetMachine::CodeGenFileType>
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000115FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000116 cl::desc("Choose a file type (not all types are supported by all targets):"),
117 cl::values(
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000118 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000119 "Emit an assembly ('.s') file"),
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000120 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
Benjamin Kramer26426152012-06-11 09:40:10 +0000121 "Emit a native object ('.o') file"),
Chris Lattneredcf0652010-02-03 05:55:08 +0000122 clEnumValN(TargetMachine::CGFT_Null, "null",
123 "Emit nothing, for performance testing"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000124 clEnumValEnd));
125
Reid Spencer8e3830d2005-07-28 02:25:30 +0000126cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +0000127 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +0000128
Devang Patel85df0cc2010-12-01 15:36:49 +0000129cl::opt<bool> DisableDotLoc("disable-dot-loc", cl::Hidden,
130 cl::desc("Do not use .loc entries"));
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000131
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000132cl::opt<bool> DisableCFI("disable-cfi", cl::Hidden,
133 cl::desc("Do not use .cfi_* directives"));
134
Nick Lewyckyaab61692011-10-31 01:06:02 +0000135cl::opt<bool> EnableDwarfDirectory("enable-dwarf-directory", cl::Hidden,
136 cl::desc("Use .file directives with an explicit directory."));
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000137
Devang Patel72a4d2f2009-06-04 22:05:33 +0000138static cl::opt<bool>
139DisableRedZone("disable-red-zone",
140 cl::desc("Do not emit code that uses the red zone."),
141 cl::init(false));
142
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000143static cl::opt<bool>
144EnableFPMAD("enable-fp-mad",
145 cl::desc("Enable less precise MAD instructions to be generated"),
146 cl::init(false));
147
148static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000149DisableFPElim("disable-fp-elim",
150 cl::desc("Disable frame pointer elimination optimization"),
151 cl::init(false));
152
153static cl::opt<bool>
154DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
155 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
156 cl::init(false));
157
158static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000159EnableUnsafeFPMath("enable-unsafe-fp-math",
160 cl::desc("Enable optimizations that may decrease FP precision"),
161 cl::init(false));
162
163static cl::opt<bool>
164EnableNoInfsFPMath("enable-no-infs-fp-math",
165 cl::desc("Enable FP math optimizations that assume no +-Infs"),
166 cl::init(false));
167
168static cl::opt<bool>
169EnableNoNaNsFPMath("enable-no-nans-fp-math",
170 cl::desc("Enable FP math optimizations that assume no NaNs"),
171 cl::init(false));
172
173static cl::opt<bool>
174EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
175 cl::Hidden,
176 cl::desc("Force codegen to assume rounding mode can change dynamically"),
177 cl::init(false));
178
179static cl::opt<bool>
180GenerateSoftFloatCalls("soft-float",
181 cl::desc("Generate software floating point library calls"),
182 cl::init(false));
183
184static cl::opt<llvm::FloatABI::ABIType>
185FloatABIForCalls("float-abi",
186 cl::desc("Choose float ABI type"),
187 cl::init(FloatABI::Default),
188 cl::values(
189 clEnumValN(FloatABI::Default, "default",
190 "Target default float ABI type"),
191 clEnumValN(FloatABI::Soft, "soft",
192 "Soft float ABI (implied by -soft-float)"),
193 clEnumValN(FloatABI::Hard, "hard",
194 "Hard float ABI (uses FP registers)"),
195 clEnumValEnd));
196
Lang Hamesb8650f12012-06-22 01:09:09 +0000197static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
198FuseFPOps("fuse-fp-ops",
199 cl::desc("Enable aggresive formation of fused FP ops"),
200 cl::init(FPOpFusion::Standard),
201 cl::values(
202 clEnumValN(FPOpFusion::Fast, "fast",
203 "Fuse FP ops whenever profitable"),
204 clEnumValN(FPOpFusion::Standard, "standard",
205 "Only fuse 'blessed' FP ops."),
206 clEnumValN(FPOpFusion::Strict, "strict",
207 "Only fuse FP ops when the result won't be effected."),
208 clEnumValEnd));
209
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000210static cl::opt<bool>
211DontPlaceZerosInBSS("nozero-initialized-in-bss",
212 cl::desc("Don't place zero-initialized symbols into bss section"),
213 cl::init(false));
214
215static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000216EnableGuaranteedTailCallOpt("tailcallopt",
217 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
218 cl::init(false));
219
Nick Lewyckyecc00842012-01-19 00:34:10 +0000220static cl::opt<bool>
221DisableTailCalls("disable-tail-calls",
222 cl::desc("Never emit tail calls"),
223 cl::init(false));
224
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000225static cl::opt<unsigned>
226OverrideStackAlignment("stack-alignment",
227 cl::desc("Override default stack alignment"),
228 cl::init(0));
229
230static cl::opt<bool>
231EnableRealignStack("realign-stack",
232 cl::desc("Realign stack if needed"),
233 cl::init(true));
234
235static cl::opt<bool>
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000236DisableSwitchTables(cl::Hidden, "disable-jump-tables",
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000237 cl::desc("Do not generate jump tables."),
238 cl::init(false));
239
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000240static cl::opt<std::string>
241TrapFuncName("trap-func", cl::Hidden,
242 cl::desc("Emit a call to trap function rather than a trap instruction"),
243 cl::init(""));
244
245static cl::opt<bool>
Chandler Carruthede4a8a2012-04-08 17:51:45 +0000246EnablePIE("enable-pie",
247 cl::desc("Assume the creation of a position independent executable."),
248 cl::init(false));
249
250static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000251SegmentedStacks("segmented-stacks",
252 cl::desc("Use segmented stacks if possible."),
253 cl::init(false));
254
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000255static cl::opt<bool>
256UseInitArray("use-init-array",
257 cl::desc("Use .init_array instead of .ctors."),
258 cl::init(false));
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000259
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000260// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000261static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +0000262GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000263 std::string IFN = InputFilename;
264 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000265 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000266 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +0000267 IFN[Len-3] == '.' &&
268 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
269 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000270 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000271 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000272 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000273 }
274 return outputFilename;
275}
276
Dan Gohmana2233f22010-09-01 14:20:41 +0000277static tool_output_file *GetOutputStream(const char *TargetName,
278 Triple::OSType OS,
279 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000280 // If we don't yet have an output filename, make one.
281 if (OutputFilename.empty()) {
282 if (InputFilename == "-")
283 OutputFilename = "-";
284 else {
285 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000286
Dan Gohman7ba6f222010-08-18 17:55:15 +0000287 switch (FileType) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000288 case TargetMachine::CGFT_AssemblyFile:
289 if (TargetName[0] == 'c') {
290 if (TargetName[1] == 0)
291 OutputFilename += ".cbe.c";
292 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
293 OutputFilename += ".cpp";
294 else
295 OutputFilename += ".s";
296 } else
297 OutputFilename += ".s";
298 break;
299 case TargetMachine::CGFT_ObjectFile:
300 if (OS == Triple::Win32)
301 OutputFilename += ".obj";
302 else
303 OutputFilename += ".o";
304 break;
305 case TargetMachine::CGFT_Null:
306 OutputFilename += ".null";
307 break;
308 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000309 }
Chris Lattner12e97302006-09-04 04:14:57 +0000310 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000311
Dan Gohman7ba6f222010-08-18 17:55:15 +0000312 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000313 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000314 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000315 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000316 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000317 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000318 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000319 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000320 break;
321 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000322
Dan Gohman7ba6f222010-08-18 17:55:15 +0000323 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000324 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000325 unsigned OpenFlags = 0;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000326 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
Dan Gohman268b0f42010-08-20 01:07:01 +0000327 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
328 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000329 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000330 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000331 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000332 return 0;
333 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000334
Dan Gohmana2233f22010-09-01 14:20:41 +0000335 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000336}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000337
Chris Lattner67e08422003-06-20 15:49:04 +0000338// main - Entry point for the llc compiler.
339//
340int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000341 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000342 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000343
344 // Enable debug stream buffering.
345 EnableDebugBuffering = true;
346
Owen Anderson19251ec2009-07-15 22:16:10 +0000347 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000348 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000349
Daniel Dunbar76628de2009-09-03 05:47:22 +0000350 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000351 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000352 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000353 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000354 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000355
Chandler Carruth2d71c422011-07-22 07:50:48 +0000356 // Register the target printer for --version.
357 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
358
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000359 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000360
Chris Lattner6dd290a2007-05-06 04:55:19 +0000361 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000362 SMDiagnostic Err;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000363 std::auto_ptr<Module> M;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000364
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000365 M.reset(ParseIRFile(InputFilename, Err, Context));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000366 if (M.get() == 0) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000367 Err.print(argv[0], errs());
Chris Lattner6dd290a2007-05-06 04:55:19 +0000368 return 1;
369 }
370 Module &mod = *M.get();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000371
Chris Lattner6dd290a2007-05-06 04:55:19 +0000372 // If we are supposed to override the target triple, do so now.
373 if (!TargetTriple.empty())
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000374 mod.setTargetTriple(Triple::normalize(TargetTriple));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000375
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000376 // Figure out the target triple.
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000377 Triple TheTriple(mod.getTargetTriple());
378 if (TheTriple.getTriple().empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000379 TheTriple.setTriple(sys::getDefaultTargetTriple());
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000380
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000381 // Get the target specific parser.
382 std::string Error;
383 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
384 Error);
385 if (!TheTarget) {
386 errs() << argv[0] << ": " << Error;
387 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000388 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000389
390 // Package up features to be passed to target/subtarget
391 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000392 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000393 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000394 for (unsigned i = 0; i != MAttrs.size(); ++i)
395 Features.AddFeature(MAttrs[i]);
396 FeaturesStr = Features.getString();
397 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000398
Evan Chengecb29082011-11-16 08:38:26 +0000399 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
400 switch (OptLevel) {
401 default:
402 errs() << argv[0] << ": invalid optimization level.\n";
403 return 1;
404 case ' ': break;
405 case '0': OLvl = CodeGenOpt::None; break;
406 case '1': OLvl = CodeGenOpt::Less; break;
407 case '2': OLvl = CodeGenOpt::Default; break;
408 case '3': OLvl = CodeGenOpt::Aggressive; break;
409 }
410
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000411 TargetOptions Options;
412 Options.LessPreciseFPMADOption = EnableFPMAD;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000413 Options.NoFramePointerElim = DisableFPElim;
414 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
Lang Hamesb8650f12012-06-22 01:09:09 +0000415 Options.AllowFPOpFusion = FuseFPOps;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000416 Options.UnsafeFPMath = EnableUnsafeFPMath;
417 Options.NoInfsFPMath = EnableNoInfsFPMath;
418 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
419 Options.HonorSignDependentRoundingFPMathOption =
420 EnableHonorSignDependentRoundingFPMath;
421 Options.UseSoftFloat = GenerateSoftFloatCalls;
422 if (FloatABIForCalls != FloatABI::Default)
423 Options.FloatABIType = FloatABIForCalls;
424 Options.NoZerosInBSS = DontPlaceZerosInBSS;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000425 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Nick Lewyckyecc00842012-01-19 00:34:10 +0000426 Options.DisableTailCalls = DisableTailCalls;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000427 Options.StackAlignmentOverride = OverrideStackAlignment;
428 Options.RealignStack = EnableRealignStack;
429 Options.DisableJumpTables = DisableSwitchTables;
430 Options.TrapFuncName = TrapFuncName;
Chandler Carruthede4a8a2012-04-08 17:51:45 +0000431 Options.PositionIndependentExecutable = EnablePIE;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000432 Options.EnableSegmentedStacks = SegmentedStacks;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000433 Options.UseInitArray = UseInitArray;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000434
Andrew Trickb826ae82011-04-05 18:41:31 +0000435 std::auto_ptr<TargetMachine>
Evan Chengefd9b422011-07-20 07:51:56 +0000436 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000437 MCPU, FeaturesStr, Options,
Evan Chengecb29082011-11-16 08:38:26 +0000438 RelocModel, CMModel, OLvl));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000439 assert(target.get() && "Could not allocate target machine!");
440 TargetMachine &Target = *target.get();
441
Devang Patel85df0cc2010-12-01 15:36:49 +0000442 if (DisableDotLoc)
443 Target.setMCUseLoc(false);
Daniel Dunbared3d5492011-04-19 20:46:13 +0000444
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000445 if (DisableCFI)
446 Target.setMCUseCFI(false);
447
Nick Lewyckyaab61692011-10-31 01:06:02 +0000448 if (EnableDwarfDirectory)
449 Target.setMCUseDwarfDirectory(true);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000450
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000451 if (GenerateSoftFloatCalls)
452 FloatABIForCalls = FloatABI::Soft;
453
Daniel Dunbared3d5492011-04-19 20:46:13 +0000454 // Disable .loc support for older OS X versions.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000455 if (TheTriple.isMacOSX() &&
Daniel Dunbarc7f2f142011-04-20 00:47:19 +0000456 TheTriple.isMacOSXVersionLT(10, 6))
Daniel Dunbared3d5492011-04-19 20:46:13 +0000457 Target.setMCUseLoc(false);
Devang Patel85df0cc2010-12-01 15:36:49 +0000458
Chris Lattner6dd290a2007-05-06 04:55:19 +0000459 // Figure out where we are going to send the output...
Dan Gohmana2233f22010-09-01 14:20:41 +0000460 OwningPtr<tool_output_file> Out
Dan Gohman268b0f42010-08-20 01:07:01 +0000461 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
462 if (!Out) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000463
Dan Gohman4cfccb82010-05-11 19:57:55 +0000464 // Build up all of the passes that we want to do to the module.
465 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000466
Dan Gohman4cfccb82010-05-11 19:57:55 +0000467 // Add the target data from the target machine, if it exists, or the module.
468 if (const TargetData *TD = Target.getTargetData())
469 PM.add(new TargetData(*TD));
470 else
471 PM.add(new TargetData(&mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000472
Dan Gohman4cfccb82010-05-11 19:57:55 +0000473 // Override default to generate verbose assembly.
474 Target.setAsmVerbosityDefault(true);
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000475
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000476 if (RelaxAll) {
477 if (FileType != TargetMachine::CGFT_ObjectFile)
478 errs() << argv[0]
479 << ": warning: ignoring -mc-relax-all because filetype != obj";
480 else
481 Target.setMCRelaxAll(true);
482 }
483
Dan Gohmana2233f22010-09-01 14:20:41 +0000484 {
485 formatted_raw_ostream FOS(Out->os());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000486
Dan Gohmana2233f22010-09-01 14:20:41 +0000487 // Ask the target to add backend passes as necessary.
Evan Chengecb29082011-11-16 08:38:26 +0000488 if (Target.addPassesToEmitFile(PM, FOS, FileType, NoVerify)) {
Dan Gohmana2233f22010-09-01 14:20:41 +0000489 errs() << argv[0] << ": target does not support generation of this"
490 << " file type!\n";
491 return 1;
492 }
493
Andrew Trick12004012011-04-05 18:54:36 +0000494 // Before executing passes, print the final values of the LLVM options.
495 cl::PrintOptionValues();
496
Dan Gohmana2233f22010-09-01 14:20:41 +0000497 PM.run(mod);
498 }
Dan Gohman4cfccb82010-05-11 19:57:55 +0000499
Dan Gohman268b0f42010-08-20 01:07:01 +0000500 // Declare success.
501 Out->keep();
Chris Lattner6dd290a2007-05-06 04:55:19 +0000502
503 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000504}