blob: 09ff4613b9720bc526dd5a7d98408f2fc8e665c5 [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"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000031#include "llvm/Support/FileSystem.h"
David Greenea31f96c2009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000033#include "llvm/Support/Host.h"
Chris Lattner76d46322006-12-06 01:18:01 +000034#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000035#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000036#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000037#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000038#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/ToolOutputFile.h"
Bob Wilsonfa594852012-08-03 21:26:18 +000042#include "llvm/Target/TargetLibraryInfo.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000043#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000044#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000045using namespace llvm;
46
Vikram S. Adveeb818692002-09-16 16:35:34 +000047// General options for llc. Other pass-specific options are specified
48// within the corresponding llc passes, and target-specific options
49// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000050//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000051static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000052InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000053
Chris Lattnerd64b2de2003-04-25 05:26:11 +000054static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000055OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
56
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000057static cl::opt<unsigned>
58TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
59 cl::value_desc("N"),
60 cl::desc("Repeat compilation N times for timing"));
61
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000062static cl::opt<bool>
63NoIntegratedAssembler("no-integrated-as", cl::Hidden,
64 cl::desc("Disable integrated assembler"));
65
Evan Cheng09bd0b12009-05-04 23:05:19 +000066// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000067static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000068OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000069 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
70 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000071 cl::Prefix,
72 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000073 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000074
Chris Lattnere568b882005-12-16 04:59:57 +000075static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000076TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000077
Eric Christopher473ec182014-05-21 21:05:05 +000078static cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
79 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +000080
Eric Christopher473ec182014-05-21 21:05:05 +000081static cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls",
82 cl::desc("Disable simplify-libcalls"));
Chad Rosierd269bd82012-08-21 16:15:24 +000083
Eric Christopher472cee32014-05-21 21:05:09 +000084static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
85 cl::desc("Show encoding in .s output"));
86
87static cl::opt<bool> EnableDwarfDirectory(
88 "enable-dwarf-directory", cl::Hidden,
89 cl::desc("Use .file directives with an explicit directory."));
90
91static cl::opt<bool> AsmVerbose("asm-verbose",
92 cl::desc("Add comments to directives."),
93 cl::init(true));
94
95static int compileModule(char **, LLVMContext &);
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000096
Chris Lattner06fcc4c2005-06-25 03:32:05 +000097// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +000098static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +000099GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000100 std::string IFN = InputFilename;
101 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000102 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000103 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +0000104 IFN[Len-3] == '.' &&
105 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
106 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000107 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000108 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000109 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000110 }
111 return outputFilename;
112}
113
Dan Gohmana2233f22010-09-01 14:20:41 +0000114static tool_output_file *GetOutputStream(const char *TargetName,
115 Triple::OSType OS,
116 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000117 // If we don't yet have an output filename, make one.
118 if (OutputFilename.empty()) {
119 if (InputFilename == "-")
120 OutputFilename = "-";
121 else {
122 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000123
Dan Gohman7ba6f222010-08-18 17:55:15 +0000124 switch (FileType) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000125 case TargetMachine::CGFT_AssemblyFile:
126 if (TargetName[0] == 'c') {
127 if (TargetName[1] == 0)
128 OutputFilename += ".cbe.c";
129 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
130 OutputFilename += ".cpp";
131 else
132 OutputFilename += ".s";
133 } else
134 OutputFilename += ".s";
135 break;
136 case TargetMachine::CGFT_ObjectFile:
137 if (OS == Triple::Win32)
138 OutputFilename += ".obj";
139 else
140 OutputFilename += ".o";
141 break;
142 case TargetMachine::CGFT_Null:
143 OutputFilename += ".null";
144 break;
145 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000146 }
Chris Lattner12e97302006-09-04 04:14:57 +0000147 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000148
Dan Gohman7ba6f222010-08-18 17:55:15 +0000149 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000150 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000151 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000152 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000153 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000154 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000155 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000156 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000157 break;
158 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000159
Dan Gohman7ba6f222010-08-18 17:55:15 +0000160 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000161 std::string error;
Rafael Espindola6d354812013-07-16 19:44:17 +0000162 sys::fs::OpenFlags OpenFlags = sys::fs::F_None;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000163 if (!Binary)
164 OpenFlags |= sys::fs::F_Text;
Dan Gohman268b0f42010-08-20 01:07:01 +0000165 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
166 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000167 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000168 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000169 delete FDOut;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000170 return nullptr;
Chris Lattner12e97302006-09-04 04:14:57 +0000171 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000172
Dan Gohmana2233f22010-09-01 14:20:41 +0000173 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000174}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000175
Chris Lattner67e08422003-06-20 15:49:04 +0000176// main - Entry point for the llc compiler.
177//
178int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000179 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000180 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000181
182 // Enable debug stream buffering.
183 EnableDebugBuffering = true;
184
Owen Anderson19251ec2009-07-15 22:16:10 +0000185 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000186 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000187
Daniel Dunbar76628de2009-09-03 05:47:22 +0000188 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000189 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000190 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000191 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000192 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000193
Bob Wilsoncac3b902012-07-02 19:48:45 +0000194 // Initialize codegen and IR passes used by llc so that the -print-after,
195 // -print-before, and -stop-after options work.
196 PassRegistry *Registry = PassRegistry::getPassRegistry();
197 initializeCore(*Registry);
198 initializeCodeGen(*Registry);
199 initializeLoopStrengthReducePass(*Registry);
200 initializeLowerIntrinsicsPass(*Registry);
201 initializeUnreachableBlockElimPass(*Registry);
Rafael Espindolae0eaa042012-06-26 21:33:36 +0000202
Chandler Carruth2d71c422011-07-22 07:50:48 +0000203 // Register the target printer for --version.
204 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
205
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000206 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000207
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +0000208 // Compile the module TimeCompilations times to give better compile time
209 // metrics.
210 for (unsigned I = TimeCompilations; I; --I)
211 if (int RetVal = compileModule(argv, Context))
212 return RetVal;
213 return 0;
214}
215
216static int compileModule(char **argv, LLVMContext &Context) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000217 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000218 SMDiagnostic Err;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000219 std::unique_ptr<Module> M;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000220 Module *mod = nullptr;
Duncan Sands206fc302012-06-27 16:23:48 +0000221 Triple TheTriple;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000222
Duncan Sands206fc302012-06-27 16:23:48 +0000223 bool SkipModule = MCPU == "help" ||
224 (!MAttrs.empty() && MAttrs.front() == "help");
225
Jim Grosbachdad17722014-04-12 01:34:31 +0000226 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
227 // this will set the CPU to an empty string which tells the target to
228 // pick a basic default.
229 if (MCPU == "native")
230 MCPU = sys::getHostCPUName();
231
Duncan Sands206fc302012-06-27 16:23:48 +0000232 // If user just wants to list available options, skip module loading
233 if (!SkipModule) {
234 M.reset(ParseIRFile(InputFilename, Err, Context));
235 mod = M.get();
Craig Toppere6cb63e2014-04-25 04:24:47 +0000236 if (mod == nullptr) {
Duncan Sands206fc302012-06-27 16:23:48 +0000237 Err.print(argv[0], errs());
238 return 1;
239 }
240
241 // If we are supposed to override the target triple, do so now.
242 if (!TargetTriple.empty())
243 mod->setTargetTriple(Triple::normalize(TargetTriple));
244 TheTriple = Triple(mod->getTargetTriple());
245 } else {
246 TheTriple = Triple(Triple::normalize(TargetTriple));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000247 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000248
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000249 if (TheTriple.getTriple().empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000250 TheTriple.setTriple(sys::getDefaultTargetTriple());
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000251
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000252 // Get the target specific parser.
253 std::string Error;
254 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
255 Error);
256 if (!TheTarget) {
257 errs() << argv[0] << ": " << Error;
258 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000259 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000260
261 // Package up features to be passed to target/subtarget
262 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000263 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000264 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000265 for (unsigned i = 0; i != MAttrs.size(); ++i)
266 Features.AddFeature(MAttrs[i]);
267 FeaturesStr = Features.getString();
268 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000269
Evan Chengecb29082011-11-16 08:38:26 +0000270 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
271 switch (OptLevel) {
272 default:
273 errs() << argv[0] << ": invalid optimization level.\n";
274 return 1;
275 case ' ': break;
276 case '0': OLvl = CodeGenOpt::None; break;
277 case '1': OLvl = CodeGenOpt::Less; break;
278 case '2': OLvl = CodeGenOpt::Default; break;
279 case '3': OLvl = CodeGenOpt::Aggressive; break;
280 }
281
Eli Benderskyf0f21002014-02-19 17:09:35 +0000282 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola48fa6ed2014-02-21 03:13:54 +0000283 Options.DisableIntegratedAS = NoIntegratedAssembler;
Eric Christopher472cee32014-05-21 21:05:09 +0000284 Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
285 Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory;
286 Options.MCOptions.AsmVerbose = AsmVerbose;
Eric Christophereb719722014-05-20 23:59:50 +0000287
Ahmed Charles56440fd2014-03-06 05:51:42 +0000288 std::unique_ptr<TargetMachine> target(
289 TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
290 Options, RelocModel, CMModel, OLvl));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000291 assert(target.get() && "Could not allocate target machine!");
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000292
293 // If we don't have a module then just exit now. We do this down
294 // here since the CPU/Feature help is underneath the target machine
295 // creation.
296 if (SkipModule)
297 return 0;
298
299 assert(mod && "Should have exited if we didn't have a module!");
Chris Lattner6dd290a2007-05-06 04:55:19 +0000300 TargetMachine &Target = *target.get();
301
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000302 if (GenerateSoftFloatCalls)
303 FloatABIForCalls = FloatABI::Soft;
304
Chad Rosier09a06c22012-07-19 00:11:45 +0000305 // Figure out where we are going to send the output.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000306 std::unique_ptr<tool_output_file> Out(
307 GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
Dan Gohman268b0f42010-08-20 01:07:01 +0000308 if (!Out) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000309
Dan Gohman4cfccb82010-05-11 19:57:55 +0000310 // Build up all of the passes that we want to do to the module.
311 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000312
Bob Wilsonfa594852012-08-03 21:26:18 +0000313 // Add an appropriate TargetLibraryInfo pass for the module's triple.
314 TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple);
Bob Wilson4c65c502012-08-08 20:31:37 +0000315 if (DisableSimplifyLibCalls)
Bob Wilsonfa594852012-08-03 21:26:18 +0000316 TLI->disableAllFunctions();
Bob Wilson4c65c502012-08-08 20:31:37 +0000317 PM.add(TLI);
Bob Wilsonfa594852012-08-03 21:26:18 +0000318
Dan Gohman4cfccb82010-05-11 19:57:55 +0000319 // Add the target data from the target machine, if it exists, or the module.
Rafael Espindolaf1939022014-02-21 02:01:42 +0000320 if (const DataLayout *DL = Target.getDataLayout())
Rafael Espindola339430f2014-02-25 23:25:17 +0000321 mod->setDataLayout(DL);
322 PM.add(new DataLayoutPass(mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000323
Eric Christophere6ece1a2014-05-15 01:10:50 +0000324 if (RelaxAll.getNumOccurrences() > 0 &&
325 FileType != TargetMachine::CGFT_ObjectFile)
326 errs() << argv[0]
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000327 << ": warning: ignoring -mc-relax-all because filetype != obj";
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000328
Dan Gohmana2233f22010-09-01 14:20:41 +0000329 {
330 formatted_raw_ostream FOS(Out->os());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000331
Craig Toppere6cb63e2014-04-25 04:24:47 +0000332 AnalysisID StartAfterID = nullptr;
333 AnalysisID StopAfterID = nullptr;
Bob Wilsoncac3b902012-07-02 19:48:45 +0000334 const PassRegistry *PR = PassRegistry::getPassRegistry();
335 if (!StartAfter.empty()) {
336 const PassInfo *PI = PR->getPassInfo(StartAfter);
337 if (!PI) {
338 errs() << argv[0] << ": start-after pass is not registered.\n";
339 return 1;
340 }
341 StartAfterID = PI->getTypeInfo();
342 }
343 if (!StopAfter.empty()) {
344 const PassInfo *PI = PR->getPassInfo(StopAfter);
345 if (!PI) {
346 errs() << argv[0] << ": stop-after pass is not registered.\n";
347 return 1;
348 }
349 StopAfterID = PI->getTypeInfo();
350 }
351
Dan Gohmana2233f22010-09-01 14:20:41 +0000352 // Ask the target to add backend passes as necessary.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000353 if (Target.addPassesToEmitFile(PM, FOS, FileType, NoVerify,
354 StartAfterID, StopAfterID)) {
Dan Gohmana2233f22010-09-01 14:20:41 +0000355 errs() << argv[0] << ": target does not support generation of this"
356 << " file type!\n";
357 return 1;
358 }
359
Andrew Trick12004012011-04-05 18:54:36 +0000360 // Before executing passes, print the final values of the LLVM options.
361 cl::PrintOptionValues();
362
Duncan Sands206fc302012-06-27 16:23:48 +0000363 PM.run(*mod);
Dan Gohmana2233f22010-09-01 14:20:41 +0000364 }
Dan Gohman4cfccb82010-05-11 19:57:55 +0000365
Dan Gohman268b0f42010-08-20 01:07:01 +0000366 // Declare success.
367 Out->keep();
Chris Lattner6dd290a2007-05-06 04:55:19 +0000368
369 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000370}