Chris Lattner | 67e0842 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 1 | //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 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 | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2cf137b | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 9 | // |
Brian Gaeke | 33e83b6 | 2004-03-16 21:47:20 +0000 | [diff] [blame] | 10 | // This is the llc code generator driver. It provides a convenient |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 11 | // command-line interface for generating native assembly-language code |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 12 | // or C code, given LLVM bitcode. |
Chris Lattner | 2cf137b | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | b27d474 | 2001-10-04 01:40:53 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 2d94a34 | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 15 | |
Wan Xiaofei | 16577a5 | 2013-06-19 02:26:00 +0000 | [diff] [blame] | 16 | |
Daniel Dunbar | 0f16ea5 | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.h" |
Daniel Dunbar | 0f16ea5 | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" |
| 20 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IRPrintingPasses.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 25 | #include "llvm/IRReader/IRReader.h" |
Evan Cheng | 8264e27 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 26 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
| 28 | #include "llvm/PassManager.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
David Greene | 6e55be6 | 2010-01-05 01:30:21 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
David Greene | a31f96c | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Host.h" |
Chris Lattner | 76d4632 | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 36 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Signals.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 38 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 39 | #include "llvm/Support/TargetRegistry.h" |
| 40 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ToolOutputFile.h" |
Bob Wilson | fa59485 | 2012-08-03 21:26:18 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetLibraryInfo.h" |
Daniel Dunbar | 0f16ea5 | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | f0ebb25 | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 44 | #include <memory> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Vikram S. Adve | eb81869 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 47 | // General options for llc. Other pass-specific options are specified |
| 48 | // within the corresponding llc passes, and target-specific options |
| 49 | // and back-end code generation options are specified with the target machine. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 50 | // |
Chris Lattner | d64b2de | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 51 | static cl::opt<std::string> |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 52 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 53 | |
Chris Lattner | d64b2de | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 54 | static cl::opt<std::string> |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 56 | |
Jakob Stoklund Olesen | 2776b4c | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 57 | static cl::opt<unsigned> |
| 58 | TimeCompilations("time-compilations", cl::Hidden, cl::init(1u), |
| 59 | cl::value_desc("N"), |
| 60 | cl::desc("Repeat compilation N times for timing")); |
| 61 | |
Rafael Espindola | 48fa6ed | 2014-02-21 03:13:54 +0000 | [diff] [blame] | 62 | static cl::opt<bool> |
| 63 | NoIntegratedAssembler("no-integrated-as", cl::Hidden, |
| 64 | cl::desc("Disable integrated assembler")); |
| 65 | |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 66 | // Determine optimization level. |
Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 67 | static cl::opt<char> |
Bill Wendling | 084669a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 68 | OptLevel("O", |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 69 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 70 | "(default = '-O2')"), |
Bill Wendling | 084669a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 71 | cl::Prefix, |
| 72 | cl::ZeroOrMore, |
Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 73 | cl::init(' ')); |
Chris Lattner | 731055e | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 74 | |
Chris Lattner | e568b88 | 2005-12-16 04:59:57 +0000 | [diff] [blame] | 75 | static cl::opt<std::string> |
Chris Lattner | 08a04cb | 2005-12-16 05:19:55 +0000 | [diff] [blame] | 76 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Chris Lattner | 731055e | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 77 | |
Eric Christopher | 473ec18 | 2014-05-21 21:05:05 +0000 | [diff] [blame] | 78 | static cl::opt<bool> NoVerify("disable-verify", cl::Hidden, |
| 79 | cl::desc("Do not verify input module")); |
Reid Spencer | 8e3830d | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 80 | |
Eric Christopher | 473ec18 | 2014-05-21 21:05:05 +0000 | [diff] [blame] | 81 | static cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls", |
| 82 | cl::desc("Disable simplify-libcalls")); |
Chad Rosier | d269bd8 | 2012-08-21 16:15:24 +0000 | [diff] [blame] | 83 | |
Eric Christopher | 472cee3 | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 84 | static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden, |
| 85 | cl::desc("Show encoding in .s output")); |
| 86 | |
| 87 | static cl::opt<bool> EnableDwarfDirectory( |
| 88 | "enable-dwarf-directory", cl::Hidden, |
| 89 | cl::desc("Use .file directives with an explicit directory.")); |
| 90 | |
| 91 | static cl::opt<bool> AsmVerbose("asm-verbose", |
| 92 | cl::desc("Add comments to directives."), |
| 93 | cl::init(true)); |
| 94 | |
| 95 | static int compileModule(char **, LLVMContext &); |
Jakob Stoklund Olesen | 2776b4c | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 06fcc4c | 2005-06-25 03:32:05 +0000 | [diff] [blame] | 97 | // GetFileNameRoot - Helper function to get the basename of a filename. |
Chris Lattner | d64b2de | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 98 | static inline std::string |
Chris Lattner | 6142ca8 | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 99 | GetFileNameRoot(const std::string &InputFilename) { |
Chris Lattner | d64b2de | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 100 | std::string IFN = InputFilename; |
| 101 | std::string outputFilename; |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 102 | int Len = IFN.length(); |
John Criswell | a289abf | 2003-08-28 21:42:29 +0000 | [diff] [blame] | 103 | if ((Len > 2) && |
Dan Gohman | e8d0150 | 2009-09-16 19:18:41 +0000 | [diff] [blame] | 104 | IFN[Len-3] == '.' && |
| 105 | ((IFN[Len-2] == 'b' && IFN[Len-1] == 'c') || |
| 106 | (IFN[Len-2] == 'l' && IFN[Len-1] == 'l'))) { |
Chris Lattner | d64b2de | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 107 | outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 108 | } else { |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 109 | outputFilename = IFN; |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 110 | } |
| 111 | return outputFilename; |
| 112 | } |
| 113 | |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 114 | static tool_output_file *GetOutputStream(const char *TargetName, |
| 115 | Triple::OSType OS, |
| 116 | const char *ProgName) { |
Dan Gohman | 7ba6f22 | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 117 | // If we don't yet have an output filename, make one. |
| 118 | if (OutputFilename.empty()) { |
| 119 | if (InputFilename == "-") |
| 120 | OutputFilename = "-"; |
| 121 | else { |
| 122 | OutputFilename = GetFileNameRoot(InputFilename); |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 123 | |
Dan Gohman | 7ba6f22 | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 124 | switch (FileType) { |
Dan Gohman | 7ba6f22 | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 125 | case TargetMachine::CGFT_AssemblyFile: |
| 126 | if (TargetName[0] == 'c') { |
| 127 | if (TargetName[1] == 0) |
| 128 | OutputFilename += ".cbe.c"; |
| 129 | else if (TargetName[1] == 'p' && TargetName[2] == 'p') |
| 130 | OutputFilename += ".cpp"; |
| 131 | else |
| 132 | OutputFilename += ".s"; |
| 133 | } else |
| 134 | OutputFilename += ".s"; |
| 135 | break; |
| 136 | case TargetMachine::CGFT_ObjectFile: |
| 137 | if (OS == Triple::Win32) |
| 138 | OutputFilename += ".obj"; |
| 139 | else |
| 140 | OutputFilename += ".o"; |
| 141 | break; |
| 142 | case TargetMachine::CGFT_Null: |
| 143 | OutputFilename += ".null"; |
| 144 | break; |
| 145 | } |
Dan Gohman | f3e13bb | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 147 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 148 | |
Dan Gohman | 7ba6f22 | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 149 | // Decide if we need "binary" output. |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 150 | bool Binary = false; |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 151 | switch (FileType) { |
Chris Lattner | f0cb12a | 2010-02-02 21:06:45 +0000 | [diff] [blame] | 152 | case TargetMachine::CGFT_AssemblyFile: |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 153 | break; |
Chris Lattner | f0cb12a | 2010-02-02 21:06:45 +0000 | [diff] [blame] | 154 | case TargetMachine::CGFT_ObjectFile: |
Chris Lattner | edcf065 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 155 | case TargetMachine::CGFT_Null: |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 156 | Binary = true; |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 159 | |
Dan Gohman | 7ba6f22 | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 160 | // Open the file. |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 161 | std::string error; |
Rafael Espindola | 6d35481 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 162 | sys::fs::OpenFlags OpenFlags = sys::fs::F_None; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 163 | if (!Binary) |
| 164 | OpenFlags |= sys::fs::F_Text; |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 165 | tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error, |
| 166 | OpenFlags); |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 167 | if (!error.empty()) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 168 | errs() << error << '\n'; |
Dan Gohman | 607818a | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 169 | delete FDOut; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 170 | return nullptr; |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 171 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 172 | |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 173 | return FDOut; |
Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 174 | } |
Vikram S. Adve | 9d40935 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 67e0842 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 176 | // main - Entry point for the llc compiler. |
| 177 | // |
| 178 | int main(int argc, char **argv) { |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 179 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 180 | PrettyStackTraceProgram X(argc, argv); |
David Greene | 6e55be6 | 2010-01-05 01:30:21 +0000 | [diff] [blame] | 181 | |
| 182 | // Enable debug stream buffering. |
| 183 | EnableDebugBuffering = true; |
| 184 | |
Owen Anderson | 19251ec | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 185 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 186 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Chris Lattner | 69e896b | 2004-02-19 20:32:39 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 76628de | 2009-09-03 05:47:22 +0000 | [diff] [blame] | 188 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 5dcc4f6 | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 189 | InitializeAllTargets(); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 190 | InitializeAllTargetMCs(); |
Chris Lattner | 5dcc4f6 | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 191 | InitializeAllAsmPrinters(); |
Chris Lattner | 8900ef1 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 192 | InitializeAllAsmParsers(); |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 193 | |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 194 | // Initialize codegen and IR passes used by llc so that the -print-after, |
| 195 | // -print-before, and -stop-after options work. |
| 196 | PassRegistry *Registry = PassRegistry::getPassRegistry(); |
| 197 | initializeCore(*Registry); |
| 198 | initializeCodeGen(*Registry); |
| 199 | initializeLoopStrengthReducePass(*Registry); |
| 200 | initializeLowerIntrinsicsPass(*Registry); |
| 201 | initializeUnreachableBlockElimPass(*Registry); |
Rafael Espindola | e0eaa04 | 2012-06-26 21:33:36 +0000 | [diff] [blame] | 202 | |
Chandler Carruth | 2d71c42 | 2011-07-22 07:50:48 +0000 | [diff] [blame] | 203 | // Register the target printer for --version. |
| 204 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 205 | |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 206 | cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 207 | |
Jakob Stoklund Olesen | 2776b4c | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 208 | // Compile the module TimeCompilations times to give better compile time |
| 209 | // metrics. |
| 210 | for (unsigned I = TimeCompilations; I; --I) |
| 211 | if (int RetVal = compileModule(argv, Context)) |
| 212 | return RetVal; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int compileModule(char **argv, LLVMContext &Context) { |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 217 | // Load the module to be compiled... |
Dan Gohman | c76bfb7 | 2009-09-02 19:35:19 +0000 | [diff] [blame] | 218 | SMDiagnostic Err; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 219 | std::unique_ptr<Module> M; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 220 | Module *mod = nullptr; |
Duncan Sands | 206fc30 | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 221 | Triple TheTriple; |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 222 | |
Duncan Sands | 206fc30 | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 223 | bool SkipModule = MCPU == "help" || |
| 224 | (!MAttrs.empty() && MAttrs.front() == "help"); |
| 225 | |
Jim Grosbach | dad1772 | 2014-04-12 01:34:31 +0000 | [diff] [blame] | 226 | // If user asked for the 'native' CPU, autodetect here. If autodection fails, |
| 227 | // this will set the CPU to an empty string which tells the target to |
| 228 | // pick a basic default. |
| 229 | if (MCPU == "native") |
| 230 | MCPU = sys::getHostCPUName(); |
| 231 | |
Duncan Sands | 206fc30 | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 232 | // If user just wants to list available options, skip module loading |
| 233 | if (!SkipModule) { |
| 234 | M.reset(ParseIRFile(InputFilename, Err, Context)); |
| 235 | mod = M.get(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 236 | if (mod == nullptr) { |
Duncan Sands | 206fc30 | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 237 | Err.print(argv[0], errs()); |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | // If we are supposed to override the target triple, do so now. |
| 242 | if (!TargetTriple.empty()) |
| 243 | mod->setTargetTriple(Triple::normalize(TargetTriple)); |
| 244 | TheTriple = Triple(mod->getTargetTriple()); |
| 245 | } else { |
| 246 | TheTriple = Triple(Triple::normalize(TargetTriple)); |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 247 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | 0f16ea5 | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 249 | if (TheTriple.getTriple().empty()) |
Sebastian Pop | 94441fb | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 250 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
Daniel Dunbar | 0f16ea5 | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 251 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 252 | // Get the target specific parser. |
| 253 | std::string Error; |
| 254 | const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, |
| 255 | Error); |
| 256 | if (!TheTarget) { |
| 257 | errs() << argv[0] << ": " << Error; |
| 258 | return 1; |
Chris Lattner | 5667f0e | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 259 | } |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 260 | |
| 261 | // Package up features to be passed to target/subtarget |
| 262 | std::string FeaturesStr; |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 263 | if (MAttrs.size()) { |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 264 | SubtargetFeatures Features; |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 265 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 266 | Features.AddFeature(MAttrs[i]); |
| 267 | FeaturesStr = Features.getString(); |
| 268 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 269 | |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 270 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 271 | switch (OptLevel) { |
| 272 | default: |
| 273 | errs() << argv[0] << ": invalid optimization level.\n"; |
| 274 | return 1; |
| 275 | case ' ': break; |
| 276 | case '0': OLvl = CodeGenOpt::None; break; |
| 277 | case '1': OLvl = CodeGenOpt::Less; break; |
| 278 | case '2': OLvl = CodeGenOpt::Default; break; |
| 279 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 280 | } |
| 281 | |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 282 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 48fa6ed | 2014-02-21 03:13:54 +0000 | [diff] [blame] | 283 | Options.DisableIntegratedAS = NoIntegratedAssembler; |
Eric Christopher | 472cee3 | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 284 | Options.MCOptions.ShowMCEncoding = ShowMCEncoding; |
| 285 | Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory; |
| 286 | Options.MCOptions.AsmVerbose = AsmVerbose; |
Eric Christopher | eb71972 | 2014-05-20 23:59:50 +0000 | [diff] [blame] | 287 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 288 | std::unique_ptr<TargetMachine> target( |
| 289 | TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr, |
| 290 | Options, RelocModel, CMModel, OLvl)); |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 291 | assert(target.get() && "Could not allocate target machine!"); |
Eric Christopher | a9f3a5c | 2014-05-06 16:29:50 +0000 | [diff] [blame] | 292 | |
| 293 | // If we don't have a module then just exit now. We do this down |
| 294 | // here since the CPU/Feature help is underneath the target machine |
| 295 | // creation. |
| 296 | if (SkipModule) |
| 297 | return 0; |
| 298 | |
| 299 | assert(mod && "Should have exited if we didn't have a module!"); |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 300 | TargetMachine &Target = *target.get(); |
| 301 | |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 302 | if (GenerateSoftFloatCalls) |
| 303 | FloatABIForCalls = FloatABI::Soft; |
| 304 | |
Chad Rosier | 09a06c2 | 2012-07-19 00:11:45 +0000 | [diff] [blame] | 305 | // Figure out where we are going to send the output. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 306 | std::unique_ptr<tool_output_file> Out( |
| 307 | GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0])); |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 308 | if (!Out) return 1; |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 309 | |
Dan Gohman | 4cfccb8 | 2010-05-11 19:57:55 +0000 | [diff] [blame] | 310 | // Build up all of the passes that we want to do to the module. |
| 311 | PassManager PM; |
Daniel Dunbar | c0deed3 | 2009-08-03 17:34:19 +0000 | [diff] [blame] | 312 | |
Bob Wilson | fa59485 | 2012-08-03 21:26:18 +0000 | [diff] [blame] | 313 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
| 314 | TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple); |
Bob Wilson | 4c65c50 | 2012-08-08 20:31:37 +0000 | [diff] [blame] | 315 | if (DisableSimplifyLibCalls) |
Bob Wilson | fa59485 | 2012-08-03 21:26:18 +0000 | [diff] [blame] | 316 | TLI->disableAllFunctions(); |
Bob Wilson | 4c65c50 | 2012-08-08 20:31:37 +0000 | [diff] [blame] | 317 | PM.add(TLI); |
Bob Wilson | fa59485 | 2012-08-03 21:26:18 +0000 | [diff] [blame] | 318 | |
Dan Gohman | 4cfccb8 | 2010-05-11 19:57:55 +0000 | [diff] [blame] | 319 | // Add the target data from the target machine, if it exists, or the module. |
Rafael Espindola | f193902 | 2014-02-21 02:01:42 +0000 | [diff] [blame] | 320 | if (const DataLayout *DL = Target.getDataLayout()) |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 321 | mod->setDataLayout(DL); |
| 322 | PM.add(new DataLayoutPass(mod)); |
Daniel Dunbar | c0deed3 | 2009-08-03 17:34:19 +0000 | [diff] [blame] | 323 | |
Eric Christopher | e6ece1a | 2014-05-15 01:10:50 +0000 | [diff] [blame] | 324 | if (RelaxAll.getNumOccurrences() > 0 && |
| 325 | FileType != TargetMachine::CGFT_ObjectFile) |
| 326 | errs() << argv[0] |
Michael J. Spencer | f695f8f | 2010-07-31 19:57:02 +0000 | [diff] [blame] | 327 | << ": warning: ignoring -mc-relax-all because filetype != obj"; |
Michael J. Spencer | f695f8f | 2010-07-31 19:57:02 +0000 | [diff] [blame] | 328 | |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 329 | { |
| 330 | formatted_raw_ostream FOS(Out->os()); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 331 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 332 | AnalysisID StartAfterID = nullptr; |
| 333 | AnalysisID StopAfterID = nullptr; |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 334 | const PassRegistry *PR = PassRegistry::getPassRegistry(); |
| 335 | if (!StartAfter.empty()) { |
| 336 | const PassInfo *PI = PR->getPassInfo(StartAfter); |
| 337 | if (!PI) { |
| 338 | errs() << argv[0] << ": start-after pass is not registered.\n"; |
| 339 | return 1; |
| 340 | } |
| 341 | StartAfterID = PI->getTypeInfo(); |
| 342 | } |
| 343 | if (!StopAfter.empty()) { |
| 344 | const PassInfo *PI = PR->getPassInfo(StopAfter); |
| 345 | if (!PI) { |
| 346 | errs() << argv[0] << ": stop-after pass is not registered.\n"; |
| 347 | return 1; |
| 348 | } |
| 349 | StopAfterID = PI->getTypeInfo(); |
| 350 | } |
| 351 | |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 352 | // Ask the target to add backend passes as necessary. |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 353 | if (Target.addPassesToEmitFile(PM, FOS, FileType, NoVerify, |
| 354 | StartAfterID, StopAfterID)) { |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 355 | errs() << argv[0] << ": target does not support generation of this" |
| 356 | << " file type!\n"; |
| 357 | return 1; |
| 358 | } |
| 359 | |
Andrew Trick | 1200401 | 2011-04-05 18:54:36 +0000 | [diff] [blame] | 360 | // Before executing passes, print the final values of the LLVM options. |
| 361 | cl::PrintOptionValues(); |
| 362 | |
Duncan Sands | 206fc30 | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 363 | PM.run(*mod); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 364 | } |
Dan Gohman | 4cfccb8 | 2010-05-11 19:57:55 +0000 | [diff] [blame] | 365 | |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 366 | // Declare success. |
| 367 | Out->keep(); |
Chris Lattner | 6dd290a | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 368 | |
| 369 | return 0; |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 370 | } |