blob: d4416e60ef59739dd6335e7ff9f59bc5186de5b4 [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"
21#include "llvm/Analysis/Verifier.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"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000025#include "llvm/Config/config.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"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000032#include "llvm/System/Host.h"
Chris Lattner278f5152004-05-27 05:41:36 +000033#include "llvm/System/Signals.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000034#include "llvm/Target/SubtargetFeature.h"
35#include "llvm/Target/TargetData.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetRegistry.h"
Chris Lattner5dcc4f62009-06-17 16:42:19 +000038#include "llvm/Target/TargetSelect.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000039#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000040using namespace llvm;
41
Vikram S. Adveeb818692002-09-16 16:35:34 +000042// General options for llc. Other pass-specific options are specified
43// within the corresponding llc passes, and target-specific options
44// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000045//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000046static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000047InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000048
Chris Lattnerd64b2de2003-04-25 05:26:11 +000049static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000050OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
51
Evan Cheng09bd0b12009-05-04 23:05:19 +000052// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000053static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000054OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000055 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
56 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000057 cl::Prefix,
58 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000059 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000060
Chris Lattnere568b882005-12-16 04:59:57 +000061static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000062TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000063
Daniel Dunbard3706452009-07-16 02:23:53 +000064static cl::opt<std::string>
65MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000066
Jim Laskey19058c32005-09-01 21:38:21 +000067static cl::opt<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000068MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000069 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000070 cl::value_desc("cpu-name"),
71 cl::init(""));
72
73static cl::list<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000074MAttrs("mattr",
Jim Laskey19058c32005-09-01 21:38:21 +000075 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000076 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000077 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000078
Michael J. Spencerf695f8f2010-07-31 19:57:02 +000079static cl::opt<bool>
80RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
81
Chris Lattner06fcc4c2005-06-25 03:32:05 +000082cl::opt<TargetMachine::CodeGenFileType>
Chris Lattnerf0cb12a2010-02-02 21:06:45 +000083FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000084 cl::desc("Choose a file type (not all types are supported by all targets):"),
85 cl::values(
Chris Lattnerf0cb12a2010-02-02 21:06:45 +000086 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000087 "Emit an assembly ('.s') file"),
Chris Lattnerf0cb12a2010-02-02 21:06:45 +000088 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000089 "Emit a native object ('.o') file [experimental]"),
Chris Lattneredcf0652010-02-03 05:55:08 +000090 clEnumValN(TargetMachine::CGFT_Null, "null",
91 "Emit nothing, for performance testing"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000092 clEnumValEnd));
93
Reid Spencer8e3830d2005-07-28 02:25:30 +000094cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +000095 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +000096
Chris Lattner06fcc4c2005-06-25 03:32:05 +000097
Devang Patel72a4d2f2009-06-04 22:05:33 +000098static cl::opt<bool>
99DisableRedZone("disable-red-zone",
100 cl::desc("Do not emit code that uses the red zone."),
101 cl::init(false));
102
Devang Pateld1c7d342009-06-05 21:57:13 +0000103static cl::opt<bool>
104NoImplicitFloats("no-implicit-float",
105 cl::desc("Don't generate implicit floating point instructions (x86-only)"),
106 cl::init(false));
107
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000108// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000109static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +0000110GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000111 std::string IFN = InputFilename;
112 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000113 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000114 if ((Len > 2) &&
Dan Gohmane8d01502009-09-16 19:18:41 +0000115 IFN[Len-3] == '.' &&
116 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
117 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000118 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000119 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000120 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000121 }
122 return outputFilename;
123}
124
Chris Lattnerb903a152010-05-06 00:54:20 +0000125static formatted_raw_ostream *GetOutputStream(const char *TargetName,
126 Triple::OSType OS,
Daniel Dunbare8338102009-07-15 20:24:03 +0000127 const char *ProgName) {
Chris Lattner12e97302006-09-04 04:14:57 +0000128 if (OutputFilename != "") {
129 if (OutputFilename == "-")
Dan Gohman174f57662010-05-27 19:47:36 +0000130 return new formatted_raw_ostream(outs(),
131 formatted_raw_ostream::PRESERVE_STREAM);
Chris Lattner12e97302006-09-04 04:14:57 +0000132
Chris Lattner12e97302006-09-04 04:14:57 +0000133 // Make sure that the Out file gets unlinked from the disk if we get a
134 // SIGINT
135 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
136
Owen Anderson93719642008-08-21 00:14:44 +0000137 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000138 raw_fd_ostream *FDOut =
139 new raw_fd_ostream(OutputFilename.c_str(), error,
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000140 raw_fd_ostream::F_Binary);
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000141 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000142 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000143 delete FDOut;
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000144 return 0;
145 }
Dan Gohman607818a2009-07-15 17:29:42 +0000146 formatted_raw_ostream *Out =
147 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000148
149 return Out;
Chris Lattner12e97302006-09-04 04:14:57 +0000150 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000151
Chris Lattner12e97302006-09-04 04:14:57 +0000152 if (InputFilename == "-") {
153 OutputFilename = "-";
Dan Gohman174f57662010-05-27 19:47:36 +0000154 return new formatted_raw_ostream(outs(),
155 formatted_raw_ostream::PRESERVE_STREAM);
Chris Lattner12e97302006-09-04 04:14:57 +0000156 }
157
158 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000159
Daniel Dunbared90e702008-11-13 05:01:07 +0000160 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000161 switch (FileType) {
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000162 default: assert(0 && "Unknown file type");
163 case TargetMachine::CGFT_AssemblyFile:
Daniel Dunbare8338102009-07-15 20:24:03 +0000164 if (TargetName[0] == 'c') {
165 if (TargetName[1] == 0)
Anton Korobeynikov78695032008-04-23 22:29:24 +0000166 OutputFilename += ".cbe.c";
Daniel Dunbare8338102009-07-15 20:24:03 +0000167 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
Anton Korobeynikov78695032008-04-23 22:29:24 +0000168 OutputFilename += ".cpp";
169 else
170 OutputFilename += ".s";
171 } else
Chris Lattner12e97302006-09-04 04:14:57 +0000172 OutputFilename += ".s";
Chris Lattner12e97302006-09-04 04:14:57 +0000173 break;
Chris Lattnerf0cb12a2010-02-02 21:06:45 +0000174 case TargetMachine::CGFT_ObjectFile:
Chris Lattnerb903a152010-05-06 00:54:20 +0000175 if (OS == Triple::Win32)
176 OutputFilename += ".obj";
177 else
178 OutputFilename += ".o";
Daniel Dunbared90e702008-11-13 05:01:07 +0000179 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000180 break;
Chris Lattneredcf0652010-02-03 05:55:08 +0000181 case TargetMachine::CGFT_Null:
182 OutputFilename += ".null";
Daniel Dunbared90e702008-11-13 05:01:07 +0000183 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000184 break;
185 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000186
Chris Lattner12e97302006-09-04 04:14:57 +0000187 // Make sure that the Out file gets unlinked from the disk if we get a
188 // SIGINT
189 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000190
Owen Anderson93719642008-08-21 00:14:44 +0000191 std::string error;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000192 unsigned OpenFlags = 0;
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000193 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
194 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), error,
195 OpenFlags);
Owen Anderson93719642008-08-21 00:14:44 +0000196 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000197 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000198 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000199 return 0;
200 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000201
Dan Gohman607818a2009-07-15 17:29:42 +0000202 formatted_raw_ostream *Out =
203 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
204
Chris Lattner12e97302006-09-04 04:14:57 +0000205 return Out;
206}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000207
Chris Lattner67e08422003-06-20 15:49:04 +0000208// main - Entry point for the llc compiler.
209//
210int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000211 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000212 PrettyStackTraceProgram X(argc, argv);
David Greene6e55be62010-01-05 01:30:21 +0000213
214 // Enable debug stream buffering.
215 EnableDebugBuffering = true;
216
Owen Anderson19251ec2009-07-15 22:16:10 +0000217 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000218 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000219
Daniel Dunbar76628de2009-09-03 05:47:22 +0000220 // Initialize targets first, so that --version shows registered targets.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000221 InitializeAllTargets();
222 InitializeAllAsmPrinters();
Chris Lattner8900ef12010-04-05 23:11:24 +0000223 InitializeAllAsmParsers();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000224
225 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000226
Chris Lattner6dd290a2007-05-06 04:55:19 +0000227 // Load the module to be compiled...
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000228 SMDiagnostic Err;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000229 std::auto_ptr<Module> M;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000230
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000231 M.reset(ParseIRFile(InputFilename, Err, Context));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000232 if (M.get() == 0) {
Dan Gohmanc76bfb72009-09-02 19:35:19 +0000233 Err.Print(argv[0], errs());
Chris Lattner6dd290a2007-05-06 04:55:19 +0000234 return 1;
235 }
236 Module &mod = *M.get();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000237
Chris Lattner6dd290a2007-05-06 04:55:19 +0000238 // If we are supposed to override the target triple, do so now.
239 if (!TargetTriple.empty())
240 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000241
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000242 Triple TheTriple(mod.getTargetTriple());
243 if (TheTriple.getTriple().empty())
244 TheTriple.setTriple(sys::getHostTriple());
245
246 // Allocate target machine. First, check whether the user has explicitly
247 // specified an architecture to compile for. If so we have to look it up by
248 // name, because it might be a backend that has no mapping to a target triple.
Daniel Dunbard3706452009-07-16 02:23:53 +0000249 const Target *TheTarget = 0;
250 if (!MArch.empty()) {
251 for (TargetRegistry::iterator it = TargetRegistry::begin(),
252 ie = TargetRegistry::end(); it != ie; ++it) {
253 if (MArch == it->getName()) {
254 TheTarget = &*it;
255 break;
256 }
257 }
258
259 if (!TheTarget) {
260 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
261 return 1;
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000262 }
263
264 // Adjust the triple to match (if known), otherwise stick with the
265 // module/host triple.
266 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
267 if (Type != Triple::UnknownArch)
268 TheTriple.setArch(Type);
Daniel Dunbare8338102009-07-15 20:24:03 +0000269 } else {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000270 std::string Err;
Daniel Dunbar719d2352009-08-03 04:20:57 +0000271 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err);
Daniel Dunbare8338102009-07-15 20:24:03 +0000272 if (TheTarget == 0) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000273 errs() << argv[0] << ": error auto-selecting target for module '"
274 << Err << "'. Please use the -march option to explicitly "
275 << "pick a target.\n";
Chris Lattnerb1492402003-08-24 14:02:14 +0000276 return 1;
Chris Lattner6142ca82004-07-11 04:03:24 +0000277 }
Chris Lattner5667f0e2002-10-29 21:12:46 +0000278 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000279
280 // Package up features to be passed to target/subtarget
281 std::string FeaturesStr;
282 if (MCPU.size() || MAttrs.size()) {
283 SubtargetFeatures Features;
284 Features.setCPU(MCPU);
285 for (unsigned i = 0; i != MAttrs.size(); ++i)
286 Features.AddFeature(MAttrs[i]);
287 FeaturesStr = Features.getString();
288 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000289
Daniel Dunbare8338102009-07-15 20:24:03 +0000290 std::auto_ptr<TargetMachine>
Daniel Dunbarad9a6c42009-08-04 04:08:40 +0000291 target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000292 assert(target.get() && "Could not allocate target machine!");
293 TargetMachine &Target = *target.get();
294
295 // Figure out where we are going to send the output...
Chris Lattnerb903a152010-05-06 00:54:20 +0000296 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(),
297 TheTriple.getOS(), argv[0]);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000298 if (Out == 0) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000299
Evan Cheng09bd0b12009-05-04 23:05:19 +0000300 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling026e5d72009-04-29 23:29:43 +0000301 switch (OptLevel) {
302 default:
Dan Gohmand8db3762009-07-15 16:35:29 +0000303 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendlingdad99192009-04-29 23:46:43 +0000304 return 1;
Bill Wendling026e5d72009-04-29 23:29:43 +0000305 case ' ': break;
306 case '0': OLvl = CodeGenOpt::None; break;
Evan Cheng5a286382009-10-16 21:02:20 +0000307 case '1': OLvl = CodeGenOpt::Less; break;
Bill Wendlingaeaf5a52009-04-30 00:57:51 +0000308 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000309 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000310 }
311
Dan Gohman5341a862010-03-01 21:45:21 +0000312 // Request that addPassesToEmitFile run the Verifier after running
313 // passes which modify the IR.
314#ifndef NDEBUG
315 bool DisableVerify = false;
316#else
317 bool DisableVerify = true;
318#endif
319
Dan Gohman4cfccb82010-05-11 19:57:55 +0000320 // Build up all of the passes that we want to do to the module.
321 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000322
Dan Gohman4cfccb82010-05-11 19:57:55 +0000323 // Add the target data from the target machine, if it exists, or the module.
324 if (const TargetData *TD = Target.getTargetData())
325 PM.add(new TargetData(*TD));
326 else
327 PM.add(new TargetData(&mod));
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000328
Dan Gohman4cfccb82010-05-11 19:57:55 +0000329 if (!NoVerify)
330 PM.add(createVerifierPass());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +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 Gohman4cfccb82010-05-11 19:57:55 +0000343 // Ask the target to add backend passes as necessary.
344 if (Target.addPassesToEmitFile(PM, *Out, FileType, OLvl,
345 DisableVerify)) {
346 errs() << argv[0] << ": target does not support generation of this"
347 << " file type!\n";
Dan Gohman174f57662010-05-27 19:47:36 +0000348 delete Out;
Dan Gohman4cfccb82010-05-11 19:57:55 +0000349 // And the Out file is empty and useless, so remove it now.
350 sys::Path(OutputFilename).eraseFromDisk();
351 return 1;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000352 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000353
Dan Gohman4cfccb82010-05-11 19:57:55 +0000354 PM.run(mod);
355
Dan Gohman174f57662010-05-27 19:47:36 +0000356 // Delete the ostream.
357 delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000358
359 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000360}