blob: 84e6867badee22c2e373c3ef68bc12d5aaaa3e4d [file] [log] [blame]
Chris Lattner5b836c42003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere737c7a2001-09-07 22:20:50 +00009//
Brian Gaekee40eae72004-03-16 21:47:20 +000010// This is the llc code generator driver. It provides a convenient
Misha Brukman3da94ae2005-04-22 00:00:37 +000011// command-line interface for generating native assembly-language code
Gabor Greifa99be512007-07-05 17:07:56 +000012// or C code, given LLVM bitcode.
Chris Lattnere737c7a2001-09-07 22:20:50 +000013//
Chris Lattnerb79757c2001-10-04 01:40:53 +000014//===----------------------------------------------------------------------===//
Vikram S. Advecb465fc2001-07-21 12:42:29 +000015
Owen Anderson8b477ed2009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000017#include "llvm/Module.h"
Chris Lattner744879e2007-05-06 09:32:02 +000018#include "llvm/ModuleProvider.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000019#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000020#include "llvm/Pass.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000021#include "llvm/ADT/Triple.h"
22#include "llvm/Analysis/Verifier.h"
Dan Gohman778b06b2009-09-02 19:35:19 +000023#include "llvm/Support/IRReader.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000024#include "llvm/CodeGen/FileWriters.h"
25#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
26#include "llvm/CodeGen/LinkAllCodegenComponents.h"
27#include "llvm/CodeGen/ObjectCodeEmitter.h"
28#include "llvm/Config/config.h"
29#include "llvm/LinkAllVMCore.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000031#include "llvm/Support/FileUtilities.h"
David Greene71847812009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000033#include "llvm/Support/ManagedStatic.h"
Chris Lattner1a735402007-05-06 04:55:19 +000034#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000036#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000037#include "llvm/System/Host.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000038#include "llvm/System/Signals.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000039#include "llvm/Target/SubtargetFeature.h"
40#include "llvm/Target/TargetData.h"
41#include "llvm/Target/TargetMachine.h"
42#include "llvm/Target/TargetRegistry.h"
Chris Lattner2deb58f2009-06-17 16:42:19 +000043#include "llvm/Target/TargetSelect.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000044#include "llvm/Transforms/Scalar.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000045#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000046using namespace llvm;
47
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000048// General options for llc. Other pass-specific options are specified
49// within the corresponding llc passes, and target-specific options
50// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000051//
Chris Lattnerb5881f12003-04-25 05:26:11 +000052static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000053InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000054
Chris Lattnerb5881f12003-04-25 05:26:11 +000055static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000056OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
57
Dan Gohmanbaa26392009-08-25 15:34:52 +000058static cl::opt<bool>
59Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000060
Evan Cheng712e80e2009-05-04 23:05:19 +000061// Determine optimization level.
Bill Wendling98a366d2009-04-29 23:29:43 +000062static cl::opt<char>
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000063OptLevel("O",
Evan Cheng712e80e2009-05-04 23:05:19 +000064 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
65 "(default = '-O2')"),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000066 cl::Prefix,
67 cl::ZeroOrMore,
Bill Wendling98a366d2009-04-29 23:29:43 +000068 cl::init(' '));
Chris Lattner178e0c42005-11-08 02:12:17 +000069
Chris Lattnerf33b8662005-12-16 04:59:57 +000070static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000071TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000072
Daniel Dunbar1d929212009-07-16 02:23:53 +000073static cl::opt<std::string>
74MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000075
Jim Laskeyb1e11802005-09-01 21:38:21 +000076static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000077MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000078 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000079 cl::value_desc("cpu-name"),
80 cl::init(""));
81
82static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000083MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000084 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000085 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000086 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000087
Chris Lattner812125a2005-06-25 03:32:05 +000088cl::opt<TargetMachine::CodeGenFileType>
89FileType("filetype", cl::init(TargetMachine::AssemblyFile),
90 cl::desc("Choose a file type (not all types are supported by all targets):"),
91 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000092 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000093 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000094 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000095 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000096 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000097 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000098 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000099 clEnumValEnd));
100
Reid Spencer4418c2b2005-07-28 02:25:30 +0000101cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000102 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +0000103
Chris Lattner812125a2005-06-25 03:32:05 +0000104
Devang Pateld18e31a2009-06-04 22:05:33 +0000105static cl::opt<bool>
106DisableRedZone("disable-red-zone",
107 cl::desc("Do not emit code that uses the red zone."),
108 cl::init(false));
109
Devang Patel578efa92009-06-05 21:57:13 +0000110static cl::opt<bool>
111NoImplicitFloats("no-implicit-float",
112 cl::desc("Don't generate implicit floating point instructions (x86-only)"),
113 cl::init(false));
114
Chris Lattner812125a2005-06-25 03:32:05 +0000115// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +0000116static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +0000117GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000118 std::string IFN = InputFilename;
119 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000120 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000121 if ((Len > 2) &&
Dan Gohmand81c4502009-09-16 19:18:41 +0000122 IFN[Len-3] == '.' &&
123 ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') ||
124 (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000125 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000126 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000127 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000128 }
129 return outputFilename;
130}
131
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000132static formatted_raw_ostream *GetOutputStream(const char *TargetName,
133 const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000134 if (OutputFilename != "") {
135 if (OutputFilename == "-")
David Greene71847812009-07-14 20:18:05 +0000136 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000137
Chris Lattner1911fd42006-09-04 04:14:57 +0000138 // Make sure that the Out file gets unlinked from the disk if we get a
139 // SIGINT
140 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
141
Owen Andersoncb371882008-08-21 00:14:44 +0000142 std::string error;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000143 raw_fd_ostream *FDOut =
144 new raw_fd_ostream(OutputFilename.c_str(), error,
Chris Lattner17e9edc2009-08-23 02:51:22 +0000145 raw_fd_ostream::F_Binary);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000146 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000147 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000148 delete FDOut;
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000149 return 0;
150 }
Dan Gohmana1bdced2009-07-15 17:29:42 +0000151 formatted_raw_ostream *Out =
152 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000153
154 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000155 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156
Chris Lattner1911fd42006-09-04 04:14:57 +0000157 if (InputFilename == "-") {
158 OutputFilename = "-";
David Greene71847812009-07-14 20:18:05 +0000159 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000160 }
161
162 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000163
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000164 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000165 switch (FileType) {
166 case TargetMachine::AssemblyFile:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000167 if (TargetName[0] == 'c') {
168 if (TargetName[1] == 0)
Anton Korobeynikov50276522008-04-23 22:29:24 +0000169 OutputFilename += ".cbe.c";
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000170 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
Anton Korobeynikov50276522008-04-23 22:29:24 +0000171 OutputFilename += ".cpp";
172 else
173 OutputFilename += ".s";
174 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000175 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000176 break;
177 case TargetMachine::ObjectFile:
178 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000179 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000180 break;
181 case TargetMachine::DynamicLibrary:
182 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000183 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000184 break;
185 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000186
Chris Lattner1911fd42006-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 Glushenkov5c1799b2009-01-16 06:53:46 +0000190
Owen Andersoncb371882008-08-21 00:14:44 +0000191 std::string error;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000192 unsigned OpenFlags = 0;
Chris Lattner17e9edc2009-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 Andersoncb371882008-08-21 00:14:44 +0000196 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000197 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000198 delete FDOut;
Chris Lattner1911fd42006-09-04 04:14:57 +0000199 return 0;
200 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000201
Dan Gohmana1bdced2009-07-15 17:29:42 +0000202 formatted_raw_ostream *Out =
203 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
204
Chris Lattner1911fd42006-09-04 04:14:57 +0000205 return Out;
206}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000207
Chris Lattner5b836c42003-06-20 15:49:04 +0000208// main - Entry point for the llc compiler.
209//
210int main(int argc, char **argv) {
Chris Lattner1a735402007-05-06 04:55:19 +0000211 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000212 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000213 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000214 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner364d1202004-02-19 20:32:39 +0000215
Daniel Dunbar2b991bb2009-09-03 05:47:22 +0000216 // Initialize targets first, so that --version shows registered targets.
Chris Lattner2deb58f2009-06-17 16:42:19 +0000217 InitializeAllTargets();
218 InitializeAllAsmPrinters();
Daniel Dunbar494d6632009-07-16 02:04:54 +0000219
220 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner2deb58f2009-06-17 16:42:19 +0000221
Chris Lattner1a735402007-05-06 04:55:19 +0000222 // Load the module to be compiled...
Dan Gohman778b06b2009-09-02 19:35:19 +0000223 SMDiagnostic Err;
Chris Lattner1a735402007-05-06 04:55:19 +0000224 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000225
Dan Gohman778b06b2009-09-02 19:35:19 +0000226 M.reset(ParseIRFile(InputFilename, Err, Context));
Chris Lattner1a735402007-05-06 04:55:19 +0000227 if (M.get() == 0) {
Dan Gohman778b06b2009-09-02 19:35:19 +0000228 Err.Print(argv[0], errs());
Chris Lattner1a735402007-05-06 04:55:19 +0000229 return 1;
230 }
231 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000232
Chris Lattner1a735402007-05-06 04:55:19 +0000233 // If we are supposed to override the target triple, do so now.
234 if (!TargetTriple.empty())
235 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000236
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000237 Triple TheTriple(mod.getTargetTriple());
238 if (TheTriple.getTriple().empty())
239 TheTriple.setTriple(sys::getHostTriple());
240
241 // Allocate target machine. First, check whether the user has explicitly
242 // specified an architecture to compile for. If so we have to look it up by
243 // name, because it might be a backend that has no mapping to a target triple.
Daniel Dunbar1d929212009-07-16 02:23:53 +0000244 const Target *TheTarget = 0;
245 if (!MArch.empty()) {
246 for (TargetRegistry::iterator it = TargetRegistry::begin(),
247 ie = TargetRegistry::end(); it != ie; ++it) {
248 if (MArch == it->getName()) {
249 TheTarget = &*it;
250 break;
251 }
252 }
253
254 if (!TheTarget) {
255 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
256 return 1;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000257 }
258
259 // Adjust the triple to match (if known), otherwise stick with the
260 // module/host triple.
261 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
262 if (Type != Triple::UnknownArch)
263 TheTriple.setArch(Type);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000264 } else {
Chris Lattner1a735402007-05-06 04:55:19 +0000265 std::string Err;
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000266 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000267 if (TheTarget == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000268 errs() << argv[0] << ": error auto-selecting target for module '"
269 << Err << "'. Please use the -march option to explicitly "
270 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000271 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000272 }
Chris Lattner63342052002-10-29 21:12:46 +0000273 }
Chris Lattner1a735402007-05-06 04:55:19 +0000274
275 // Package up features to be passed to target/subtarget
276 std::string FeaturesStr;
277 if (MCPU.size() || MAttrs.size()) {
278 SubtargetFeatures Features;
279 Features.setCPU(MCPU);
280 for (unsigned i = 0; i != MAttrs.size(); ++i)
281 Features.AddFeature(MAttrs[i]);
282 FeaturesStr = Features.getString();
283 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000284
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000285 std::auto_ptr<TargetMachine>
Daniel Dunbar4b3d5722009-08-04 04:08:40 +0000286 target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr));
Chris Lattner1a735402007-05-06 04:55:19 +0000287 assert(target.get() && "Could not allocate target machine!");
288 TargetMachine &Target = *target.get();
289
290 // Figure out where we are going to send the output...
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000291 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000292 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000293
Evan Cheng712e80e2009-05-04 23:05:19 +0000294 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling98a366d2009-04-29 23:29:43 +0000295 switch (OptLevel) {
296 default:
Dan Gohman65f57c22009-07-15 16:35:29 +0000297 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendling8dc85dd2009-04-29 23:46:43 +0000298 return 1;
Bill Wendling98a366d2009-04-29 23:29:43 +0000299 case ' ': break;
300 case '0': OLvl = CodeGenOpt::None; break;
Evan Chengbf57b522009-10-16 21:02:20 +0000301 case '1': OLvl = CodeGenOpt::Less; break;
Bill Wendling581b9342009-04-30 00:57:51 +0000302 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000303 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000304 }
305
Chris Lattner1a735402007-05-06 04:55:19 +0000306 // If this target requires addPassesToEmitWholeFile, do it now. This is
307 // used by strange things like the C backend.
308 if (Target.WantsWholeFile()) {
309 PassManager PM;
Daniel Dunbar4e02eb02009-08-03 17:34:19 +0000310
311 // Add the target data from the target machine, if it exists, or the module.
312 if (const TargetData *TD = Target.getTargetData())
313 PM.add(new TargetData(*TD));
314 else
315 PM.add(new TargetData(&mod));
316
Chris Lattner1a735402007-05-06 04:55:19 +0000317 if (!NoVerify)
318 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000319
Chris Lattner1a735402007-05-06 04:55:19 +0000320 // Ask the target to add backend passes as necessary.
Bill Wendling98a366d2009-04-29 23:29:43 +0000321 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000322 errs() << argv[0] << ": target does not support generation of this"
323 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000324 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000325 // And the Out file is empty and useless, so remove it now.
326 sys::Path(OutputFilename).eraseFromDisk();
327 return 1;
328 }
329 PM.run(mod);
330 } else {
331 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000332 ExistingModuleProvider Provider(M.release());
333 FunctionPassManager Passes(&Provider);
Daniel Dunbar4e02eb02009-08-03 17:34:19 +0000334
335 // Add the target data from the target machine, if it exists, or the module.
336 if (const TargetData *TD = Target.getTargetData())
337 Passes.add(new TargetData(*TD));
338 else
339 Passes.add(new TargetData(&mod));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000340
Chris Lattner1a735402007-05-06 04:55:19 +0000341#ifndef NDEBUG
342 if (!NoVerify)
343 Passes.add(createVerifierPass());
344#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000345
Chris Lattner1a735402007-05-06 04:55:19 +0000346 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000347 ObjectCodeEmitter *OCE = 0;
Chris Lattner1a735402007-05-06 04:55:19 +0000348
Evan Cheng23120ba2009-03-25 01:48:21 +0000349 // Override default to generate verbose assembly.
350 Target.setAsmVerbosityDefault(true);
351
Bill Wendling98a366d2009-04-29 23:29:43 +0000352 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000353 default:
354 assert(0 && "Invalid file model!");
355 return 1;
356 case FileModel::Error:
Dan Gohman65f57c22009-07-15 16:35:29 +0000357 errs() << argv[0] << ": target does not support generation of this"
358 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000359 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000360 // And the Out file is empty and useless, so remove it now.
361 sys::Path(OutputFilename).eraseFromDisk();
362 return 1;
363 case FileModel::AsmFile:
364 break;
365 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000366 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000367 break;
368 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000369 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000370 break;
371 }
372
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000373 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000374 errs() << argv[0] << ": target does not support generation of this"
375 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000376 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000377 // And the Out file is empty and useless, so remove it now.
378 sys::Path(OutputFilename).eraseFromDisk();
379 return 1;
380 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000381
Chris Lattner1a735402007-05-06 04:55:19 +0000382 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000383
Chris Lattner1a735402007-05-06 04:55:19 +0000384 // Run our queue of passes all at once now, efficiently.
385 // TODO: this could lazily stream functions out of the module.
386 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Pateld18e31a2009-06-04 22:05:33 +0000387 if (!I->isDeclaration()) {
388 if (DisableRedZone)
389 I->addFnAttr(Attribute::NoRedZone);
Devang Patel578efa92009-06-05 21:57:13 +0000390 if (NoImplicitFloats)
391 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner1a735402007-05-06 04:55:19 +0000392 Passes.run(*I);
Devang Pateld18e31a2009-06-04 22:05:33 +0000393 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000394
Chris Lattner1a735402007-05-06 04:55:19 +0000395 Passes.doFinalization();
396 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000397
Chris Lattner1a735402007-05-06 04:55:19 +0000398 // Delete the ostream if it's not a stdout stream
David Greene71847812009-07-14 20:18:05 +0000399 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000400
401 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000402}