blob: a2474729dd22797a1898b9d8bc895c3dd74e2d4a [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
Chris Lattner1a735402007-05-06 04:55:19 +000016#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendling546d0fb2007-02-08 01:41:07 +000017#include "llvm/CodeGen/FileWriters.h"
Chris Lattnerd3a680a2006-08-01 22:34:35 +000018#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Anton Korobeynikov2b110042008-08-17 14:33:01 +000019#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000020#include "llvm/Target/SubtargetFeature.h"
Owen Anderson07000c62006-05-12 06:33:49 +000021#include "llvm/Target/TargetData.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000023#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000025#include "llvm/Module.h"
Chris Lattner744879e2007-05-06 09:32:02 +000026#include "llvm/ModuleProvider.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000027#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000028#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000030#include "llvm/Support/FileUtilities.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000031#include "llvm/Support/ManagedStatic.h"
Chris Lattner1a735402007-05-06 04:55:19 +000032#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000034#include "llvm/Support/PrettyStackTrace.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000035#include "llvm/Support/RegistryParser.h"
Owen Andersoncb371882008-08-21 00:14:44 +000036#include "llvm/Support/raw_ostream.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000037#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000038#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000039#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000040#include "llvm/LinkAllVMCore.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000041#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000042#include <iostream>
43#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000044using namespace llvm;
45
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000046// General options for llc. Other pass-specific options are specified
47// within the corresponding llc passes, and target-specific options
48// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000049//
Chris Lattnerb5881f12003-04-25 05:26:11 +000050static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000051InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Chris Lattnerb5881f12003-04-25 05:26:11 +000053static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000054OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
55
56static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
57
Evan Cheng712e80e2009-05-04 23:05:19 +000058// Determine optimization level.
Bill Wendling98a366d2009-04-29 23:29:43 +000059static cl::opt<char>
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000060OptLevel("O",
Evan Cheng712e80e2009-05-04 23:05:19 +000061 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
62 "(default = '-O2')"),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000063 cl::Prefix,
64 cl::ZeroOrMore,
Bill Wendling98a366d2009-04-29 23:29:43 +000065 cl::init(' '));
Chris Lattner178e0c42005-11-08 02:12:17 +000066
Chris Lattnerf33b8662005-12-16 04:59:57 +000067static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000068TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000069
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000070static cl::opt<const TargetMachineRegistry::entry*, false,
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000071 RegistryParser<TargetMachine> >
Chris Lattnercbb34a72005-06-25 03:00:34 +000072MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000073
Jim Laskeyb1e11802005-09-01 21:38:21 +000074static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000075MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000076 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000077 cl::value_desc("cpu-name"),
78 cl::init(""));
79
80static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000081MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000082 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000083 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000084 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000085
Chris Lattner812125a2005-06-25 03:32:05 +000086cl::opt<TargetMachine::CodeGenFileType>
87FileType("filetype", cl::init(TargetMachine::AssemblyFile),
88 cl::desc("Choose a file type (not all types are supported by all targets):"),
89 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000090 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000091 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000092 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000093 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000094 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000095 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000096 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000097 clEnumValEnd));
98
Reid Spencer4418c2b2005-07-28 02:25:30 +000099cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000100 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +0000101
Chris Lattner812125a2005-06-25 03:32:05 +0000102
Devang Pateld18e31a2009-06-04 22:05:33 +0000103static cl::opt<bool>
104DisableRedZone("disable-red-zone",
105 cl::desc("Do not emit code that uses the red zone."),
106 cl::init(false));
107
Chris Lattner812125a2005-06-25 03:32:05 +0000108// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +0000109static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +0000110GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000111 std::string IFN = InputFilename;
112 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000113 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000114 if ((Len > 2) &&
115 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000116 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000117 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000118 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000119 }
120 return outputFilename;
121}
122
Owen Andersoncb371882008-08-21 00:14:44 +0000123static raw_ostream *GetOutputStream(const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000124 if (OutputFilename != "") {
125 if (OutputFilename == "-")
Owen Andersoncb371882008-08-21 00:14:44 +0000126 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000127
128 // Specified an output filename?
129 if (!Force && std::ifstream(OutputFilename.c_str())) {
130 // If force is not specified, make sure not to overwrite a file!
131 std::cerr << ProgName << ": error opening '" << OutputFilename
132 << "': file exists!\n"
133 << "Use -f command line argument to force output\n";
134 return 0;
135 }
136 // Make sure that the Out file gets unlinked from the disk if we get a
137 // SIGINT
138 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
139
Owen Andersoncb371882008-08-21 00:14:44 +0000140 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000141 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), true, error);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000142 if (!error.empty()) {
143 std::cerr << error << '\n';
144 delete Out;
145 return 0;
146 }
147
148 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000149 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000150
Chris Lattner1911fd42006-09-04 04:14:57 +0000151 if (InputFilename == "-") {
152 OutputFilename = "-";
Owen Andersoncb371882008-08-21 00:14:44 +0000153 return &outs();
Chris Lattner1911fd42006-09-04 04:14:57 +0000154 }
155
156 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000157
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000158 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000159 switch (FileType) {
160 case TargetMachine::AssemblyFile:
Anton Korobeynikov50276522008-04-23 22:29:24 +0000161 if (MArch->Name[0] == 'c') {
162 if (MArch->Name[1] == 0)
163 OutputFilename += ".cbe.c";
164 else if (MArch->Name[1] == 'p' && MArch->Name[2] == 'p')
165 OutputFilename += ".cpp";
166 else
167 OutputFilename += ".s";
168 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000169 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000170 break;
171 case TargetMachine::ObjectFile:
172 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000173 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000174 break;
175 case TargetMachine::DynamicLibrary:
176 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000177 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000178 break;
179 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000180
Chris Lattner1911fd42006-09-04 04:14:57 +0000181 if (!Force && std::ifstream(OutputFilename.c_str())) {
182 // If force is not specified, make sure not to overwrite a file!
183 std::cerr << ProgName << ": error opening '" << OutputFilename
184 << "': file exists!\n"
185 << "Use -f command line argument to force output\n";
186 return 0;
187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000188
Chris Lattner1911fd42006-09-04 04:14:57 +0000189 // Make sure that the Out file gets unlinked from the disk if we get a
190 // SIGINT
191 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000192
Owen Andersoncb371882008-08-21 00:14:44 +0000193 std::string error;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000194 raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Binary, error);
Owen Andersoncb371882008-08-21 00:14:44 +0000195 if (!error.empty()) {
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000196 std::cerr << error << '\n';
Chris Lattner1911fd42006-09-04 04:14:57 +0000197 delete Out;
198 return 0;
199 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000200
Chris Lattner1911fd42006-09-04 04:14:57 +0000201 return Out;
202}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000203
Chris Lattner5b836c42003-06-20 15:49:04 +0000204// main - Entry point for the llc compiler.
205//
206int main(int argc, char **argv) {
Chris Lattner1a735402007-05-06 04:55:19 +0000207 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000208 PrettyStackTraceProgram X(argc, argv);
209 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
210 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner364d1202004-02-19 20:32:39 +0000211
Chris Lattner1a735402007-05-06 04:55:19 +0000212 // Load the module to be compiled...
213 std::string ErrorMessage;
214 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000215
Chris Lattner744879e2007-05-06 09:32:02 +0000216 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000217 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner744879e2007-05-06 09:32:02 +0000218 if (Buffer.get())
219 M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage));
Chris Lattner1a735402007-05-06 04:55:19 +0000220 if (M.get() == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000221 std::cerr << argv[0] << ": bitcode didn't read correctly.\n";
Chris Lattner1a735402007-05-06 04:55:19 +0000222 std::cerr << "Reason: " << ErrorMessage << "\n";
223 return 1;
224 }
225 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000226
Chris Lattner1a735402007-05-06 04:55:19 +0000227 // If we are supposed to override the target triple, do so now.
228 if (!TargetTriple.empty())
229 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000230
Chris Lattner1a735402007-05-06 04:55:19 +0000231 // Allocate target machine. First, check whether the user has
232 // explicitly specified an architecture to compile for.
233 if (MArch == 0) {
234 std::string Err;
235 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
236 if (MArch == 0) {
237 std::cerr << argv[0] << ": error auto-selecting target for module '"
238 << Err << "'. Please use the -march option to explicitly "
239 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000240 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000241 }
Chris Lattner63342052002-10-29 21:12:46 +0000242 }
Chris Lattner1a735402007-05-06 04:55:19 +0000243
244 // Package up features to be passed to target/subtarget
245 std::string FeaturesStr;
246 if (MCPU.size() || MAttrs.size()) {
247 SubtargetFeatures Features;
248 Features.setCPU(MCPU);
249 for (unsigned i = 0; i != MAttrs.size(); ++i)
250 Features.AddFeature(MAttrs[i]);
251 FeaturesStr = Features.getString();
252 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000253
Chris Lattner1a735402007-05-06 04:55:19 +0000254 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
255 assert(target.get() && "Could not allocate target machine!");
256 TargetMachine &Target = *target.get();
257
258 // Figure out where we are going to send the output...
Owen Andersoncb371882008-08-21 00:14:44 +0000259 raw_ostream *Out = GetOutputStream(argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000260 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000261
Evan Cheng712e80e2009-05-04 23:05:19 +0000262 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling98a366d2009-04-29 23:29:43 +0000263 switch (OptLevel) {
264 default:
Bill Wendling8dc85dd2009-04-29 23:46:43 +0000265 std::cerr << argv[0] << ": invalid optimization level.\n";
266 return 1;
Bill Wendling98a366d2009-04-29 23:29:43 +0000267 case ' ': break;
268 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendling581b9342009-04-30 00:57:51 +0000269 case '1':
270 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000271 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000272 }
273
Chris Lattner1a735402007-05-06 04:55:19 +0000274 // If this target requires addPassesToEmitWholeFile, do it now. This is
275 // used by strange things like the C backend.
276 if (Target.WantsWholeFile()) {
277 PassManager PM;
278 PM.add(new TargetData(*Target.getTargetData()));
279 if (!NoVerify)
280 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000281
Chris Lattner1a735402007-05-06 04:55:19 +0000282 // Ask the target to add backend passes as necessary.
Bill Wendling98a366d2009-04-29 23:29:43 +0000283 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000284 std::cerr << argv[0] << ": target does not support generation of this"
285 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000286 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000287 // And the Out file is empty and useless, so remove it now.
288 sys::Path(OutputFilename).eraseFromDisk();
289 return 1;
290 }
291 PM.run(mod);
292 } else {
293 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000294 ExistingModuleProvider Provider(M.release());
295 FunctionPassManager Passes(&Provider);
Chris Lattner1a735402007-05-06 04:55:19 +0000296 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000297
Chris Lattner1a735402007-05-06 04:55:19 +0000298#ifndef NDEBUG
299 if (!NoVerify)
300 Passes.add(createVerifierPass());
301#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000302
Chris Lattner1a735402007-05-06 04:55:19 +0000303 // Ask the target to add backend passes as necessary.
304 MachineCodeEmitter *MCE = 0;
305
Evan Cheng23120ba2009-03-25 01:48:21 +0000306 // Override default to generate verbose assembly.
307 Target.setAsmVerbosityDefault(true);
308
Bill Wendling98a366d2009-04-29 23:29:43 +0000309 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000310 default:
311 assert(0 && "Invalid file model!");
312 return 1;
313 case FileModel::Error:
314 std::cerr << argv[0] << ": target does not support generation of this"
315 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000316 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000317 // And the Out file is empty and useless, so remove it now.
318 sys::Path(OutputFilename).eraseFromDisk();
319 return 1;
320 case FileModel::AsmFile:
321 break;
322 case FileModel::MachOFile:
323 MCE = AddMachOWriter(Passes, *Out, Target);
324 break;
325 case FileModel::ElfFile:
326 MCE = AddELFWriter(Passes, *Out, Target);
327 break;
328 }
329
Bill Wendling98a366d2009-04-29 23:29:43 +0000330 if (Target.addPassesToEmitFileFinish(Passes, MCE, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000331 std::cerr << argv[0] << ": target does not support generation of this"
332 << " file type!\n";
Owen Andersoncb371882008-08-21 00:14:44 +0000333 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000334 // And the Out file is empty and useless, so remove it now.
335 sys::Path(OutputFilename).eraseFromDisk();
336 return 1;
337 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000338
Chris Lattner1a735402007-05-06 04:55:19 +0000339 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000340
Chris Lattner1a735402007-05-06 04:55:19 +0000341 // Run our queue of passes all at once now, efficiently.
342 // TODO: this could lazily stream functions out of the module.
343 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Pateld18e31a2009-06-04 22:05:33 +0000344 if (!I->isDeclaration()) {
345 if (DisableRedZone)
346 I->addFnAttr(Attribute::NoRedZone);
Chris Lattner1a735402007-05-06 04:55:19 +0000347 Passes.run(*I);
Devang Pateld18e31a2009-06-04 22:05:33 +0000348 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000349
Chris Lattner1a735402007-05-06 04:55:19 +0000350 Passes.doFinalization();
351 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000352
Chris Lattner1a735402007-05-06 04:55:19 +0000353 // Delete the ostream if it's not a stdout stream
Owen Andersoncb371882008-08-21 00:14:44 +0000354 if (Out != &outs()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000355
356 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000357}