blob: 6373d20811df0846851a702e62dd7fc4c80625f4 [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
Wan Xiaofei16577a52013-06-19 02:26:00 +000016
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000017#include "llvm/ADT/Triple.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000018#include "llvm/CodeGen/CommandFlags.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000019#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
20#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000022#include "llvm/IR/IRPrintingPasses.h"
23#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000025#include "llvm/IRReader/IRReader.h"
Evan Cheng8264e272011-06-29 01:14:12 +000026#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/Pass.h"
28#include "llvm/PassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
David Greene6e55be62010-01-05 01:30:21 +000030#include "llvm/Support/Debug.h"
David Greenea31f96c2009-07-14 20:18:05 +000031#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000032#include "llvm/Support/Host.h"
Chris Lattner76d46322006-12-06 01:18:01 +000033#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000034#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000035#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000036#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000037#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Support/ToolOutputFile.h"
Bob Wilsonfa594852012-08-03 21:26:18 +000041#include "llvm/Target/TargetLibraryInfo.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000042#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000043#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000044using namespace llvm;
45
Vikram S. Adveeb818692002-09-16 16:35:34 +000046// General options for llc. Other pass-specific options are specified
47// within the corresponding llc passes, and target-specific options
48// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000049//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000050static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000051InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000052
Chris Lattnerd64b2de2003-04-25 05:26:11 +000053static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000054OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
55
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000056static cl::opt<unsigned>
57TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
58 cl::value_desc("N"),
59 cl::desc("Repeat compilation N times for timing"));
60
Evan Cheng09bd0b12009-05-04 23:05:19 +000061// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000062static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000063OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000064 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
65 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000066 cl::Prefix,
67 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000068 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000069
Chris Lattnere568b882005-12-16 04:59:57 +000070static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000071TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000072
Reid Spencer8e3830d2005-07-28 02:25:30 +000073cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +000074 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +000075
Nadav Rotem5dc203e2012-10-18 23:22:48 +000076cl::opt<bool>
Bob Wilsonfa594852012-08-03 21:26:18 +000077DisableSimplifyLibCalls("disable-simplify-libcalls",
Nadav Rotem5dc203e2012-10-18 23:22:48 +000078 cl::desc("Disable simplify-libcalls"),
79 cl::init(false));
Chad Rosierd269bd82012-08-21 16:15:24 +000080
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000081static int compileModule(char**, LLVMContext&);
82
Chris Lattner06fcc4c2005-06-25 03:32:05 +000083// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +000084static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +000085GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000086 std::string IFN = InputFilename;
87 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000088 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +000089 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +000090 IFN[Len-3] == '.' &&
91 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
92 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000093 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +000094 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000095 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000096 }
97 return outputFilename;
98}
99
Dan Gohmana2233f22010-09-01 14:20:41 +0000100static tool_output_file *GetOutputStream(const char *TargetName,
101 Triple::OSType OS,
102 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000103 // If we don't yet have an output filename, make one.
104 if (OutputFilename.empty()) {
105 if (InputFilename == "-")
106 OutputFilename = "-";
107 else {
108 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000109
Dan Gohman7ba6f222010-08-18 17:55:15 +0000110 switch (FileType) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000111 case TargetMachine::CGFT_AssemblyFile:
112 if (TargetName[0] == 'c') {
113 if (TargetName[1] == 0)
114 OutputFilename += ".cbe.c";
115 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
116 OutputFilename += ".cpp";
117 else
118 OutputFilename += ".s";
119 } else
120 OutputFilename += ".s";
121 break;
122 case TargetMachine::CGFT_ObjectFile:
123 if (OS == Triple::Win32)
124 OutputFilename += ".obj";
125 else
126 OutputFilename += ".o";
127 break;
128 case TargetMachine::CGFT_Null:
129 OutputFilename += ".null";
130 break;
131 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000132 }
Chris Lattner12e97302006-09-04 04:14:57 +0000133 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000134
Dan Gohman7ba6f222010-08-18 17:55:15 +0000135 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000136 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000137 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000138 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000139 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000140 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000141 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000142 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000143 break;
144 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000145
Dan Gohman7ba6f222010-08-18 17:55:15 +0000146 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000147 std::string error;
Rafael Espindola6d354812013-07-16 19:44:17 +0000148 sys::fs::OpenFlags OpenFlags = sys::fs::F_None;
149 if (Binary)
150 OpenFlags |= sys::fs::F_Binary;
Dan Gohman268b0f42010-08-20 01:07:01 +0000151 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
152 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000153 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000154 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000155 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000156 return 0;
157 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000158
Dan Gohmana2233f22010-09-01 14:20:41 +0000159 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000160}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000161
Chris Lattner67e08422003-06-20 15:49:04 +0000162// main - Entry point for the llc compiler.
163//
164int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000165 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000166 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000167
168 // Enable debug stream buffering.
169 EnableDebugBuffering = true;
170
Owen Anderson19251ec2009-07-15 22:16:10 +0000171 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000172 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000173
Daniel Dunbar76628de2009-09-03 05:47:22 +0000174 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000175 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000176 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000177 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000178 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000179
Bob Wilsoncac3b902012-07-02 19:48:45 +0000180 // Initialize codegen and IR passes used by llc so that the -print-after,
181 // -print-before, and -stop-after options work.
182 PassRegistry *Registry = PassRegistry::getPassRegistry();
183 initializeCore(*Registry);
184 initializeCodeGen(*Registry);
185 initializeLoopStrengthReducePass(*Registry);
186 initializeLowerIntrinsicsPass(*Registry);
187 initializeUnreachableBlockElimPass(*Registry);
Rafael Espindolae0eaa042012-06-26 21:33:36 +0000188
Chandler Carruth2d71c422011-07-22 07:50:48 +0000189 // Register the target printer for --version.
190 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
191
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000192 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000193
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +0000194 // Compile the module TimeCompilations times to give better compile time
195 // metrics.
196 for (unsigned I = TimeCompilations; I; --I)
197 if (int RetVal = compileModule(argv, Context))
198 return RetVal;
199 return 0;
200}
201
202static int compileModule(char **argv, LLVMContext &Context) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000203 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000204 SMDiagnostic Err;
Andy Gibbs95777552013-04-12 10:56:28 +0000205 OwningPtr<Module> M;
Duncan Sands206fc302012-06-27 16:23:48 +0000206 Module *mod = 0;
207 Triple TheTriple;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000208
Duncan Sands206fc302012-06-27 16:23:48 +0000209 bool SkipModule = MCPU == "help" ||
210 (!MAttrs.empty() && MAttrs.front() == "help");
211
212 // If user just wants to list available options, skip module loading
213 if (!SkipModule) {
214 M.reset(ParseIRFile(InputFilename, Err, Context));
215 mod = M.get();
216 if (mod == 0) {
217 Err.print(argv[0], errs());
218 return 1;
219 }
220
221 // If we are supposed to override the target triple, do so now.
222 if (!TargetTriple.empty())
223 mod->setTargetTriple(Triple::normalize(TargetTriple));
224 TheTriple = Triple(mod->getTargetTriple());
225 } else {
226 TheTriple = Triple(Triple::normalize(TargetTriple));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000227 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000228
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000229 if (TheTriple.getTriple().empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000230 TheTriple.setTriple(sys::getDefaultTargetTriple());
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000231
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000232 // Get the target specific parser.
233 std::string Error;
234 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
235 Error);
236 if (!TheTarget) {
237 errs() << argv[0] << ": " << Error;
238 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000239 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000240
241 // Package up features to be passed to target/subtarget
242 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000243 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000244 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000245 for (unsigned i = 0; i != MAttrs.size(); ++i)
246 Features.AddFeature(MAttrs[i]);
247 FeaturesStr = Features.getString();
248 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000249
Evan Chengecb29082011-11-16 08:38:26 +0000250 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
251 switch (OptLevel) {
252 default:
253 errs() << argv[0] << ": invalid optimization level.\n";
254 return 1;
255 case ' ': break;
256 case '0': OLvl = CodeGenOpt::None; break;
257 case '1': OLvl = CodeGenOpt::Less; break;
258 case '2': OLvl = CodeGenOpt::Default; break;
259 case '3': OLvl = CodeGenOpt::Aggressive; break;
260 }
261
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000262 TargetOptions Options;
263 Options.LessPreciseFPMADOption = EnableFPMAD;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000264 Options.NoFramePointerElim = DisableFPElim;
Lang Hamesb8650f12012-06-22 01:09:09 +0000265 Options.AllowFPOpFusion = FuseFPOps;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000266 Options.UnsafeFPMath = EnableUnsafeFPMath;
267 Options.NoInfsFPMath = EnableNoInfsFPMath;
268 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
269 Options.HonorSignDependentRoundingFPMathOption =
270 EnableHonorSignDependentRoundingFPMath;
271 Options.UseSoftFloat = GenerateSoftFloatCalls;
272 if (FloatABIForCalls != FloatABI::Default)
273 Options.FloatABIType = FloatABIForCalls;
274 Options.NoZerosInBSS = DontPlaceZerosInBSS;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000275 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Nick Lewyckyecc00842012-01-19 00:34:10 +0000276 Options.DisableTailCalls = DisableTailCalls;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000277 Options.StackAlignmentOverride = OverrideStackAlignment;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000278 Options.TrapFuncName = TrapFuncName;
Chandler Carruthede4a8a2012-04-08 17:51:45 +0000279 Options.PositionIndependentExecutable = EnablePIE;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000280 Options.EnableSegmentedStacks = SegmentedStacks;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000281 Options.UseInitArray = UseInitArray;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000282
Andy Gibbs95777552013-04-12 10:56:28 +0000283 OwningPtr<TargetMachine>
Evan Chengefd9b422011-07-20 07:51:56 +0000284 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000285 MCPU, FeaturesStr, Options,
Evan Chengecb29082011-11-16 08:38:26 +0000286 RelocModel, CMModel, OLvl));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000287 assert(target.get() && "Could not allocate target machine!");
Duncan Sands206fc302012-06-27 16:23:48 +0000288 assert(mod && "Should have exited after outputting help!");
Chris Lattner6dd290a2007-05-06 04:55:19 +0000289 TargetMachine &Target = *target.get();
290
Devang Patel85df0cc2010-12-01 15:36:49 +0000291 if (DisableDotLoc)
292 Target.setMCUseLoc(false);
Daniel Dunbared3d5492011-04-19 20:46:13 +0000293
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000294 if (DisableCFI)
295 Target.setMCUseCFI(false);
296
Nick Lewyckyaab61692011-10-31 01:06:02 +0000297 if (EnableDwarfDirectory)
298 Target.setMCUseDwarfDirectory(true);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000299
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000300 if (GenerateSoftFloatCalls)
301 FloatABIForCalls = FloatABI::Soft;
302
Daniel Dunbared3d5492011-04-19 20:46:13 +0000303 // Disable .loc support for older OS X versions.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000304 if (TheTriple.isMacOSX() &&
Daniel Dunbarc7f2f142011-04-20 00:47:19 +0000305 TheTriple.isMacOSXVersionLT(10, 6))
Daniel Dunbared3d5492011-04-19 20:46:13 +0000306 Target.setMCUseLoc(false);
Devang Patel85df0cc2010-12-01 15:36:49 +0000307
Chad Rosier09a06c22012-07-19 00:11:45 +0000308 // Figure out where we are going to send the output.
Dan Gohmana2233f22010-09-01 14:20:41 +0000309 OwningPtr<tool_output_file> Out
Dan Gohman268b0f42010-08-20 01:07:01 +0000310 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
311 if (!Out) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000312
Dan Gohman4cfccb82010-05-11 19:57:55 +0000313 // Build up all of the passes that we want to do to the module.
314 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000315
Bob Wilsonfa594852012-08-03 21:26:18 +0000316 // Add an appropriate TargetLibraryInfo pass for the module's triple.
317 TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple);
Bob Wilson4c65c502012-08-08 20:31:37 +0000318 if (DisableSimplifyLibCalls)
Bob Wilsonfa594852012-08-03 21:26:18 +0000319 TLI->disableAllFunctions();
Bob Wilson4c65c502012-08-08 20:31:37 +0000320 PM.add(TLI);
Bob Wilsonfa594852012-08-03 21:26:18 +0000321
Chandler Carruth664e3542013-01-07 01:37:14 +0000322 // Add intenal analysis passes from the target machine.
323 Target.addAnalysisPasses(PM);
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000324
Dan Gohman4cfccb82010-05-11 19:57:55 +0000325 // Add the target data from the target machine, if it exists, or the module.
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000326 if (const DataLayout *TD = Target.getDataLayout())
327 PM.add(new DataLayout(*TD));
Dan Gohman4cfccb82010-05-11 19:57:55 +0000328 else
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000329 PM.add(new DataLayout(mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000330
Dan Gohman4cfccb82010-05-11 19:57:55 +0000331 // Override default to generate verbose assembly.
332 Target.setAsmVerbosityDefault(true);
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000333
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000334 if (RelaxAll) {
335 if (FileType != TargetMachine::CGFT_ObjectFile)
336 errs() << argv[0]
337 << ": warning: ignoring -mc-relax-all because filetype != obj";
338 else
339 Target.setMCRelaxAll(true);
340 }
341
Dan Gohmana2233f22010-09-01 14:20:41 +0000342 {
343 formatted_raw_ostream FOS(Out->os());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000344
Bob Wilsoncac3b902012-07-02 19:48:45 +0000345 AnalysisID StartAfterID = 0;
346 AnalysisID StopAfterID = 0;
347 const PassRegistry *PR = PassRegistry::getPassRegistry();
348 if (!StartAfter.empty()) {
349 const PassInfo *PI = PR->getPassInfo(StartAfter);
350 if (!PI) {
351 errs() << argv[0] << ": start-after pass is not registered.\n";
352 return 1;
353 }
354 StartAfterID = PI->getTypeInfo();
355 }
356 if (!StopAfter.empty()) {
357 const PassInfo *PI = PR->getPassInfo(StopAfter);
358 if (!PI) {
359 errs() << argv[0] << ": stop-after pass is not registered.\n";
360 return 1;
361 }
362 StopAfterID = PI->getTypeInfo();
363 }
364
Dan Gohmana2233f22010-09-01 14:20:41 +0000365 // Ask the target to add backend passes as necessary.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000366 if (Target.addPassesToEmitFile(PM, FOS, FileType, NoVerify,
367 StartAfterID, StopAfterID)) {
Dan Gohmana2233f22010-09-01 14:20:41 +0000368 errs() << argv[0] << ": target does not support generation of this"
369 << " file type!\n";
370 return 1;
371 }
372
Andrew Trick12004012011-04-05 18:54:36 +0000373 // Before executing passes, print the final values of the LLVM options.
374 cl::PrintOptionValues();
375
Duncan Sands206fc302012-06-27 16:23:48 +0000376 PM.run(*mod);
Dan Gohmana2233f22010-09-01 14:20:41 +0000377 }
Dan Gohman4cfccb82010-05-11 19:57:55 +0000378
Dan Gohman268b0f42010-08-20 01:07:01 +0000379 // Declare success.
380 Out->keep();
Chris Lattner6dd290a2007-05-06 04:55:19 +0000381
382 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000383}