blob: aa652234731a94a4c4de5cf920223140a7dbe9c4 [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"
Evan Cheng8264e272011-06-29 01:14:12 +000024#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/Pass.h"
26#include "llvm/PassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
David Greene6e55be62010-01-05 01:30:21 +000028#include "llvm/Support/Debug.h"
David Greenea31f96c2009-07-14 20:18:05 +000029#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Support/Host.h"
31#include "llvm/Support/IRReader.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"
Evan Cheng2bb40352011-08-24 18:08:43 +000036#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Support/ToolOutputFile.h"
Bob Wilsonfa594852012-08-03 21:26:18 +000039#include "llvm/Target/TargetLibraryInfo.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000040#include "llvm/Target/TargetMachine.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000041#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000042using namespace llvm;
43
Vikram S. Adveeb818692002-09-16 16:35:34 +000044// General options for llc. Other pass-specific options are specified
45// within the corresponding llc passes, and target-specific options
46// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000047//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000048static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000049InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000050
Chris Lattnerd64b2de2003-04-25 05:26:11 +000051static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000052OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
53
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000054static cl::opt<unsigned>
55TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
56 cl::value_desc("N"),
57 cl::desc("Repeat compilation N times for timing"));
58
Evan Cheng09bd0b12009-05-04 23:05:19 +000059// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000060static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000061OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000062 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
63 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000064 cl::Prefix,
65 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000066 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000067
Chris Lattnere568b882005-12-16 04:59:57 +000068static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000069TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000070
Reid Spencer8e3830d2005-07-28 02:25:30 +000071cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +000072 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +000073
Nadav Rotem5dc203e2012-10-18 23:22:48 +000074cl::opt<bool>
Bob Wilsonfa594852012-08-03 21:26:18 +000075DisableSimplifyLibCalls("disable-simplify-libcalls",
Nadav Rotem5dc203e2012-10-18 23:22:48 +000076 cl::desc("Disable simplify-libcalls"),
77 cl::init(false));
Chad Rosierd269bd82012-08-21 16:15:24 +000078
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +000079static int compileModule(char**, LLVMContext&);
80
Chris Lattner06fcc4c2005-06-25 03:32:05 +000081// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +000082static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +000083GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000084 std::string IFN = InputFilename;
85 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000086 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +000087 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +000088 IFN[Len-3] == '.' &&
89 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
90 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000091 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +000092 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000093 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000094 }
95 return outputFilename;
96}
97
Dan Gohmana2233f22010-09-01 14:20:41 +000098static tool_output_file *GetOutputStream(const char *TargetName,
99 Triple::OSType OS,
100 const char *ProgName) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000101 // If we don't yet have an output filename, make one.
102 if (OutputFilename.empty()) {
103 if (InputFilename == "-")
104 OutputFilename = "-";
105 else {
106 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner12e97302006-09-04 04:14:57 +0000107
Dan Gohman7ba6f222010-08-18 17:55:15 +0000108 switch (FileType) {
Dan Gohman7ba6f222010-08-18 17:55:15 +0000109 case TargetMachine::CGFT_AssemblyFile:
110 if (TargetName[0] == 'c') {
111 if (TargetName[1] == 0)
112 OutputFilename += ".cbe.c";
113 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
114 OutputFilename += ".cpp";
115 else
116 OutputFilename += ".s";
117 } else
118 OutputFilename += ".s";
119 break;
120 case TargetMachine::CGFT_ObjectFile:
121 if (OS == Triple::Win32)
122 OutputFilename += ".obj";
123 else
124 OutputFilename += ".o";
125 break;
126 case TargetMachine::CGFT_Null:
127 OutputFilename += ".null";
128 break;
129 }
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000130 }
Chris Lattner12e97302006-09-04 04:14:57 +0000131 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000132
Dan Gohman7ba6f222010-08-18 17:55:15 +0000133 // Decide if we need "binary" output.
Daniel Dunbared90e702008-11-13 05:01:07 +0000134 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000135 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000136 case TargetMachine::CGFT_AssemblyFile:
Chris Lattner12e97302006-09-04 04:14:57 +0000137 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000138 case TargetMachine::CGFT_ObjectFile:
Chris Lattneredcf0652010-02-03 05:55:08 +0000139 case TargetMachine::CGFT_Null:
Daniel Dunbared90e702008-11-13 05:01:07 +0000140 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000141 break;
142 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000143
Dan Gohman7ba6f222010-08-18 17:55:15 +0000144 // Open the file.
Owen Anderson93719642008-08-21 00:14:44 +0000145 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000146 unsigned OpenFlags = 0;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000147 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
Dan Gohman268b0f42010-08-20 01:07:01 +0000148 tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
149 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000150 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000151 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000152 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000153 return 0;
154 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000155
Dan Gohmana2233f22010-09-01 14:20:41 +0000156 return FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000157}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000158
Chris Lattner67e08422003-06-20 15:49:04 +0000159// main - Entry point for the llc compiler.
160//
161int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000162 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000163 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000164
165 // Enable debug stream buffering.
166 EnableDebugBuffering = true;
167
Owen Anderson19251ec2009-07-15 22:16:10 +0000168 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000169 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000170
Daniel Dunbar76628de2009-09-03 05:47:22 +0000171 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000172 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000173 InitializeAllTargetMCs();
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000174 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000175 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000176
Bob Wilsoncac3b902012-07-02 19:48:45 +0000177 // Initialize codegen and IR passes used by llc so that the -print-after,
178 // -print-before, and -stop-after options work.
179 PassRegistry *Registry = PassRegistry::getPassRegistry();
180 initializeCore(*Registry);
181 initializeCodeGen(*Registry);
182 initializeLoopStrengthReducePass(*Registry);
183 initializeLowerIntrinsicsPass(*Registry);
184 initializeUnreachableBlockElimPass(*Registry);
Rafael Espindolae0eaa042012-06-26 21:33:36 +0000185
Chandler Carruth2d71c422011-07-22 07:50:48 +0000186 // Register the target printer for --version.
187 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
188
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000189 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Andrew Trickb826ae82011-04-05 18:41:31 +0000190
Jakob Stoklund Olesen2776b4c2012-11-30 21:42:47 +0000191 // Compile the module TimeCompilations times to give better compile time
192 // metrics.
193 for (unsigned I = TimeCompilations; I; --I)
194 if (int RetVal = compileModule(argv, Context))
195 return RetVal;
196 return 0;
197}
198
199static int compileModule(char **argv, LLVMContext &Context) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000200 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000201 SMDiagnostic Err;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000202 std::auto_ptr<Module> M;
Duncan Sands206fc302012-06-27 16:23:48 +0000203 Module *mod = 0;
204 Triple TheTriple;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000205
Duncan Sands206fc302012-06-27 16:23:48 +0000206 bool SkipModule = MCPU == "help" ||
207 (!MAttrs.empty() && MAttrs.front() == "help");
208
209 // If user just wants to list available options, skip module loading
210 if (!SkipModule) {
211 M.reset(ParseIRFile(InputFilename, Err, Context));
212 mod = M.get();
213 if (mod == 0) {
214 Err.print(argv[0], errs());
215 return 1;
216 }
217
218 // If we are supposed to override the target triple, do so now.
219 if (!TargetTriple.empty())
220 mod->setTargetTriple(Triple::normalize(TargetTriple));
221 TheTriple = Triple(mod->getTargetTriple());
222 } else {
223 TheTriple = Triple(Triple::normalize(TargetTriple));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000224 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000225
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000226 if (TheTriple.getTriple().empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000227 TheTriple.setTriple(sys::getDefaultTargetTriple());
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000228
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000229 // Get the target specific parser.
230 std::string Error;
231 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
232 Error);
233 if (!TheTarget) {
234 errs() << argv[0] << ": " << Error;
235 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000236 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000237
238 // Package up features to be passed to target/subtarget
239 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +0000240 if (MAttrs.size()) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000241 SubtargetFeatures Features;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000242 for (unsigned i = 0; i != MAttrs.size(); ++i)
243 Features.AddFeature(MAttrs[i]);
244 FeaturesStr = Features.getString();
245 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000246
Evan Chengecb29082011-11-16 08:38:26 +0000247 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
248 switch (OptLevel) {
249 default:
250 errs() << argv[0] << ": invalid optimization level.\n";
251 return 1;
252 case ' ': break;
253 case '0': OLvl = CodeGenOpt::None; break;
254 case '1': OLvl = CodeGenOpt::Less; break;
255 case '2': OLvl = CodeGenOpt::Default; break;
256 case '3': OLvl = CodeGenOpt::Aggressive; break;
257 }
258
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000259 TargetOptions Options;
260 Options.LessPreciseFPMADOption = EnableFPMAD;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000261 Options.NoFramePointerElim = DisableFPElim;
262 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
Lang Hamesb8650f12012-06-22 01:09:09 +0000263 Options.AllowFPOpFusion = FuseFPOps;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000264 Options.UnsafeFPMath = EnableUnsafeFPMath;
265 Options.NoInfsFPMath = EnableNoInfsFPMath;
266 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
267 Options.HonorSignDependentRoundingFPMathOption =
268 EnableHonorSignDependentRoundingFPMath;
269 Options.UseSoftFloat = GenerateSoftFloatCalls;
270 if (FloatABIForCalls != FloatABI::Default)
271 Options.FloatABIType = FloatABIForCalls;
272 Options.NoZerosInBSS = DontPlaceZerosInBSS;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000273 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Nick Lewyckyecc00842012-01-19 00:34:10 +0000274 Options.DisableTailCalls = DisableTailCalls;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000275 Options.StackAlignmentOverride = OverrideStackAlignment;
276 Options.RealignStack = EnableRealignStack;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000277 Options.TrapFuncName = TrapFuncName;
Chandler Carruthede4a8a2012-04-08 17:51:45 +0000278 Options.PositionIndependentExecutable = EnablePIE;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000279 Options.EnableSegmentedStacks = SegmentedStacks;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000280 Options.UseInitArray = UseInitArray;
Chad Rosierd269bd82012-08-21 16:15:24 +0000281 Options.SSPBufferSize = SSPBufferSize;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000282
Andrew Trickb826ae82011-04-05 18:41:31 +0000283 std::auto_ptr<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}