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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 |
Brian Gaeke | e40eae7 | 2004-03-16 21:47:20 +0000 | [diff] [blame] | 12 | // or C code, given LLVM bytecode. |
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 | |
Vikram S. Adve | cb465fc | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 16 | #include "llvm/Bytecode/Reader.h" |
Chris Lattner | d3a680a | 2006-08-01 22:34:35 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 18 | #include "llvm/Target/SubtargetFeature.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachineRegistry.h" |
Chris Lattner | 65f1b89 | 2002-05-07 20:03:27 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 46ac43c | 2001-09-07 21:26:31 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Chris Lattner | cd50d3f | 2002-01-31 00:46:45 +0000 | [diff] [blame] | 24 | #include "llvm/PassManager.h" |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/PluginLoader.h" |
Duraid Madina | ea8aed7 | 2005-12-30 02:47:21 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FileUtilities.h" |
Reid Spencer | 4418c2b | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 30 | #include "llvm/System/Signals.h" |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 31 | #include "llvm/Config/config.h" |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 32 | #include "llvm/LinkAllVMCore.h" |
Chris Lattner | 78f7e1a | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 33 | #include <fstream> |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 34 | #include <iostream> |
| 35 | #include <memory> |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 36 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 39 | // General options for llc. Other pass-specific options are specified |
| 40 | // within the corresponding llc passes, and target-specific options |
| 41 | // and back-end code generation options are specified with the target machine. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 42 | // |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 43 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 44 | InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); |
| 45 | |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 46 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 47 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 48 | |
| 49 | static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); |
| 50 | |
Chris Lattner | 178e0c4 | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 51 | static cl::opt<bool> Fast("fast", |
| 52 | cl::desc("Generate code quickly, potentially sacrificing code quality")); |
| 53 | |
Chris Lattner | f33b866 | 2005-12-16 04:59:57 +0000 | [diff] [blame] | 54 | static cl::opt<std::string> |
Chris Lattner | be19383 | 2005-12-16 05:19:55 +0000 | [diff] [blame] | 55 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Chris Lattner | 178e0c4 | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 56 | |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 57 | static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser> |
Chris Lattner | cbb34a7 | 2005-06-25 03:00:34 +0000 | [diff] [blame] | 58 | MArch("march", cl::desc("Architecture to generate code for:")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 59 | |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 60 | static cl::opt<std::string> |
| 61 | MCPU("mcpu", |
Chris Lattner | 7b7593c | 2005-10-23 22:35:42 +0000 | [diff] [blame] | 62 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 63 | cl::value_desc("cpu-name"), |
| 64 | cl::init("")); |
| 65 | |
| 66 | static cl::list<std::string> |
| 67 | MAttrs("mattr", |
| 68 | cl::CommaSeparated, |
Chris Lattner | 7b7593c | 2005-10-23 22:35:42 +0000 | [diff] [blame] | 69 | cl::desc("Target specific attributes (-mattr=help for details)"), |
Chris Lattner | 2094725 | 2005-10-23 22:37:13 +0000 | [diff] [blame] | 70 | cl::value_desc("a1,+a2,-a3,...")); |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 72 | cl::opt<TargetMachine::CodeGenFileType> |
| 73 | FileType("filetype", cl::init(TargetMachine::AssemblyFile), |
| 74 | cl::desc("Choose a file type (not all types are supported by all targets):"), |
| 75 | cl::values( |
| 76 | clEnumValN(TargetMachine::AssemblyFile, "asm", |
| 77 | " Emit an assembly ('.s') file"), |
| 78 | clEnumValN(TargetMachine::ObjectFile, "obj", |
Chris Lattner | 07be8d4 | 2005-10-22 22:00:45 +0000 | [diff] [blame] | 79 | " Emit a native object ('.o') file [experimental]"), |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 80 | clEnumValN(TargetMachine::DynamicLibrary, "dynlib", |
Nate Begeman | 712b835 | 2006-08-23 21:29:52 +0000 | [diff] [blame] | 81 | " Emit a native dynamic library ('.so') file" |
| 82 | " [experimental]"), |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 83 | clEnumValEnd)); |
| 84 | |
Reid Spencer | 4418c2b | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 85 | cl::opt<bool> NoVerify("disable-verify", cl::Hidden, |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 86 | cl::desc("Do not verify input module")); |
Reid Spencer | 4418c2b | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 812125a | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 88 | |
| 89 | // GetFileNameRoot - Helper function to get the basename of a filename. |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 90 | static inline std::string |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 91 | GetFileNameRoot(const std::string &InputFilename) { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 92 | std::string IFN = InputFilename; |
| 93 | std::string outputFilename; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 94 | int Len = IFN.length(); |
John Criswell | b5d09bf | 2003-08-28 21:42:29 +0000 | [diff] [blame] | 95 | if ((Len > 2) && |
| 96 | IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 97 | outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 98 | } else { |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 99 | outputFilename = IFN; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 100 | } |
| 101 | return outputFilename; |
| 102 | } |
| 103 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 104 | static std::ostream *GetOutputStream(const char *ProgName) { |
| 105 | if (OutputFilename != "") { |
| 106 | if (OutputFilename == "-") |
| 107 | return &std::cout; |
| 108 | |
| 109 | // Specified an output filename? |
| 110 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 111 | // If force is not specified, make sure not to overwrite a file! |
| 112 | std::cerr << ProgName << ": error opening '" << OutputFilename |
| 113 | << "': file exists!\n" |
| 114 | << "Use -f command line argument to force output\n"; |
| 115 | return 0; |
| 116 | } |
| 117 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 118 | // SIGINT |
| 119 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 120 | |
| 121 | return new std::ofstream(OutputFilename.c_str()); |
| 122 | } |
| 123 | |
| 124 | if (InputFilename == "-") { |
| 125 | OutputFilename = "-"; |
| 126 | return &std::cout; |
| 127 | } |
| 128 | |
| 129 | OutputFilename = GetFileNameRoot(InputFilename); |
| 130 | |
| 131 | switch (FileType) { |
| 132 | case TargetMachine::AssemblyFile: |
| 133 | if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE |
| 134 | OutputFilename += ".s"; |
| 135 | else |
| 136 | OutputFilename += ".cbe.c"; |
| 137 | break; |
| 138 | case TargetMachine::ObjectFile: |
| 139 | OutputFilename += ".o"; |
| 140 | break; |
| 141 | case TargetMachine::DynamicLibrary: |
| 142 | OutputFilename += LTDL_SHLIB_EXT; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 147 | // If force is not specified, make sure not to overwrite a file! |
| 148 | std::cerr << ProgName << ": error opening '" << OutputFilename |
| 149 | << "': file exists!\n" |
| 150 | << "Use -f command line argument to force output\n"; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 155 | // SIGINT |
| 156 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 157 | |
| 158 | std::ostream *Out = new std::ofstream(OutputFilename.c_str()); |
| 159 | if (!Out->good()) { |
| 160 | std::cerr << ProgName << ": error opening " << OutputFilename << "!\n"; |
| 161 | delete Out; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | return Out; |
| 166 | } |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 168 | // main - Entry point for the llc compiler. |
| 169 | // |
| 170 | int main(int argc, char **argv) { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 171 | try { |
| 172 | cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); |
| 173 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 364d120 | 2004-02-19 20:32:39 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 175 | // Load the module to be compiled... |
| 176 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
| 177 | if (M.get() == 0) { |
| 178 | std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; |
Chris Lattner | bb43350 | 2003-08-24 14:02:14 +0000 | [diff] [blame] | 179 | return 1; |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 180 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 181 | Module &mod = *M.get(); |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 182 | |
Chris Lattner | f33b866 | 2005-12-16 04:59:57 +0000 | [diff] [blame] | 183 | // If we are supposed to override the target triple, do so now. |
| 184 | if (!TargetTriple.empty()) |
| 185 | mod.setTargetTriple(TargetTriple); |
| 186 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 187 | // Allocate target machine. First, check whether the user has |
| 188 | // explicitly specified an architecture to compile for. |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 189 | if (MArch == 0) { |
| 190 | std::string Err; |
| 191 | MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); |
| 192 | if (MArch == 0) { |
| 193 | std::cerr << argv[0] << ": error auto-selecting target for module '" |
| 194 | << Err << "'. Please use the -march option to explicitly " |
| 195 | << "pick a target.\n"; |
Misha Brukman | 878ba7c | 2004-07-12 22:58:07 +0000 | [diff] [blame] | 196 | return 1; |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 197 | } |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 198 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 199 | |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 200 | // Package up features to be passed to target/subtarget |
| 201 | std::string FeaturesStr; |
| 202 | if (MCPU.size() || MAttrs.size()) { |
| 203 | SubtargetFeatures Features; |
| 204 | Features.setCPU(MCPU); |
| 205 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 206 | Features.AddFeature(MAttrs[i]); |
| 207 | FeaturesStr = Features.getString(); |
| 208 | } |
| 209 | |
Chris Lattner | ef98691 | 2006-03-23 05:28:02 +0000 | [diff] [blame] | 210 | std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr)); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 211 | assert(target.get() && "Could not allocate target machine!"); |
| 212 | TargetMachine &Target = *target.get(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 213 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 214 | // Figure out where we are going to send the output... |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 215 | std::ostream *Out = GetOutputStream(argv[0]); |
| 216 | if (Out == 0) return 1; |
| 217 | |
| 218 | // If this target requires addPassesToEmitWholeFile, do it now. This is |
| 219 | // used by strange things like the C backend. |
| 220 | if (Target.WantsWholeFile()) { |
| 221 | PassManager PM; |
| 222 | PM.add(new TargetData(*Target.getTargetData())); |
| 223 | if (!NoVerify) |
| 224 | PM.add(createVerifierPass()); |
| 225 | |
| 226 | // Ask the target to add backend passes as necessary. |
| 227 | if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) { |
| 228 | std::cerr << argv[0] << ": target does not support generation of this" |
| 229 | << " file type!\n"; |
| 230 | if (Out != &std::cout) delete Out; |
| 231 | // And the Out file is empty and useless, so remove it now. |
| 232 | sys::Path(OutputFilename).eraseFromDisk(); |
| 233 | return 1; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 235 | PM.run(mod); |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 236 | } else { |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 237 | // Build up all of the passes that we want to do to the module. |
| 238 | FunctionPassManager Passes(new ExistingModuleProvider(M.get())); |
| 239 | Passes.add(new TargetData(*Target.getTargetData())); |
| 240 | |
| 241 | #ifndef NDEBUG |
| 242 | if (!NoVerify) |
| 243 | Passes.add(createVerifierPass()); |
| 244 | #endif |
| 245 | |
| 246 | // Ask the target to add backend passes as necessary. |
| 247 | if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) { |
| 248 | std::cerr << argv[0] << ": target does not support generation of this" |
| 249 | << " file type!\n"; |
| 250 | if (Out != &std::cout) delete Out; |
| 251 | // And the Out file is empty and useless, so remove it now. |
| 252 | sys::Path(OutputFilename).eraseFromDisk(); |
| 253 | return 1; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 255 | |
| 256 | Passes.doInitialization(); |
| 257 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 258 | // Run our queue of passes all at once now, efficiently. |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 259 | // TODO: this could lazily stream functions out of the module. |
| 260 | for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I) |
| 261 | if (!I->isExternal()) |
| 262 | Passes.run(*I); |
| 263 | |
| 264 | Passes.doFinalization(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 265 | } |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 266 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 267 | // Delete the ostream if it's not a stdout stream |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 268 | if (Out != &std::cout) delete Out; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } catch (const std::string& msg) { |
| 272 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 273 | } catch (...) { |
| 274 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 275 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 276 | return 1; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 277 | } |