blob: 1dce9d7b60112c639c704074d02d2909bbcdec27 [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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/LLVMContext.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000017#include "llvm/ADT/Triple.h"
Bob Wilsoncac3b902012-07-02 19:48:45 +000018#include "llvm/Assembly/PrintModulePass.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000019#include "llvm/CodeGen/CommandFlags.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000020#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
21#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000024#include "llvm/IRReader/IRReader.h"
Evan Cheng8264e272011-06-29 01:14:12 +000025#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/Pass.h"
27#include "llvm/PassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
David Greene6e55be62010-01-05 01:30:21 +000029#include "llvm/Support/Debug.h"
David Greenea31f96c2009-07-14 20:18:05 +000030#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Support/Host.h"
Chris Lattner76d46322006-12-06 01:18:01 +000032#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000033#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000034#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000035#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000036#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Support/ToolOutputFile.h"
Bob Wilsonfa594852012-08-03 21:26:18 +000040#include "llvm/Target/TargetLibraryInfo.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000041#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000042#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000043using namespace llvm;
44
Vikram S. Adveeb818692002-09-16 16:35:34 +000045// General options for llc. Other pass-specific options are specified
46// within the corresponding llc passes, and target-specific options
47// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000048//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000049static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000050InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000051
Chris Lattnerd64b2de2003-04-25 05:26:11 +000052static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000053OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
54
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000055static cl::opt<unsigned>
56TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
57 cl::value_desc("N"),
58 cl::desc("Repeat compilation N times for timing"));
59
Evan Cheng09bd0b12009-05-04 23:05:19 +000060// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000061static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000062OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000063 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
64 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000065 cl::Prefix,
66 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000067 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000068
Chris Lattnere568b882005-12-16 04:59:57 +000069static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000070TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000071
Reid Spencer8e3830d2005-07-28 02:25:30 +000072cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +000073 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +000074
Nadav Rotem5dc203e2012-10-18 23:22:48 +000075cl::opt<bool>
Bob Wilsonfa594852012-08-03 21:26:18 +000076DisableSimplifyLibCalls("disable-simplify-libcalls",
Nadav Rotem5dc203e2012-10-18 23:22:48 +000077 cl::desc("Disable simplify-libcalls"),
78 cl::init(false));
Chad Rosierd269bd82012-08-21 16:15:24 +000079
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000080static int compileModule(char**, LLVMContext&);
81
Chris Lattner06fcc4c2005-06-25 03:32:05 +000082// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +000083static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +000084GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000085 std::string IFN = InputFilename;
86 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000087 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +000088 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +000089 IFN[Len-3] == '.' &&
90 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
91 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000092 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +000093 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000094 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000095 }
96 return outputFilename;
97}
98
Dan Gohmana2233f22010-09-01 14:20:41 +000099static tool_output_file *GetOutputStream(const char *TargetName,
100 Triple::OSType OS,
101 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000102 // If we don't yet have an output filename, make one.
103 if (OutputFilename.empty()) {
104 if (InputFilename == "-")
105 OutputFilename = "-";
106 else {
107 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000108
Dan Gohman7ba6f222010-08-18 17:55:15 +0000109 switch (FileType) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000110 case TargetMachine::CGFT_AssemblyFile:
111 if (TargetName[0] == 'c') {
112 if (TargetName[1] == 0)
113 OutputFilename += ".cbe.c";
114 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
115 OutputFilename += ".cpp";
116 else
117 OutputFilename += ".s";
118 } else
119 OutputFilename += ".s";
120 break;
121 case TargetMachine::CGFT_ObjectFile:
122 if (OS == Triple::Win32)
123 OutputFilename += ".obj";
124 else
125 OutputFilename += ".o";
126 break;
127 case TargetMachine::CGFT_Null:
128 OutputFilename += ".null";
129 break;
130 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000131 }
Chris Lattner12e97302006-09-04 04:14:57 +0000132 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000133
Dan Gohman7ba6f222010-08-18 17:55:15 +0000134 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000135 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000136 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000137 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000138 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000139 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000140 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000141 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000142 break;
143 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000144
Dan Gohman7ba6f222010-08-18 17:55:15 +0000145 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000146 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000147 unsigned OpenFlags = 0;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000148 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
Dan Gohman268b0f42010-08-20 01:07:01 +0000149 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
150 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000151 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000152 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000153 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000154 return 0;
155 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000156
Dan Gohmana2233f22010-09-01 14:20:41 +0000157 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000158}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000159
Chris Lattner67e08422003-06-20 15:49:04 +0000160// main - Entry point for the llc compiler.
161//
162int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000163 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000164 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000165
166 // Enable debug stream buffering.
167 EnableDebugBuffering = true;
168
Owen Anderson19251ec2009-07-15 22:16:10 +0000169 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000170 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000171
Daniel Dunbar76628de2009-09-03 05:47:22 +0000172 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000173 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000174 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000175 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000176 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000177
Bob Wilsoncac3b902012-07-02 19:48:45 +0000178 // Initialize codegen and IR passes used by llc so that the -print-after,
179 // -print-before, and -stop-after options work.
180 PassRegistry *Registry = PassRegistry::getPassRegistry();
181 initializeCore(*Registry);
182 initializeCodeGen(*Registry);
183 initializeLoopStrengthReducePass(*Registry);
184 initializeLowerIntrinsicsPass(*Registry);
185 initializeUnreachableBlockElimPass(*Registry);
Rafael Espindolae0eaa042012-06-26 21:33:36 +0000186
Chandler Carruth2d71c422011-07-22 07:50:48 +0000187 // Register the target printer for --version.
188 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
189
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000190 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000191
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +0000192 // Compile the module TimeCompilations times to give better compile time
193 // metrics.
194 for (unsigned I = TimeCompilations; I; --I)
195 if (int RetVal = compileModule(argv, Context))
196 return RetVal;
197 return 0;
198}
199
200static int compileModule(char **argv, LLVMContext &Context) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000201 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000202 SMDiagnostic Err;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000203 std::auto_ptr<Module> M;
Duncan Sands206fc302012-06-27 16:23:48 +0000204 Module *mod = 0;
205 Triple TheTriple;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000206
Duncan Sands206fc302012-06-27 16:23:48 +0000207 bool SkipModule = MCPU == "help" ||
208 (!MAttrs.empty() && MAttrs.front() == "help");
209
210 // If user just wants to list available options, skip module loading
211 if (!SkipModule) {
212 M.reset(ParseIRFile(InputFilename, Err, Context));
213 mod = M.get();
214 if (mod == 0) {
215 Err.print(argv[0], errs());
216 return 1;
217 }
218
219 // If we are supposed to override the target triple, do so now.
220 if (!TargetTriple.empty())
221 mod->setTargetTriple(Triple::normalize(TargetTriple));
222 TheTriple = Triple(mod->getTargetTriple());
223 } else {
224 TheTriple = Triple(Triple::normalize(TargetTriple));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000225 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000226
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000227 if (TheTriple.getTriple().empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000228 TheTriple.setTriple(sys::getDefaultTargetTriple());
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000229
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000230 // Get the target specific parser.
231 std::string Error;
232 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
233 Error);
234 if (!TheTarget) {
235 errs() << argv[0] << ": " << Error;
236 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000237 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000238
239 // Package up features to be passed to target/subtarget
240 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000241 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000242 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000243 for (unsigned i = 0; i != MAttrs.size(); ++i)
244 Features.AddFeature(MAttrs[i]);
245 FeaturesStr = Features.getString();
246 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000247
Evan Chengecb29082011-11-16 08:38:26 +0000248 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
249 switch (OptLevel) {
250 default:
251 errs() << argv[0] << ": invalid optimization level.\n";
252 return 1;
253 case ' ': break;
254 case '0': OLvl = CodeGenOpt::None; break;
255 case '1': OLvl = CodeGenOpt::Less; break;
256 case '2': OLvl = CodeGenOpt::Default; break;
257 case '3': OLvl = CodeGenOpt::Aggressive; break;
258 }
259
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000260 TargetOptions Options;
261 Options.LessPreciseFPMADOption = EnableFPMAD;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000262 Options.NoFramePointerElim = DisableFPElim;
263 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
Lang Hamesb8650f12012-06-22 01:09:09 +0000264 Options.AllowFPOpFusion = FuseFPOps;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000265 Options.UnsafeFPMath = EnableUnsafeFPMath;
266 Options.NoInfsFPMath = EnableNoInfsFPMath;
267 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
268 Options.HonorSignDependentRoundingFPMathOption =
269 EnableHonorSignDependentRoundingFPMath;
270 Options.UseSoftFloat = GenerateSoftFloatCalls;
271 if (FloatABIForCalls != FloatABI::Default)
272 Options.FloatABIType = FloatABIForCalls;
273 Options.NoZerosInBSS = DontPlaceZerosInBSS;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000274 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Nick Lewyckyecc00842012-01-19 00:34:10 +0000275 Options.DisableTailCalls = DisableTailCalls;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000276 Options.StackAlignmentOverride = OverrideStackAlignment;
277 Options.RealignStack = EnableRealignStack;
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;
Chad Rosierd269bd82012-08-21 16:15:24 +0000282 Options.SSPBufferSize = SSPBufferSize;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000283
Andrew Trickb826ae82011-04-05 18:41:31 +0000284 std::auto_ptr<TargetMachine>
Evan Chengefd9b422011-07-20 07:51:56 +0000285 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000286 MCPU, FeaturesStr, Options,
Evan Chengecb29082011-11-16 08:38:26 +0000287 RelocModel, CMModel, OLvl));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000288 assert(target.get() && "Could not allocate target machine!");
Duncan Sands206fc302012-06-27 16:23:48 +0000289 assert(mod && "Should have exited after outputting help!");
Chris Lattner6dd290a2007-05-06 04:55:19 +0000290 TargetMachine &Target = *target.get();
291
Devang Patel85df0cc2010-12-01 15:36:49 +0000292 if (DisableDotLoc)
293 Target.setMCUseLoc(false);
Daniel Dunbared3d5492011-04-19 20:46:13 +0000294
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000295 if (DisableCFI)
296 Target.setMCUseCFI(false);
297
Nick Lewyckyaab61692011-10-31 01:06:02 +0000298 if (EnableDwarfDirectory)
299 Target.setMCUseDwarfDirectory(true);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000300
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000301 if (GenerateSoftFloatCalls)
302 FloatABIForCalls = FloatABI::Soft;
303
Daniel Dunbared3d5492011-04-19 20:46:13 +0000304 // Disable .loc support for older OS X versions.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000305 if (TheTriple.isMacOSX() &&
Daniel Dunbarc7f2f142011-04-20 00:47:19 +0000306 TheTriple.isMacOSXVersionLT(10, 6))
Daniel Dunbared3d5492011-04-19 20:46:13 +0000307 Target.setMCUseLoc(false);
Devang Patel85df0cc2010-12-01 15:36:49 +0000308
Chad Rosier09a06c22012-07-19 00:11:45 +0000309 // Figure out where we are going to send the output.
Dan Gohmana2233f22010-09-01 14:20:41 +0000310 OwningPtr<tool_output_file> Out
Dan Gohman268b0f42010-08-20 01:07:01 +0000311 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
312 if (!Out) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000313
Dan Gohman4cfccb82010-05-11 19:57:55 +0000314 // Build up all of the passes that we want to do to the module.
315 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000316
Bob Wilsonfa594852012-08-03 21:26:18 +0000317 // Add an appropriate TargetLibraryInfo pass for the module's triple.
318 TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple);
Bob Wilson4c65c502012-08-08 20:31:37 +0000319 if (DisableSimplifyLibCalls)
Bob Wilsonfa594852012-08-03 21:26:18 +0000320 TLI->disableAllFunctions();
Bob Wilson4c65c502012-08-08 20:31:37 +0000321 PM.add(TLI);
Bob Wilsonfa594852012-08-03 21:26:18 +0000322
Chandler Carruth664e3542013-01-07 01:37:14 +0000323 // Add intenal analysis passes from the target machine.
324 Target.addAnalysisPasses(PM);
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000325
Dan Gohman4cfccb82010-05-11 19:57:55 +0000326 // Add the target data from the target machine, if it exists, or the module.
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000327 if (const DataLayout *TD = Target.getDataLayout())
328 PM.add(new DataLayout(*TD));
Dan Gohman4cfccb82010-05-11 19:57:55 +0000329 else
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000330 PM.add(new DataLayout(mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000331
Dan Gohman4cfccb82010-05-11 19:57:55 +0000332 // Override default to generate verbose assembly.
333 Target.setAsmVerbosityDefault(true);
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000334
Michael J. Spencerf695f8f2010-07-31 19:57:02 +0000335 if (RelaxAll) {
336 if (FileType != TargetMachine::CGFT_ObjectFile)
337 errs() << argv[0]
338 << ": warning: ignoring -mc-relax-all because filetype != obj";
339 else
340 Target.setMCRelaxAll(true);
341 }
342
Dan Gohmana2233f22010-09-01 14:20:41 +0000343 {
344 formatted_raw_ostream FOS(Out->os());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000345
Bob Wilsoncac3b902012-07-02 19:48:45 +0000346 AnalysisID StartAfterID = 0;
347 AnalysisID StopAfterID = 0;
348 const PassRegistry *PR = PassRegistry::getPassRegistry();
349 if (!StartAfter.empty()) {
350 const PassInfo *PI = PR->getPassInfo(StartAfter);
351 if (!PI) {
352 errs() << argv[0] << ": start-after pass is not registered.\n";
353 return 1;
354 }
355 StartAfterID = PI->getTypeInfo();
356 }
357 if (!StopAfter.empty()) {
358 const PassInfo *PI = PR->getPassInfo(StopAfter);
359 if (!PI) {
360 errs() << argv[0] << ": stop-after pass is not registered.\n";
361 return 1;
362 }
363 StopAfterID = PI->getTypeInfo();
364 }
365
Dan Gohmana2233f22010-09-01 14:20:41 +0000366 // Ask the target to add backend passes as necessary.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000367 if (Target.addPassesToEmitFile(PM, FOS, FileType, NoVerify,
368 StartAfterID, StopAfterID)) {
Dan Gohmana2233f22010-09-01 14:20:41 +0000369 errs() << argv[0] << ": target does not support generation of this"
370 << " file type!\n";
371 return 1;
372 }
373
Andrew Trick12004012011-04-05 18:54:36 +0000374 // Before executing passes, print the final values of the LLVM options.
375 cl::PrintOptionValues();
376
Duncan Sands206fc302012-06-27 16:23:48 +0000377 PM.run(*mod);
Dan Gohmana2233f22010-09-01 14:20:41 +0000378 }
Dan Gohman4cfccb82010-05-11 19:57:55 +0000379
Dan Gohman268b0f42010-08-20 01:07:01 +0000380 // Declare success.
381 Out->keep();
Chris Lattner6dd290a2007-05-06 04:55:19 +0000382
383 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000384}