blob: 81f297f594a2b236fb1de5a1ef790bb2e2ce63a1 [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"
Bob Wilsoncac3b902012-07-02 19:48:45 +000021#include "llvm/Assembly/PrintModulePass.h"
Dan Gohmanc76bfb72009-09-02 19:35:19 +000022#include "llvm/Support/IRReader.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000023#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
24#include "llvm/CodeGen/LinkAllCodegenComponents.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"
Bob Wilsonfa594852012-08-03 21:26:18 +000038#include "llvm/Target/TargetLibraryInfo.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000039#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000040#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000041using namespace llvm;
42
Vikram S. Adveeb818692002-09-16 16:35:34 +000043// General options for llc. Other pass-specific options are specified
44// within the corresponding llc passes, and target-specific options
45// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000046//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000047static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000048InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000049
Chris Lattnerd64b2de2003-04-25 05:26:11 +000050static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000051OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
52
Evan Cheng09bd0b12009-05-04 23:05:19 +000053// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000054static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000055OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000056 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
57 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000058 cl::Prefix,
59 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000060 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000061
Chris Lattnere568b882005-12-16 04:59:57 +000062static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000063TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000064
Daniel Dunbard3706452009-07-16 02:23:53 +000065static cl::opt<std::string>
66MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000067
Jim Laskey19058c32005-09-01 21:38:21 +000068static cl::opt<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000069MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000070 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000071 cl::value_desc("cpu-name"),
72 cl::init(""));
73
74static cl::list<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000075MAttrs("mattr",
Jim Laskey19058c32005-09-01 21:38:21 +000076 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000077 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000078 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000079
Evan Cheng2129f592011-07-19 06:37:02 +000080static cl::opt<Reloc::Model>
81RelocModel("relocation-model",
82 cl::desc("Choose relocation model"),
83 cl::init(Reloc::Default),
84 cl::values(
85 clEnumValN(Reloc::Default, "default",
86 "Target default relocation model"),
87 clEnumValN(Reloc::Static, "static",
88 "Non-relocatable code"),
89 clEnumValN(Reloc::PIC_, "pic",
90 "Fully relocatable, position independent code"),
91 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
92 "Relocatable external references, non-relocatable code"),
93 clEnumValEnd));
94
Evan Chengefd9b422011-07-20 07:51:56 +000095static cl::opt<llvm::CodeModel::Model>
96CMModel("code-model",
97 cl::desc("Choose code model"),
98 cl::init(CodeModel::Default),
99 cl::values(clEnumValN(CodeModel::Default, "default",
100 "Target default code model"),
101 clEnumValN(CodeModel::Small, "small",
102 "Small code model"),
103 clEnumValN(CodeModel::Kernel, "kernel",
104 "Kernel code model"),
105 clEnumValN(CodeModel::Medium, "medium",
106 "Medium code model"),
107 clEnumValN(CodeModel::Large, "large",
108 "Large code model"),
109 clEnumValEnd));
110
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000111static cl::opt<bool>
Michael J. Spencer4f50b972010-08-06 21:37:45 +0000112RelaxAll("mc-relax-all",
113 cl::desc("When used with filetype=obj, "
Michael J. Spencer4c089582010-08-08 23:26:49 +0000114 "relax all fixups in the emitted object file"));
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000115
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000116cl::opt<TargetMachine::CodeGenFileType>
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000117FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000118 cl::desc("Choose a file type (not all types are supported by all targets):"),
119 cl::values(
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000120 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000121 "Emit an assembly ('.s') file"),
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000122 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
Benjamin Kramer26426152012-06-11 09:40:10 +0000123 "Emit a native object ('.o') file"),
Chris Lattneredcf0652010-02-03 05:55:08 +0000124 clEnumValN(TargetMachine::CGFT_Null, "null",
125 "Emit nothing, for performance testing"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000126 clEnumValEnd));
127
Reid Spencer8e3830d2005-07-28 02:25:30 +0000128cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +0000129 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +0000130
Devang Patel85df0cc2010-12-01 15:36:49 +0000131cl::opt<bool> DisableDotLoc("disable-dot-loc", cl::Hidden,
132 cl::desc("Do not use .loc entries"));
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000133
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000134cl::opt<bool> DisableCFI("disable-cfi", cl::Hidden,
135 cl::desc("Do not use .cfi_* directives"));
136
Nick Lewyckyaab61692011-10-31 01:06:02 +0000137cl::opt<bool> EnableDwarfDirectory("enable-dwarf-directory", cl::Hidden,
138 cl::desc("Use .file directives with an explicit directory."));
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000139
Devang Patel72a4d2f2009-06-04 22:05:33 +0000140static cl::opt<bool>
141DisableRedZone("disable-red-zone",
142 cl::desc("Do not emit code that uses the red zone."),
143 cl::init(false));
144
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000145static cl::opt<bool>
146EnableFPMAD("enable-fp-mad",
147 cl::desc("Enable less precise MAD instructions to be generated"),
148 cl::init(false));
149
150static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000151DisableFPElim("disable-fp-elim",
152 cl::desc("Disable frame pointer elimination optimization"),
153 cl::init(false));
154
155static cl::opt<bool>
156DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
157 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
158 cl::init(false));
159
160static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000161EnableUnsafeFPMath("enable-unsafe-fp-math",
162 cl::desc("Enable optimizations that may decrease FP precision"),
163 cl::init(false));
164
165static cl::opt<bool>
166EnableNoInfsFPMath("enable-no-infs-fp-math",
167 cl::desc("Enable FP math optimizations that assume no +-Infs"),
168 cl::init(false));
169
170static cl::opt<bool>
171EnableNoNaNsFPMath("enable-no-nans-fp-math",
172 cl::desc("Enable FP math optimizations that assume no NaNs"),
173 cl::init(false));
174
175static cl::opt<bool>
176EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
177 cl::Hidden,
178 cl::desc("Force codegen to assume rounding mode can change dynamically"),
179 cl::init(false));
180
181static cl::opt<bool>
182GenerateSoftFloatCalls("soft-float",
183 cl::desc("Generate software floating point library calls"),
184 cl::init(false));
185
186static cl::opt<llvm::FloatABI::ABIType>
187FloatABIForCalls("float-abi",
188 cl::desc("Choose float ABI type"),
189 cl::init(FloatABI::Default),
190 cl::values(
191 clEnumValN(FloatABI::Default, "default",
192 "Target default float ABI type"),
193 clEnumValN(FloatABI::Soft, "soft",
194 "Soft float ABI (implied by -soft-float)"),
195 clEnumValN(FloatABI::Hard, "hard",
196 "Hard float ABI (uses FP registers)"),
197 clEnumValEnd));
198
Lang Hamesb8650f12012-06-22 01:09:09 +0000199static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
Lang Hamesc98ebda2012-06-22 22:31:00 +0000200FuseFPOps("fp-contract",
Lang Hamesb8650f12012-06-22 01:09:09 +0000201 cl::desc("Enable aggresive formation of fused FP ops"),
202 cl::init(FPOpFusion::Standard),
203 cl::values(
204 clEnumValN(FPOpFusion::Fast, "fast",
205 "Fuse FP ops whenever profitable"),
Lang Hamesc98ebda2012-06-22 22:31:00 +0000206 clEnumValN(FPOpFusion::Standard, "on",
Lang Hamesb8650f12012-06-22 01:09:09 +0000207 "Only fuse 'blessed' FP ops."),
Lang Hamesc98ebda2012-06-22 22:31:00 +0000208 clEnumValN(FPOpFusion::Strict, "off",
Lang Hamesb8650f12012-06-22 01:09:09 +0000209 "Only fuse FP ops when the result won't be effected."),
210 clEnumValEnd));
211
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000212static cl::opt<bool>
213DontPlaceZerosInBSS("nozero-initialized-in-bss",
214 cl::desc("Don't place zero-initialized symbols into bss section"),
215 cl::init(false));
216
217static cl::opt<bool>
Bob Wilsonfa594852012-08-03 21:26:18 +0000218DisableSimplifyLibCalls("disable-simplify-libcalls",
219 cl::desc("Disable simplify-libcalls"),
220 cl::init(false));
221
222static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000223EnableGuaranteedTailCallOpt("tailcallopt",
224 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
225 cl::init(false));
226
Nick Lewyckyecc00842012-01-19 00:34:10 +0000227static cl::opt<bool>
228DisableTailCalls("disable-tail-calls",
229 cl::desc("Never emit tail calls"),
230 cl::init(false));
231
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000232static cl::opt<unsigned>
233OverrideStackAlignment("stack-alignment",
234 cl::desc("Override default stack alignment"),
235 cl::init(0));
236
237static cl::opt<bool>
238EnableRealignStack("realign-stack",
239 cl::desc("Realign stack if needed"),
240 cl::init(true));
241
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000242static cl::opt<std::string>
243TrapFuncName("trap-func", cl::Hidden,
244 cl::desc("Emit a call to trap function rather than a trap instruction"),
245 cl::init(""));
246
247static cl::opt<bool>
Chandler Carruthede4a8a2012-04-08 17:51:45 +0000248EnablePIE("enable-pie",
249 cl::desc("Assume the creation of a position independent executable."),
250 cl::init(false));
251
252static cl::opt<bool>
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000253SegmentedStacks("segmented-stacks",
254 cl::desc("Use segmented stacks if possible."),
255 cl::init(false));
256
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000257static cl::opt<bool>
258UseInitArray("use-init-array",
259 cl::desc("Use .init_array instead of .ctors."),
260 cl::init(false));
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000261
Bob Wilsoncac3b902012-07-02 19:48:45 +0000262static cl::opt<std::string> StopAfter("stop-after",
263 cl::desc("Stop compilation after a specific pass"),
264 cl::value_desc("pass-name"),
265 cl::init(""));
266static cl::opt<std::string> StartAfter("start-after",
267 cl::desc("Resume compilation after a specific pass"),
268 cl::value_desc("pass-name"),
269 cl::init(""));
270
Chad Rosierd269bd82012-08-21 16:15:24 +0000271static cl::opt<unsigned>
272SSPBufferSize("stack-protector-buffer-size", cl::init(8),
273 cl::desc("Lower bound for a buffer to be considered for "
274 "stack protection"));
275
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000276// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000277static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +0000278GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000279 std::string IFN = InputFilename;
280 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000281 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000282 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +0000283 IFN[Len-3] == '.' &&
284 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
285 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000286 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000287 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000288 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000289 }
290 return outputFilename;
291}
292
Dan Gohmana2233f22010-09-01 14:20:41 +0000293static tool_output_file *GetOutputStream(const char *TargetName,
294 Triple::OSType OS,
295 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000296 // If we don't yet have an output filename, make one.
297 if (OutputFilename.empty()) {
298 if (InputFilename == "-")
299 OutputFilename = "-";
300 else {
301 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000302
Dan Gohman7ba6f222010-08-18 17:55:15 +0000303 switch (FileType) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000304 case TargetMachine::CGFT_AssemblyFile:
305 if (TargetName[0] == 'c') {
306 if (TargetName[1] == 0)
307 OutputFilename += ".cbe.c";
308 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
309 OutputFilename += ".cpp";
310 else
311 OutputFilename += ".s";
312 } else
313 OutputFilename += ".s";
314 break;
315 case TargetMachine::CGFT_ObjectFile:
316 if (OS == Triple::Win32)
317 OutputFilename += ".obj";
318 else
319 OutputFilename += ".o";
320 break;
321 case TargetMachine::CGFT_Null:
322 OutputFilename += ".null";
323 break;
324 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000325 }
Chris Lattner12e97302006-09-04 04:14:57 +0000326 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000327
Dan Gohman7ba6f222010-08-18 17:55:15 +0000328 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000329 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000330 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000331 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000332 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000333 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000334 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000335 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000336 break;
337 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000338
Dan Gohman7ba6f222010-08-18 17:55:15 +0000339 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000340 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000341 unsigned OpenFlags = 0;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000342 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
Dan Gohman268b0f42010-08-20 01:07:01 +0000343 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
344 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000345 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000346 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000347 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000348 return 0;
349 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000350
Dan Gohmana2233f22010-09-01 14:20:41 +0000351 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000352}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000353
Chris Lattner67e08422003-06-20 15:49:04 +0000354// main - Entry point for the llc compiler.
355//
356int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000357 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000358 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000359
360 // Enable debug stream buffering.
361 EnableDebugBuffering = true;
362
Owen Anderson19251ec2009-07-15 22:16:10 +0000363 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000364 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000365
Daniel Dunbar76628de2009-09-03 05:47:22 +0000366 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000367 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000368 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000369 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000370 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000371
Bob Wilsoncac3b902012-07-02 19:48:45 +0000372 // Initialize codegen and IR passes used by llc so that the -print-after,
373 // -print-before, and -stop-after options work.
374 PassRegistry *Registry = PassRegistry::getPassRegistry();
375 initializeCore(*Registry);
376 initializeCodeGen(*Registry);
377 initializeLoopStrengthReducePass(*Registry);
378 initializeLowerIntrinsicsPass(*Registry);
379 initializeUnreachableBlockElimPass(*Registry);
Rafael Espindolae0eaa042012-06-26 21:33:36 +0000380
Chandler Carruth2d71c422011-07-22 07:50:48 +0000381 // Register the target printer for --version.
382 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
383
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000384 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000385
Chris Lattner6dd290a2007-05-06 04:55:19 +0000386 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000387 SMDiagnostic Err;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000388 std::auto_ptr<Module> M;
Duncan Sands206fc302012-06-27 16:23:48 +0000389 Module *mod = 0;
390 Triple TheTriple;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000391
Duncan Sands206fc302012-06-27 16:23:48 +0000392 bool SkipModule = MCPU == "help" ||
393 (!MAttrs.empty() && MAttrs.front() == "help");
394
395 // If user just wants to list available options, skip module loading
396 if (!SkipModule) {
397 M.reset(ParseIRFile(InputFilename, Err, Context));
398 mod = M.get();
399 if (mod == 0) {
400 Err.print(argv[0], errs());
401 return 1;
402 }
403
404 // If we are supposed to override the target triple, do so now.
405 if (!TargetTriple.empty())
406 mod->setTargetTriple(Triple::normalize(TargetTriple));
407 TheTriple = Triple(mod->getTargetTriple());
408 } else {
409 TheTriple = Triple(Triple::normalize(TargetTriple));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000410 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000411
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000412 if (TheTriple.getTriple().empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000413 TheTriple.setTriple(sys::getDefaultTargetTriple());
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000414
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000415 // Get the target specific parser.
416 std::string Error;
417 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
418 Error);
419 if (!TheTarget) {
420 errs() << argv[0] << ": " << Error;
421 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000422 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000423
424 // Package up features to be passed to target/subtarget
425 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000426 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000427 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000428 for (unsigned i = 0; i != MAttrs.size(); ++i)
429 Features.AddFeature(MAttrs[i]);
430 FeaturesStr = Features.getString();
431 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000432
Evan Chengecb29082011-11-16 08:38:26 +0000433 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
434 switch (OptLevel) {
435 default:
436 errs() << argv[0] << ": invalid optimization level.\n";
437 return 1;
438 case ' ': break;
439 case '0': OLvl = CodeGenOpt::None; break;
440 case '1': OLvl = CodeGenOpt::Less; break;
441 case '2': OLvl = CodeGenOpt::Default; break;
442 case '3': OLvl = CodeGenOpt::Aggressive; break;
443 }
444
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000445 TargetOptions Options;
446 Options.LessPreciseFPMADOption = EnableFPMAD;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000447 Options.NoFramePointerElim = DisableFPElim;
448 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
Lang Hamesb8650f12012-06-22 01:09:09 +0000449 Options.AllowFPOpFusion = FuseFPOps;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000450 Options.UnsafeFPMath = EnableUnsafeFPMath;
451 Options.NoInfsFPMath = EnableNoInfsFPMath;
452 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
453 Options.HonorSignDependentRoundingFPMathOption =
454 EnableHonorSignDependentRoundingFPMath;
455 Options.UseSoftFloat = GenerateSoftFloatCalls;
456 if (FloatABIForCalls != FloatABI::Default)
457 Options.FloatABIType = FloatABIForCalls;
458 Options.NoZerosInBSS = DontPlaceZerosInBSS;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000459 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Nick Lewyckyecc00842012-01-19 00:34:10 +0000460 Options.DisableTailCalls = DisableTailCalls;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000461 Options.StackAlignmentOverride = OverrideStackAlignment;
462 Options.RealignStack = EnableRealignStack;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000463 Options.TrapFuncName = TrapFuncName;
Chandler Carruthede4a8a2012-04-08 17:51:45 +0000464 Options.PositionIndependentExecutable = EnablePIE;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000465 Options.EnableSegmentedStacks = SegmentedStacks;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000466 Options.UseInitArray = UseInitArray;
Chad Rosierd269bd82012-08-21 16:15:24 +0000467 Options.SSPBufferSize = SSPBufferSize;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000468
Andrew Trickb826ae82011-04-05 18:41:31 +0000469 std::auto_ptr<TargetMachine>
Evan Chengefd9b422011-07-20 07:51:56 +0000470 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000471 MCPU, FeaturesStr, Options,
Evan Chengecb29082011-11-16 08:38:26 +0000472 RelocModel, CMModel, OLvl));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000473 assert(target.get() && "Could not allocate target machine!");
Duncan Sands206fc302012-06-27 16:23:48 +0000474 assert(mod && "Should have exited after outputting help!");
Chris Lattner6dd290a2007-05-06 04:55:19 +0000475 TargetMachine &Target = *target.get();
476
Devang Patel85df0cc2010-12-01 15:36:49 +0000477 if (DisableDotLoc)
478 Target.setMCUseLoc(false);
Daniel Dunbared3d5492011-04-19 20:46:13 +0000479
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000480 if (DisableCFI)
481 Target.setMCUseCFI(false);
482
Nick Lewyckyaab61692011-10-31 01:06:02 +0000483 if (EnableDwarfDirectory)
484 Target.setMCUseDwarfDirectory(true);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000485
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000486 if (GenerateSoftFloatCalls)
487 FloatABIForCalls = FloatABI::Soft;
488
Daniel Dunbared3d5492011-04-19 20:46:13 +0000489 // Disable .loc support for older OS X versions.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000490 if (TheTriple.isMacOSX() &&
Daniel Dunbarc7f2f142011-04-20 00:47:19 +0000491 TheTriple.isMacOSXVersionLT(10, 6))
Daniel Dunbared3d5492011-04-19 20:46:13 +0000492 Target.setMCUseLoc(false);
Devang Patel85df0cc2010-12-01 15:36:49 +0000493
Chad Rosier09a06c22012-07-19 00:11:45 +0000494 // Figure out where we are going to send the output.
Dan Gohmana2233f22010-09-01 14:20:41 +0000495 OwningPtr<tool_output_file> Out
Dan Gohman268b0f42010-08-20 01:07:01 +0000496 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
497 if (!Out) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000498
Dan Gohman4cfccb82010-05-11 19:57:55 +0000499 // Build up all of the passes that we want to do to the module.
500 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000501
Bob Wilsonfa594852012-08-03 21:26:18 +0000502 // Add an appropriate TargetLibraryInfo pass for the module's triple.
503 TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple);
Bob Wilson4c65c502012-08-08 20:31:37 +0000504 if (DisableSimplifyLibCalls)
Bob Wilsonfa594852012-08-03 21:26:18 +0000505 TLI->disableAllFunctions();
Bob Wilson4c65c502012-08-08 20:31:37 +0000506 PM.add(TLI);
Bob Wilsonfa594852012-08-03 21:26:18 +0000507
Dan Gohman4cfccb82010-05-11 19:57:55 +0000508 // Add the target data from the target machine, if it exists, or the module.
509 if (const TargetData *TD = Target.getTargetData())
510 PM.add(new TargetData(*TD));
511 else
Duncan Sands206fc302012-06-27 16:23:48 +0000512 PM.add(new TargetData(mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000513
Dan Gohman4cfccb82010-05-11 19:57:55 +0000514 // Override default to generate verbose assembly.
515 Target.setAsmVerbosityDefault(true);
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000516
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000517 if (RelaxAll) {
518 if (FileType != TargetMachine::CGFT_ObjectFile)
519 errs() << argv[0]
520 << ": warning: ignoring -mc-relax-all because filetype != obj";
521 else
522 Target.setMCRelaxAll(true);
523 }
524
Dan Gohmana2233f22010-09-01 14:20:41 +0000525 {
526 formatted_raw_ostream FOS(Out->os());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000527
Bob Wilsoncac3b902012-07-02 19:48:45 +0000528 AnalysisID StartAfterID = 0;
529 AnalysisID StopAfterID = 0;
530 const PassRegistry *PR = PassRegistry::getPassRegistry();
531 if (!StartAfter.empty()) {
532 const PassInfo *PI = PR->getPassInfo(StartAfter);
533 if (!PI) {
534 errs() << argv[0] << ": start-after pass is not registered.\n";
535 return 1;
536 }
537 StartAfterID = PI->getTypeInfo();
538 }
539 if (!StopAfter.empty()) {
540 const PassInfo *PI = PR->getPassInfo(StopAfter);
541 if (!PI) {
542 errs() << argv[0] << ": stop-after pass is not registered.\n";
543 return 1;
544 }
545 StopAfterID = PI->getTypeInfo();
546 }
547
Dan Gohmana2233f22010-09-01 14:20:41 +0000548 // Ask the target to add backend passes as necessary.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000549 if (Target.addPassesToEmitFile(PM, FOS, FileType, NoVerify,
550 StartAfterID, StopAfterID)) {
Dan Gohmana2233f22010-09-01 14:20:41 +0000551 errs() << argv[0] << ": target does not support generation of this"
552 << " file type!\n";
553 return 1;
554 }
555
Andrew Trick12004012011-04-05 18:54:36 +0000556 // Before executing passes, print the final values of the LLVM options.
557 cl::PrintOptionValues();
558
Duncan Sands206fc302012-06-27 16:23:48 +0000559 PM.run(*mod);
Dan Gohmana2233f22010-09-01 14:20:41 +0000560 }
Dan Gohman4cfccb82010-05-11 19:57:55 +0000561
Dan Gohman268b0f42010-08-20 01:07:01 +0000562 // Declare success.
563 Out->keep();
Chris Lattner6dd290a2007-05-06 04:55:19 +0000564
565 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000566}