Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 1 | //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e737c7a | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 9 | // |
Brian Gaeke | e40eae7 | 2004-03-16 21:47:20 +0000 | [diff] [blame] | 10 | // This is the llc code generator driver. It provides a convenient |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 11 | // command-line interface for generating native assembly-language code |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 12 | // or C code, given LLVM bitcode. |
Chris Lattner | e737c7a | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | b79757c | 2001-10-04 01:40:53 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | cb465fc | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 15 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 16 | #include "llvm/LLVMContext.h" |
Chris Lattner | 46ac43c | 2001-09-07 21:26:31 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 18 | #include "llvm/ModuleProvider.h" |
Chris Lattner | cd50d3f | 2002-01-31 00:46:45 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
| 22 | #include "llvm/Analysis/Verifier.h" |
Dan Gohman | 778b06b | 2009-09-02 19:35:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/IRReader.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 24 | #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 Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileUtilities.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 36 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 37 | #include "llvm/System/Host.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 38 | #include "llvm/System/Signals.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 39 | #include "llvm/Target/SubtargetFeature.h" |
| 40 | #include "llvm/Target/TargetData.h" |
| 41 | #include "llvm/Target/TargetMachine.h" |
| 42 | #include "llvm/Target/TargetRegistry.h" |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetSelect.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Scalar.h" |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 45 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 48 | // 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 Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 51 | // |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 52 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 53 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 54 | |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 55 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 56 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 57 | |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
| 59 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 60 | |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 61 | // Determine optimization level. |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 62 | static cl::opt<char> |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 63 | OptLevel("O", |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 64 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 65 | "(default = '-O2')"), |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 66 | cl::Prefix, |
| 67 | cl::ZeroOrMore, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 68 | cl::init(' ')); |
Chris Lattner | 178e0c4 | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 69 | |
Chris Lattner | f33b866 | 2005-12-16 04:59:57 +0000 | [diff] [blame] | 70 | static cl::opt<std::string> |
Chris Lattner | be19383 | 2005-12-16 05:19:55 +0000 | [diff] [blame] | 71 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Chris Lattner | 178e0c4 | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | 1d92921 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 73 | static cl::opt<std::string> |
| 74 | MArch("march", cl::desc("Architecture to generate code for (see --version)")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 75 | |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 76 | static cl::opt<std::string> |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 77 | MCPU("mcpu", |
Chris Lattner | 7b7593c | 2005-10-23 22:35:42 +0000 | [diff] [blame] | 78 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 79 | cl::value_desc("cpu-name"), |
| 80 | cl::init("")); |
| 81 | |
| 82 | static cl::list<std::string> |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 83 | MAttrs("mattr", |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 84 | cl::CommaSeparated, |
Chris Lattner | 7b7593c | 2005-10-23 22:35:42 +0000 | [diff] [blame] | 85 | cl::desc("Target specific attributes (-mattr=help for details)"), |
Chris Lattner | 2094725 | 2005-10-23 22:37:13 +0000 | [diff] [blame] | 86 | cl::value_desc("a1,+a2,-a3,...")); |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 88 | cl::opt<TargetMachine::CodeGenFileType> |
| 89 | FileType("filetype", cl::init(TargetMachine::AssemblyFile), |
| 90 | cl::desc("Choose a file type (not all types are supported by all targets):"), |
| 91 | cl::values( |
Misha Brukman | 262b05f | 2008-12-31 17:39:58 +0000 | [diff] [blame] | 92 | clEnumValN(TargetMachine::AssemblyFile, "asm", |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 93 | "Emit an assembly ('.s') file"), |
Misha Brukman | 262b05f | 2008-12-31 17:39:58 +0000 | [diff] [blame] | 94 | clEnumValN(TargetMachine::ObjectFile, "obj", |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 95 | "Emit a native object ('.o') file [experimental]"), |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 96 | clEnumValN(TargetMachine::DynamicLibrary, "dynlib", |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 97 | "Emit a native dynamic library ('.so') file" |
Nate Begeman | 712b835 | 2006-08-23 21:29:52 +0000 | [diff] [blame] | 98 | " [experimental]"), |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 99 | clEnumValEnd)); |
| 100 | |
Reid Spencer | 4418c2b | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 101 | cl::opt<bool> NoVerify("disable-verify", cl::Hidden, |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 102 | cl::desc("Do not verify input module")); |
Reid Spencer | 4418c2b | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 104 | |
Devang Patel | d18e31a | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 105 | static cl::opt<bool> |
| 106 | DisableRedZone("disable-red-zone", |
| 107 | cl::desc("Do not emit code that uses the red zone."), |
| 108 | cl::init(false)); |
| 109 | |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 110 | static cl::opt<bool> |
| 111 | NoImplicitFloats("no-implicit-float", |
| 112 | cl::desc("Don't generate implicit floating point instructions (x86-only)"), |
| 113 | cl::init(false)); |
| 114 | |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 115 | // GetFileNameRoot - Helper function to get the basename of a filename. |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 116 | static inline std::string |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 117 | GetFileNameRoot(const std::string &InputFilename) { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 118 | std::string IFN = InputFilename; |
| 119 | std::string outputFilename; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 120 | int Len = IFN.length(); |
John Criswell | b5d09bf | 2003-08-28 21:42:29 +0000 | [diff] [blame] | 121 | if ((Len > 2) && |
Dan Gohman | d81c450 | 2009-09-16 19:18:41 +0000 | [diff] [blame] | 122 | IFN[Len-3] == '.' && |
| 123 | ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') || |
| 124 | (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 125 | outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 126 | } else { |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 127 | outputFilename = IFN; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 128 | } |
| 129 | return outputFilename; |
| 130 | } |
| 131 | |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 132 | static formatted_raw_ostream *GetOutputStream(const char *TargetName, |
| 133 | const char *ProgName) { |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 134 | if (OutputFilename != "") { |
| 135 | if (OutputFilename == "-") |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 136 | return &fouts(); |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 138 | // 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 Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 142 | std::string error; |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 143 | raw_fd_ostream *FDOut = |
| 144 | new raw_fd_ostream(OutputFilename.c_str(), error, |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 145 | raw_fd_ostream::F_Binary); |
Dan Gohman | ed3e8b4 | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 146 | if (!error.empty()) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 147 | errs() << error << '\n'; |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 148 | delete FDOut; |
Dan Gohman | ed3e8b4 | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 151 | formatted_raw_ostream *Out = |
| 152 | new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM); |
Dan Gohman | ed3e8b4 | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 153 | |
| 154 | return Out; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 155 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 157 | if (InputFilename == "-") { |
| 158 | OutputFilename = "-"; |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 159 | return &fouts(); |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | OutputFilename = GetFileNameRoot(InputFilename); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 164 | bool Binary = false; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 165 | switch (FileType) { |
| 166 | case TargetMachine::AssemblyFile: |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 167 | if (TargetName[0] == 'c') { |
| 168 | if (TargetName[1] == 0) |
Anton Korobeynikov | 5027652 | 2008-04-23 22:29:24 +0000 | [diff] [blame] | 169 | OutputFilename += ".cbe.c"; |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 170 | else if (TargetName[1] == 'p' && TargetName[2] == 'p') |
Anton Korobeynikov | 5027652 | 2008-04-23 22:29:24 +0000 | [diff] [blame] | 171 | OutputFilename += ".cpp"; |
| 172 | else |
| 173 | OutputFilename += ".s"; |
| 174 | } else |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 175 | OutputFilename += ".s"; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 176 | break; |
| 177 | case TargetMachine::ObjectFile: |
| 178 | OutputFilename += ".o"; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 179 | Binary = true; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 180 | break; |
| 181 | case TargetMachine::DynamicLibrary: |
| 182 | OutputFilename += LTDL_SHLIB_EXT; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 183 | Binary = true; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 187 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 188 | // SIGINT |
| 189 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 190 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 191 | std::string error; |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 192 | unsigned OpenFlags = 0; |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 193 | if (Binary) OpenFlags |= raw_fd_ostream::F_Binary; |
| 194 | raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), error, |
| 195 | OpenFlags); |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 196 | if (!error.empty()) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 197 | errs() << error << '\n'; |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 198 | delete FDOut; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 199 | return 0; |
| 200 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 201 | |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 202 | formatted_raw_ostream *Out = |
| 203 | new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM); |
| 204 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 205 | return Out; |
| 206 | } |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 208 | // main - Entry point for the llc compiler. |
| 209 | // |
| 210 | int main(int argc, char **argv) { |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 211 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 212 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 213 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 214 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Chris Lattner | 364d120 | 2004-02-19 20:32:39 +0000 | [diff] [blame] | 215 | |
Daniel Dunbar | 2b991bb | 2009-09-03 05:47:22 +0000 | [diff] [blame] | 216 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 217 | InitializeAllTargets(); |
| 218 | InitializeAllAsmPrinters(); |
Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 219 | |
| 220 | cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 222 | // Load the module to be compiled... |
Dan Gohman | 778b06b | 2009-09-02 19:35:19 +0000 | [diff] [blame] | 223 | SMDiagnostic Err; |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 224 | std::auto_ptr<Module> M; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 225 | |
Dan Gohman | 778b06b | 2009-09-02 19:35:19 +0000 | [diff] [blame] | 226 | M.reset(ParseIRFile(InputFilename, Err, Context)); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 227 | if (M.get() == 0) { |
Dan Gohman | 778b06b | 2009-09-02 19:35:19 +0000 | [diff] [blame] | 228 | Err.Print(argv[0], errs()); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | Module &mod = *M.get(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 233 | // If we are supposed to override the target triple, do so now. |
| 234 | if (!TargetTriple.empty()) |
| 235 | mod.setTargetTriple(TargetTriple); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 236 | |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 237 | 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 Dunbar | 1d92921 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 244 | 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 Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 257 | } |
| 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 Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 264 | } else { |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 265 | std::string Err; |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 266 | TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 267 | if (TheTarget == 0) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 268 | errs() << argv[0] << ": error auto-selecting target for module '" |
| 269 | << Err << "'. Please use the -march option to explicitly " |
| 270 | << "pick a target.\n"; |
Chris Lattner | bb43350 | 2003-08-24 14:02:14 +0000 | [diff] [blame] | 271 | return 1; |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 274 | |
| 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 Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 285 | std::auto_ptr<TargetMachine> |
Daniel Dunbar | 4b3d572 | 2009-08-04 04:08:40 +0000 | [diff] [blame] | 286 | target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr)); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 287 | 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 Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 291 | formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 292 | if (Out == 0) return 1; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 293 | |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 294 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 295 | switch (OptLevel) { |
| 296 | default: |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 297 | errs() << argv[0] << ": invalid optimization level.\n"; |
Bill Wendling | 8dc85dd | 2009-04-29 23:46:43 +0000 | [diff] [blame] | 298 | return 1; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 299 | case ' ': break; |
| 300 | case '0': OLvl = CodeGenOpt::None; break; |
Evan Cheng | bf57b52 | 2009-10-16 21:02:20 +0000 | [diff] [blame] | 301 | case '1': OLvl = CodeGenOpt::Less; break; |
Bill Wendling | 581b934 | 2009-04-30 00:57:51 +0000 | [diff] [blame] | 302 | case '2': OLvl = CodeGenOpt::Default; break; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 303 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 306 | // 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 Dunbar | 4e02eb0 | 2009-08-03 17:34:19 +0000 | [diff] [blame] | 310 | |
| 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 Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 317 | if (!NoVerify) |
| 318 | PM.add(createVerifierPass()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 320 | // Ask the target to add backend passes as necessary. |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 321 | if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 322 | errs() << argv[0] << ": target does not support generation of this" |
| 323 | << " file type!\n"; |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 324 | if (Out != &fouts()) delete Out; |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 325 | // 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 Gohman | 33ef2bb | 2008-04-16 15:56:26 +0000 | [diff] [blame] | 332 | ExistingModuleProvider Provider(M.release()); |
| 333 | FunctionPassManager Passes(&Provider); |
Daniel Dunbar | 4e02eb0 | 2009-08-03 17:34:19 +0000 | [diff] [blame] | 334 | |
| 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 Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 341 | #ifndef NDEBUG |
| 342 | if (!NoVerify) |
| 343 | Passes.add(createVerifierPass()); |
| 344 | #endif |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 346 | // Ask the target to add backend passes as necessary. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 347 | ObjectCodeEmitter *OCE = 0; |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 348 | |
Evan Cheng | 23120ba | 2009-03-25 01:48:21 +0000 | [diff] [blame] | 349 | // Override default to generate verbose assembly. |
| 350 | Target.setAsmVerbosityDefault(true); |
| 351 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 352 | switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) { |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 353 | default: |
| 354 | assert(0 && "Invalid file model!"); |
| 355 | return 1; |
| 356 | case FileModel::Error: |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 357 | errs() << argv[0] << ": target does not support generation of this" |
| 358 | << " file type!\n"; |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 359 | if (Out != &fouts()) delete Out; |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 360 | // 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 Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 366 | OCE = AddMachOWriter(Passes, *Out, Target); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 367 | break; |
| 368 | case FileModel::ElfFile: |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 369 | OCE = AddELFWriter(Passes, *Out, Target); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 373 | if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 374 | errs() << argv[0] << ": target does not support generation of this" |
| 375 | << " file type!\n"; |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 376 | if (Out != &fouts()) delete Out; |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 377 | // And the Out file is empty and useless, so remove it now. |
| 378 | sys::Path(OutputFilename).eraseFromDisk(); |
| 379 | return 1; |
| 380 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 382 | Passes.doInitialization(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 384 | // 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 Patel | d18e31a | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 387 | if (!I->isDeclaration()) { |
| 388 | if (DisableRedZone) |
| 389 | I->addFnAttr(Attribute::NoRedZone); |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 390 | if (NoImplicitFloats) |
| 391 | I->addFnAttr(Attribute::NoImplicitFloat); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 392 | Passes.run(*I); |
Devang Patel | d18e31a | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 393 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 395 | Passes.doFinalization(); |
| 396 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 398 | // Delete the ostream if it's not a stdout stream |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 399 | if (Out != &fouts()) delete Out; |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 400 | |
| 401 | return 0; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 402 | } |