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 | |
Benjamin Kramer | d59c5f9 | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | bda1349 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
David Blaikie | 461bf52 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/CommandFlags.inc" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" |
| 21 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
Matthias Braun | bbeb3ea | 2016-05-10 01:32:44 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Matthias Braun | 3c559a0 | 2016-05-10 02:09:32 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Matthias Braun | 6a6190d | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetPassConfig.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 27 | #include "llvm/IR/AutoUpgrade.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
Renato Golin | 3b0d377 | 2016-05-16 14:28:02 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DiagnosticInfo.h" |
| 30 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9f20a4c | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IRPrintingPasses.h" |
| 32 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 33 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Module.h" |
Duncan P. N. Exon Smith | de89228 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 36 | #include "llvm/IRReader/IRReader.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 37 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" |
David Greene | c58d01b | 2010-01-05 01:30:21 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 7259f14 | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 41 | #include "llvm/Support/FileSystem.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 42 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Host.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 44 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 46 | #include "llvm/Support/PluginLoader.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 47 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 48 | #include "llvm/Support/TargetRegistry.h" |
| 49 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 51 | #include "llvm/Support/WithColor.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetMachine.h" |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 53 | #include "llvm/Transforms/Utils/Cloning.h" |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 54 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 55 | using namespace llvm; |
| 56 | |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 57 | // General options for llc. Other pass-specific options are specified |
| 58 | // within the corresponding llc passes, and target-specific options |
| 59 | // and back-end code generation options are specified with the target machine. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 60 | // |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 62 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 63 | |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 64 | static cl::opt<std::string> |
Matthias Braun | f632627 | 2017-06-06 20:06:57 +0000 | [diff] [blame] | 65 | InputLanguage("x", cl::desc("Input language ('ir' or 'mir')")); |
| 66 | |
| 67 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 68 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 69 | |
Peter Collingbourne | 9ffe073 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 70 | static cl::opt<std::string> |
| 71 | SplitDwarfOutputFile("split-dwarf-output", |
| 72 | cl::desc(".dwo output filename"), |
| 73 | cl::value_desc("filename")); |
| 74 | |
Jakob Stoklund Olesen | 8820928 | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 75 | static cl::opt<unsigned> |
| 76 | TimeCompilations("time-compilations", cl::Hidden, cl::init(1u), |
| 77 | cl::value_desc("N"), |
| 78 | cl::desc("Repeat compilation N times for timing")); |
| 79 | |
Rafael Espindola | 9f5066c | 2014-02-21 03:13:54 +0000 | [diff] [blame] | 80 | static cl::opt<bool> |
| 81 | NoIntegratedAssembler("no-integrated-as", cl::Hidden, |
| 82 | cl::desc("Disable integrated assembler")); |
| 83 | |
Nirav Dave | aee0f14 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 84 | static cl::opt<bool> |
Nirav Dave | dcf0053 | 2016-07-13 14:20:41 +0000 | [diff] [blame] | 85 | PreserveComments("preserve-as-comments", cl::Hidden, |
Nirav Dave | 72d33ec | 2016-07-12 15:32:36 +0000 | [diff] [blame] | 86 | cl::desc("Preserve Comments in outputted assembly"), |
| 87 | cl::init(true)); |
Nirav Dave | aee0f14 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 88 | |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 89 | // Determine optimization level. |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 90 | static cl::opt<char> |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 91 | OptLevel("O", |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 92 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 93 | "(default = '-O2')"), |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 94 | cl::Prefix, |
| 95 | cl::ZeroOrMore, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 96 | cl::init(' ')); |
Chris Lattner | 178e0c4 | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 97 | |
Chris Lattner | f33b8663 | 2005-12-16 04:59:57 +0000 | [diff] [blame] | 98 | static cl::opt<std::string> |
Chris Lattner | be19383 | 2005-12-16 05:19:55 +0000 | [diff] [blame] | 99 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Chris Lattner | 178e0c4 | 2005-11-08 02:12:17 +0000 | [diff] [blame] | 100 | |
David Blaikie | 9c1b0af | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 101 | static cl::opt<std::string> SplitDwarfFile( |
| 102 | "split-dwarf-file", |
| 103 | cl::desc( |
| 104 | "Specify the name of the .dwo file to encode in the DWARF output")); |
| 105 | |
Eric Christopher | 9a37986 | 2014-05-21 21:05:05 +0000 | [diff] [blame] | 106 | static cl::opt<bool> NoVerify("disable-verify", cl::Hidden, |
| 107 | cl::desc("Do not verify input module")); |
Reid Spencer | 4418c2b | 2005-07-28 02:25:30 +0000 | [diff] [blame] | 108 | |
Eric Christopher | 9a37986 | 2014-05-21 21:05:05 +0000 | [diff] [blame] | 109 | static cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls", |
| 110 | cl::desc("Disable simplify-libcalls")); |
Chad Rosier | 35907e9 | 2012-08-21 16:15:24 +0000 | [diff] [blame] | 111 | |
Eric Christopher | d1b5bda | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 112 | static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden, |
| 113 | cl::desc("Show encoding in .s output")); |
| 114 | |
| 115 | static cl::opt<bool> EnableDwarfDirectory( |
| 116 | "enable-dwarf-directory", cl::Hidden, |
| 117 | cl::desc("Use .file directives with an explicit directory.")); |
| 118 | |
| 119 | static cl::opt<bool> AsmVerbose("asm-verbose", |
| 120 | cl::desc("Add comments to directives."), |
| 121 | cl::init(true)); |
| 122 | |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 123 | static cl::opt<bool> |
| 124 | CompileTwice("compile-twice", cl::Hidden, |
| 125 | cl::desc("Run everything twice, re-using the same pass " |
Sanjay Patel | 322ee9e | 2015-12-07 19:21:39 +0000 | [diff] [blame] | 126 | "manager and verify the result is the same."), |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 127 | cl::init(false)); |
| 128 | |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 129 | static cl::opt<bool> DiscardValueNames( |
| 130 | "discard-value-names", |
| 131 | cl::desc("Discard names from Value (other than GlobalValue)."), |
| 132 | cl::init(false), cl::Hidden); |
| 133 | |
Saleem Abdulrasool | 50116a2 | 2017-01-05 05:56:39 +0000 | [diff] [blame] | 134 | static cl::list<std::string> IncludeDirs("I", cl::desc("include search path")); |
| 135 | |
Adam Nemet | 3e42698 | 2017-01-25 23:55:59 +0000 | [diff] [blame] | 136 | static cl::opt<bool> PassRemarksWithHotness( |
| 137 | "pass-remarks-with-hotness", |
| 138 | cl::desc("With PGO, include profile count in optimization remarks"), |
| 139 | cl::Hidden); |
| 140 | |
Brian Gesiak | e330528 | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 141 | static cl::opt<unsigned> PassRemarksHotnessThreshold( |
| 142 | "pass-remarks-hotness-threshold", |
| 143 | cl::desc("Minimum profile count required for an optimization remark to be output"), |
| 144 | cl::Hidden); |
| 145 | |
Adam Nemet | c8168e7 | 2017-01-26 00:39:51 +0000 | [diff] [blame] | 146 | static cl::opt<std::string> |
| 147 | RemarksFilename("pass-remarks-output", |
| 148 | cl::desc("YAML output filename for pass remarks"), |
| 149 | cl::value_desc("filename")); |
| 150 | |
Quentin Colombet | aef5f64 | 2016-06-10 00:52:10 +0000 | [diff] [blame] | 151 | namespace { |
| 152 | static ManagedStatic<std::vector<std::string>> RunPassNames; |
| 153 | |
| 154 | struct RunPassOption { |
| 155 | void operator=(const std::string &Val) const { |
| 156 | if (Val.empty()) |
| 157 | return; |
| 158 | SmallVector<StringRef, 8> PassNames; |
| 159 | StringRef(Val).split(PassNames, ',', -1, false); |
| 160 | for (auto PassName : PassNames) |
| 161 | RunPassNames->push_back(PassName); |
| 162 | } |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | static RunPassOption RunPassOpt; |
| 167 | |
| 168 | static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass( |
| 169 | "run-pass", |
| 170 | cl::desc("Run compiler only for specified passes (comma separated list)"), |
| 171 | cl::value_desc("pass-name"), cl::ZeroOrMore, cl::location(RunPassOpt)); |
| 172 | |
Eric Christopher | d1b5bda | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 173 | static int compileModule(char **, LLVMContext &); |
Jakob Stoklund Olesen | 8820928 | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 174 | |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 175 | static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName, |
| 176 | Triple::OSType OS, |
| 177 | const char *ProgName) { |
Dan Gohman | 18cde6d | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 178 | // If we don't yet have an output filename, make one. |
| 179 | if (OutputFilename.empty()) { |
| 180 | if (InputFilename == "-") |
| 181 | OutputFilename = "-"; |
| 182 | else { |
Craig Topper | 847998f0d | 2014-08-30 16:48:22 +0000 | [diff] [blame] | 183 | // If InputFilename ends in .bc or .ll, remove it. |
| 184 | StringRef IFN = InputFilename; |
| 185 | if (IFN.endswith(".bc") || IFN.endswith(".ll")) |
| 186 | OutputFilename = IFN.drop_back(3); |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 187 | else if (IFN.endswith(".mir")) |
| 188 | OutputFilename = IFN.drop_back(4); |
Craig Topper | 847998f0d | 2014-08-30 16:48:22 +0000 | [diff] [blame] | 189 | else |
| 190 | OutputFilename = IFN; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 191 | |
Dan Gohman | 18cde6d | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 192 | switch (FileType) { |
Dan Gohman | 18cde6d | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 193 | case TargetMachine::CGFT_AssemblyFile: |
| 194 | if (TargetName[0] == 'c') { |
| 195 | if (TargetName[1] == 0) |
| 196 | OutputFilename += ".cbe.c"; |
| 197 | else if (TargetName[1] == 'p' && TargetName[2] == 'p') |
| 198 | OutputFilename += ".cpp"; |
| 199 | else |
| 200 | OutputFilename += ".s"; |
| 201 | } else |
| 202 | OutputFilename += ".s"; |
| 203 | break; |
| 204 | case TargetMachine::CGFT_ObjectFile: |
| 205 | if (OS == Triple::Win32) |
| 206 | OutputFilename += ".obj"; |
| 207 | else |
| 208 | OutputFilename += ".o"; |
| 209 | break; |
| 210 | case TargetMachine::CGFT_Null: |
| 211 | OutputFilename += ".null"; |
| 212 | break; |
| 213 | } |
Dan Gohman | ed3e8b4 | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 215 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 216 | |
Dan Gohman | 18cde6d | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 217 | // Decide if we need "binary" output. |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 218 | bool Binary = false; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 219 | switch (FileType) { |
Chris Lattner | 211edae | 2010-02-02 21:06:45 +0000 | [diff] [blame] | 220 | case TargetMachine::CGFT_AssemblyFile: |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 221 | break; |
Chris Lattner | 211edae | 2010-02-02 21:06:45 +0000 | [diff] [blame] | 222 | case TargetMachine::CGFT_ObjectFile: |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 223 | case TargetMachine::CGFT_Null: |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 224 | Binary = true; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 225 | break; |
| 226 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 227 | |
Dan Gohman | 18cde6d | 2010-08-18 17:55:15 +0000 | [diff] [blame] | 228 | // Open the file. |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 229 | std::error_code EC; |
Rafael Espindola | c1b49b5 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 230 | sys::fs::OpenFlags OpenFlags = sys::fs::F_None; |
Rafael Espindola | ac69459 | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 231 | if (!Binary) |
| 232 | OpenFlags |= sys::fs::F_Text; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 233 | auto FDOut = llvm::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags); |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 234 | if (EC) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 235 | WithColor::error() << EC.message() << '\n'; |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 236 | return nullptr; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 237 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 238 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 239 | return FDOut; |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 240 | } |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 241 | |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 242 | struct LLCDiagnosticHandler : public DiagnosticHandler { |
| 243 | bool *HasError; |
| 244 | LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {} |
| 245 | bool handleDiagnostics(const DiagnosticInfo &DI) override { |
| 246 | if (DI.getSeverity() == DS_Error) |
| 247 | *HasError = true; |
Renato Golin | 3b0d377 | 2016-05-16 14:28:02 +0000 | [diff] [blame] | 248 | |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 249 | if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) |
| 250 | if (!Remark->isEnabled()) |
| 251 | return true; |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 252 | |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 253 | DiagnosticPrinterRawOStream DP(errs()); |
| 254 | errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; |
| 255 | DI.print(DP); |
| 256 | errs() << "\n"; |
| 257 | return true; |
| 258 | } |
| 259 | }; |
Renato Golin | 3b0d377 | 2016-05-16 14:28:02 +0000 | [diff] [blame] | 260 | |
Sanne Wouda | 5f9d8e2 | 2017-02-03 11:14:39 +0000 | [diff] [blame] | 261 | static void InlineAsmDiagHandler(const SMDiagnostic &SMD, void *Context, |
Sanne Wouda | ff6c8c5 | 2017-02-28 10:34:48 +0000 | [diff] [blame] | 262 | unsigned LocCookie) { |
Sanne Wouda | 5f9d8e2 | 2017-02-03 11:14:39 +0000 | [diff] [blame] | 263 | bool *HasError = static_cast<bool *>(Context); |
| 264 | if (SMD.getKind() == SourceMgr::DK_Error) |
| 265 | *HasError = true; |
| 266 | |
| 267 | SMD.print(nullptr, errs()); |
Sanne Wouda | ff6c8c5 | 2017-02-28 10:34:48 +0000 | [diff] [blame] | 268 | |
| 269 | // For testing purposes, we print the LocCookie here. |
| 270 | if (LocCookie) |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 271 | WithColor::note() << "!srcloc = " << LocCookie << "\n"; |
Sanne Wouda | 5f9d8e2 | 2017-02-03 11:14:39 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 274 | // main - Entry point for the llc compiler. |
| 275 | // |
| 276 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 277 | InitLLVM X(argc, argv); |
David Greene | c58d01b | 2010-01-05 01:30:21 +0000 | [diff] [blame] | 278 | |
| 279 | // Enable debug stream buffering. |
| 280 | EnableDebugBuffering = true; |
| 281 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 282 | LLVMContext Context; |
Chris Lattner | 364d120 | 2004-02-19 20:32:39 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 2b991bb | 2009-09-03 05:47:22 +0000 | [diff] [blame] | 284 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 285 | InitializeAllTargets(); |
Evan Cheng | e78085a | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 286 | InitializeAllTargetMCs(); |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 287 | InitializeAllAsmPrinters(); |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 288 | InitializeAllAsmParsers(); |
Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 289 | |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 290 | // Initialize codegen and IR passes used by llc so that the -print-after, |
| 291 | // -print-before, and -stop-after options work. |
| 292 | PassRegistry *Registry = PassRegistry::getPassRegistry(); |
| 293 | initializeCore(*Registry); |
| 294 | initializeCodeGen(*Registry); |
| 295 | initializeLoopStrengthReducePass(*Registry); |
| 296 | initializeLowerIntrinsicsPass(*Registry); |
Hans Wennborg | 5765d84 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 297 | initializeEntryExitInstrumenterPass(*Registry); |
| 298 | initializePostInlineEntryExitInstrumenterPass(*Registry); |
Wei Mi | 302c35d | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 299 | initializeUnreachableBlockElimLegacyPassPass(*Registry); |
Davide Italiano | 1e15122 | 2016-12-06 23:49:58 +0000 | [diff] [blame] | 300 | initializeConstantHoistingLegacyPassPass(*Registry); |
Matt Arsenault | 878b099 | 2017-01-17 05:11:25 +0000 | [diff] [blame] | 301 | initializeScalarOpts(*Registry); |
| 302 | initializeVectorization(*Registry); |
Ayman Musa | eadb58f | 2017-05-15 11:30:54 +0000 | [diff] [blame] | 303 | initializeScalarizeMaskedMemIntrinPass(*Registry); |
Amara Emerson | 0dd30f8 | 2017-05-10 09:42:49 +0000 | [diff] [blame] | 304 | initializeExpandReductionsPass(*Registry); |
Rafael Espindola | 275c85f | 2012-06-26 21:33:36 +0000 | [diff] [blame] | 305 | |
Matthias Braun | 3310b59 | 2017-06-02 23:01:42 +0000 | [diff] [blame] | 306 | // Initialize debugging passes. |
| 307 | initializeScavengerTestPass(*Registry); |
| 308 | |
Chandler Carruth | 4039313 | 2011-07-22 07:50:48 +0000 | [diff] [blame] | 309 | // Register the target printer for --version. |
| 310 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 311 | |
Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 312 | cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 313 | |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 314 | Context.setDiscardValueNames(DiscardValueNames); |
| 315 | |
Renato Golin | 3b0d377 | 2016-05-16 14:28:02 +0000 | [diff] [blame] | 316 | // Set a diagnostic handler that doesn't exit on the first error |
| 317 | bool HasError = false; |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 318 | Context.setDiagnosticHandler( |
| 319 | llvm::make_unique<LLCDiagnosticHandler>(&HasError)); |
Sanne Wouda | 5f9d8e2 | 2017-02-03 11:14:39 +0000 | [diff] [blame] | 320 | Context.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, &HasError); |
| 321 | |
Adam Nemet | 3e42698 | 2017-01-25 23:55:59 +0000 | [diff] [blame] | 322 | if (PassRemarksWithHotness) |
Brian Gesiak | 8e8ec78 | 2017-06-30 18:13:59 +0000 | [diff] [blame] | 323 | Context.setDiagnosticsHotnessRequested(true); |
Renato Golin | 3b0d377 | 2016-05-16 14:28:02 +0000 | [diff] [blame] | 324 | |
Brian Gesiak | e330528 | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 325 | if (PassRemarksHotnessThreshold) |
| 326 | Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold); |
| 327 | |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 328 | std::unique_ptr<ToolOutputFile> YamlFile; |
Adam Nemet | c8168e7 | 2017-01-26 00:39:51 +0000 | [diff] [blame] | 329 | if (RemarksFilename != "") { |
| 330 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 331 | YamlFile = |
| 332 | llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None); |
Adam Nemet | c8168e7 | 2017-01-26 00:39:51 +0000 | [diff] [blame] | 333 | if (EC) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 334 | WithColor::error(errs(), argv[0]) << EC.message() << '\n'; |
Adam Nemet | c8168e7 | 2017-01-26 00:39:51 +0000 | [diff] [blame] | 335 | return 1; |
| 336 | } |
| 337 | Context.setDiagnosticsOutputFile( |
| 338 | llvm::make_unique<yaml::Output>(YamlFile->os())); |
| 339 | } |
| 340 | |
Matthias Braun | f632627 | 2017-06-06 20:06:57 +0000 | [diff] [blame] | 341 | if (InputLanguage != "" && InputLanguage != "ir" && |
| 342 | InputLanguage != "mir") { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 343 | WithColor::error(errs(), argv[0]) |
| 344 | << "input language must be '', 'IR' or 'MIR'\n"; |
Matthias Braun | f632627 | 2017-06-06 20:06:57 +0000 | [diff] [blame] | 345 | return 1; |
| 346 | } |
| 347 | |
Jakob Stoklund Olesen | 8820928 | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 348 | // Compile the module TimeCompilations times to give better compile time |
| 349 | // metrics. |
| 350 | for (unsigned I = TimeCompilations; I; --I) |
| 351 | if (int RetVal = compileModule(argv, Context)) |
| 352 | return RetVal; |
Adam Nemet | c8168e7 | 2017-01-26 00:39:51 +0000 | [diff] [blame] | 353 | |
| 354 | if (YamlFile) |
| 355 | YamlFile->keep(); |
Jakob Stoklund Olesen | 8820928 | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Matthias Braun | 4bd6d7f | 2016-07-16 02:24:15 +0000 | [diff] [blame] | 359 | static bool addPass(PassManagerBase &PM, const char *argv0, |
| 360 | StringRef PassName, TargetPassConfig &TPC) { |
Quentin Colombet | 2a400a7 | 2017-04-01 01:26:24 +0000 | [diff] [blame] | 361 | if (PassName == "none") |
Matthias Braun | 3346c15 | 2016-07-16 02:24:59 +0000 | [diff] [blame] | 362 | return false; |
| 363 | |
Quentin Colombet | 2a400a7 | 2017-04-01 01:26:24 +0000 | [diff] [blame] | 364 | const PassRegistry *PR = PassRegistry::getPassRegistry(); |
| 365 | const PassInfo *PI = PR->getPassInfo(PassName); |
| 366 | if (!PI) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 367 | WithColor::error(errs(), argv0) |
| 368 | << "run-pass " << PassName << " is not registered.\n"; |
Quentin Colombet | 2a400a7 | 2017-04-01 01:26:24 +0000 | [diff] [blame] | 369 | return true; |
| 370 | } |
Matthias Braun | 4bd6d7f | 2016-07-16 02:24:15 +0000 | [diff] [blame] | 371 | |
| 372 | Pass *P; |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 373 | if (PI->getNormalCtor()) |
Matthias Braun | 4bd6d7f | 2016-07-16 02:24:15 +0000 | [diff] [blame] | 374 | P = PI->getNormalCtor()(); |
| 375 | else { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 376 | WithColor::error(errs(), argv0) |
| 377 | << "cannot create pass: " << PI->getPassName() << "\n"; |
Matthias Braun | 4bd6d7f | 2016-07-16 02:24:15 +0000 | [diff] [blame] | 378 | return true; |
| 379 | } |
| 380 | std::string Banner = std::string("After ") + std::string(P->getPassName()); |
| 381 | PM.add(P); |
| 382 | TPC.printAndVerify(Banner); |
| 383 | |
| 384 | return false; |
| 385 | } |
| 386 | |
Jakob Stoklund Olesen | 8820928 | 2012-11-30 21:42:47 +0000 | [diff] [blame] | 387 | static int compileModule(char **argv, LLVMContext &Context) { |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 388 | // Load the module to be compiled... |
Dan Gohman | 778b06b | 2009-09-02 19:35:19 +0000 | [diff] [blame] | 389 | SMDiagnostic Err; |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 390 | std::unique_ptr<Module> M; |
Alex Lorenz | a15d888 | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 391 | std::unique_ptr<MIRParser> MIR; |
Duncan Sands | f6ce8ea | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 392 | Triple TheTriple; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 393 | |
Duncan Sands | f6ce8ea | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 394 | bool SkipModule = MCPU == "help" || |
| 395 | (!MAttrs.empty() && MAttrs.front() == "help"); |
| 396 | |
| 397 | // If user just wants to list available options, skip module loading |
| 398 | if (!SkipModule) { |
Matthias Braun | f632627 | 2017-06-06 20:06:57 +0000 | [diff] [blame] | 399 | if (InputLanguage == "mir" || |
| 400 | (InputLanguage == "" && StringRef(InputFilename).endswith(".mir"))) { |
Alex Lorenz | a15d888 | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 401 | MIR = createMIRParserFromFile(InputFilename, Err, Context); |
Diana Picus | 1063f93 | 2016-06-09 10:31:05 +0000 | [diff] [blame] | 402 | if (MIR) |
Matthias Braun | 2144c52 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 403 | M = MIR->parseIRModule(); |
Alex Lorenz | a15d888 | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 404 | } else |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 405 | M = parseIRFile(InputFilename, Err, Context, false); |
Craig Topper | 4fca0dd | 2014-12-11 07:04:52 +0000 | [diff] [blame] | 406 | if (!M) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 407 | Err.print(argv[0], WithColor::error(errs(), argv[0])); |
Duncan Sands | f6ce8ea | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | // If we are supposed to override the target triple, do so now. |
| 412 | if (!TargetTriple.empty()) |
Craig Topper | 4fca0dd | 2014-12-11 07:04:52 +0000 | [diff] [blame] | 413 | M->setTargetTriple(Triple::normalize(TargetTriple)); |
| 414 | TheTriple = Triple(M->getTargetTriple()); |
Duncan Sands | f6ce8ea | 2012-06-27 16:23:48 +0000 | [diff] [blame] | 415 | } else { |
| 416 | TheTriple = Triple(Triple::normalize(TargetTriple)); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 417 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 418 | |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 419 | if (TheTriple.getTriple().empty()) |
Sebastian Pop | 0173864 | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 420 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 421 | |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 422 | // Get the target specific parser. |
| 423 | std::string Error; |
| 424 | const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, |
| 425 | Error); |
| 426 | if (!TheTarget) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 427 | WithColor::error(errs(), argv[0]) << Error; |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 428 | return 1; |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 430 | |
Akira Hatanaka | 4f28a76 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 431 | std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr(); |
Craig Topper | 9a7c003 | 2015-03-31 05:52:57 +0000 | [diff] [blame] | 432 | |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 433 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 434 | switch (OptLevel) { |
| 435 | default: |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 436 | WithColor::error(errs(), argv[0]) << "invalid optimization level.\n"; |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 437 | return 1; |
| 438 | case ' ': break; |
| 439 | case '0': OLvl = CodeGenOpt::None; break; |
| 440 | case '1': OLvl = CodeGenOpt::Less; break; |
| 441 | case '2': OLvl = CodeGenOpt::Default; break; |
| 442 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 443 | } |
| 444 | |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 445 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 9f5066c | 2014-02-21 03:13:54 +0000 | [diff] [blame] | 446 | Options.DisableIntegratedAS = NoIntegratedAssembler; |
Eric Christopher | d1b5bda | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 447 | Options.MCOptions.ShowMCEncoding = ShowMCEncoding; |
| 448 | Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory; |
| 449 | Options.MCOptions.AsmVerbose = AsmVerbose; |
Nirav Dave | 72d33ec | 2016-07-12 15:32:36 +0000 | [diff] [blame] | 450 | Options.MCOptions.PreserveAsmComments = PreserveComments; |
Saleem Abdulrasool | 50116a2 | 2017-01-05 05:56:39 +0000 | [diff] [blame] | 451 | Options.MCOptions.IASSearchPaths = IncludeDirs; |
David Blaikie | 9c1b0af | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 452 | Options.MCOptions.SplitDwarfFile = SplitDwarfFile; |
Eric Christopher | 4f6d26d | 2014-05-20 23:59:50 +0000 | [diff] [blame] | 453 | |
Rafael Espindola | 9aafb85 | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 454 | std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine( |
| 455 | TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(), |
| 456 | getCodeModel(), OLvl)); |
Akira Hatanaka | 4f28a76 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 457 | |
Craig Topper | e34c578 | 2014-12-12 07:52:06 +0000 | [diff] [blame] | 458 | assert(Target && "Could not allocate target machine!"); |
Eric Christopher | bfc3f30 | 2014-05-06 16:29:50 +0000 | [diff] [blame] | 459 | |
| 460 | // If we don't have a module then just exit now. We do this down |
| 461 | // here since the CPU/Feature help is underneath the target machine |
| 462 | // creation. |
| 463 | if (SkipModule) |
| 464 | return 0; |
| 465 | |
Craig Topper | 4fca0dd | 2014-12-11 07:04:52 +0000 | [diff] [blame] | 466 | assert(M && "Should have exited if we didn't have a module!"); |
Eric Christopher | 0552d51 | 2015-05-12 01:26:05 +0000 | [diff] [blame] | 467 | if (FloatABIForCalls != FloatABI::Default) |
| 468 | Options.FloatABIType = FloatABIForCalls; |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 469 | |
Chad Rosier | 7a43e30 | 2012-07-19 00:11:45 +0000 | [diff] [blame] | 470 | // Figure out where we are going to send the output. |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 471 | std::unique_ptr<ToolOutputFile> Out = |
Craig Topper | 2a4988e | 2014-12-12 07:52:00 +0000 | [diff] [blame] | 472 | GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]); |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 473 | if (!Out) return 1; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 474 | |
Peter Collingbourne | 9ffe073 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 475 | std::unique_ptr<ToolOutputFile> DwoOut; |
| 476 | if (!SplitDwarfOutputFile.empty()) { |
| 477 | std::error_code EC; |
| 478 | DwoOut = llvm::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC, |
| 479 | sys::fs::F_None); |
| 480 | if (EC) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 481 | WithColor::error(errs(), argv[0]) << EC.message() << '\n'; |
Peter Collingbourne | 9ffe073 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 482 | return 1; |
| 483 | } |
| 484 | } |
| 485 | |
Dan Gohman | 99dca4f | 2010-05-11 19:57:55 +0000 | [diff] [blame] | 486 | // Build up all of the passes that we want to do to the module. |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 487 | legacy::PassManager PM; |
Daniel Dunbar | 4e02eb0 | 2009-08-03 17:34:19 +0000 | [diff] [blame] | 488 | |
Bob Wilson | 772af92 | 2012-08-03 21:26:18 +0000 | [diff] [blame] | 489 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
Chandler Carruth | 6f409cb | 2015-01-24 02:06:09 +0000 | [diff] [blame] | 490 | TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple())); |
Chandler Carruth | eeeec3c | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 491 | |
| 492 | // The -disable-simplify-libcalls flag actually disables all builtin optzns. |
Bob Wilson | 5f91a99 | 2012-08-08 20:31:37 +0000 | [diff] [blame] | 493 | if (DisableSimplifyLibCalls) |
Chandler Carruth | 6f409cb | 2015-01-24 02:06:09 +0000 | [diff] [blame] | 494 | TLII.disableAllFunctions(); |
| 495 | PM.add(new TargetLibraryInfoWrapperPass(TLII)); |
Bob Wilson | 772af92 | 2012-08-03 21:26:18 +0000 | [diff] [blame] | 496 | |
Dan Gohman | 99dca4f | 2010-05-11 19:57:55 +0000 | [diff] [blame] | 497 | // Add the target data from the target machine, if it exists, or the module. |
Mehdi Amini | 15e1e7f | 2015-07-24 16:04:22 +0000 | [diff] [blame] | 498 | M->setDataLayout(Target->createDataLayout()); |
Daniel Dunbar | 4e02eb0 | 2009-08-03 17:34:19 +0000 | [diff] [blame] | 499 | |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 500 | // This needs to be done after setting datalayout since it calls verifier |
| 501 | // to check debug info whereas verifier relies on correct datalayout. |
| 502 | UpgradeDebugInfo(*M); |
| 503 | |
| 504 | // Verify module immediately to catch problems before doInitialization() is |
| 505 | // called on any passes. |
| 506 | if (!NoVerify && verifyModule(*M, &errs())) { |
Jonas Devlieghere | 7cde7d9 | 2018-06-23 19:04:10 +0000 | [diff] [blame] | 507 | std::string Prefix = |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 508 | (Twine(argv[0]) + Twine(": ") + Twine(InputFilename)).str(); |
| 509 | WithColor::error(errs(), Prefix) << "input module is broken!\n"; |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 510 | return 1; |
| 511 | } |
| 512 | |
Akira Hatanaka | 6f49135 | 2015-05-26 20:17:20 +0000 | [diff] [blame] | 513 | // Override function attributes based on CPUStr, FeaturesStr, and command line |
| 514 | // flags. |
| 515 | setFunctionAttributes(CPUStr, FeaturesStr, *M); |
Akira Hatanaka | e6f0494 | 2015-05-06 23:54:14 +0000 | [diff] [blame] | 516 | |
Eric Christopher | 475ac5d | 2014-05-15 01:10:50 +0000 | [diff] [blame] | 517 | if (RelaxAll.getNumOccurrences() > 0 && |
| 518 | FileType != TargetMachine::CGFT_ObjectFile) |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 519 | WithColor::warning(errs(), argv[0]) |
| 520 | << ": warning: ignoring -mc-relax-all because filetype != obj"; |
Michael J. Spencer | 7c380ed | 2010-07-31 19:57:02 +0000 | [diff] [blame] | 521 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 522 | { |
Rafael Espindola | c98092e | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 523 | raw_pwrite_stream *OS = &Out->os(); |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 524 | |
| 525 | // Manually do the buffering rather than using buffer_ostream, |
| 526 | // so we can memcmp the contents in CompileTwice mode |
| 527 | SmallVector<char, 0> Buffer; |
| 528 | std::unique_ptr<raw_svector_ostream> BOS; |
| 529 | if ((FileType != TargetMachine::CGFT_AssemblyFile && |
| 530 | !Out->os().supportsSeeking()) || |
| 531 | CompileTwice) { |
| 532 | BOS = make_unique<raw_svector_ostream>(Buffer); |
Rafael Espindola | c98092e | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 533 | OS = BOS.get(); |
| 534 | } |
| 535 | |
Matthias Braun | e6c9b7e | 2017-06-06 00:26:24 +0000 | [diff] [blame] | 536 | const char *argv0 = argv[0]; |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 537 | LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine&>(*Target); |
| 538 | MachineModuleInfo *MMI = new MachineModuleInfo(&LLVMTM); |
Quentin Colombet | 6131fb5 | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 539 | |
| 540 | // Construct a custom pass pipeline that starts after instruction |
| 541 | // selection. |
| 542 | if (!RunPassNames->empty()) { |
| 543 | if (!MIR) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 544 | WithColor::warning(errs(), argv[0]) |
| 545 | << "run-pass is for .mir file only.\n"; |
Quentin Colombet | 6131fb5 | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 546 | return 1; |
| 547 | } |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 548 | TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM); |
Quentin Colombet | 6131fb5 | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 549 | if (TPC.hasLimitedCodeGenPipeline()) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 550 | WithColor::warning(errs(), argv[0]) |
| 551 | << "run-pass cannot be used with " |
| 552 | << TPC.getLimitedCodeGenPipelineReason(" and ") << ".\n"; |
Quentin Colombet | 6131fb5 | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | TPC.setDisableVerify(NoVerify); |
| 557 | PM.add(&TPC); |
| 558 | PM.add(MMI); |
| 559 | TPC.printAndVerify(""); |
| 560 | for (const std::string &RunPassName : *RunPassNames) { |
| 561 | if (addPass(PM, argv0, RunPassName, TPC)) |
| 562 | return 1; |
| 563 | } |
| 564 | TPC.setInitialized(); |
| 565 | PM.add(createPrintMIRPass(*OS)); |
| 566 | PM.add(createFreeMachineFunctionPass()); |
Peter Collingbourne | 9ffe073 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 567 | } else if (Target->addPassesToEmitFile(PM, *OS, |
| 568 | DwoOut ? &DwoOut->os() : nullptr, |
| 569 | FileType, NoVerify, MMI)) { |
Jonas Devlieghere | 3042921 | 2018-06-23 16:51:10 +0000 | [diff] [blame] | 570 | WithColor::warning(errs(), argv[0]) |
| 571 | << "target does not support generation of this" |
| 572 | << " file type!\n"; |
Matthias Braun | e6c9b7e | 2017-06-06 00:26:24 +0000 | [diff] [blame] | 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | if (MIR) { |
Quentin Colombet | f514aa4 | 2017-07-31 18:31:04 +0000 | [diff] [blame] | 577 | assert(MMI && "Forgot to create MMI?"); |
Matthias Braun | 2144c52 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 578 | if (MIR->parseMachineFunctions(*M, *MMI)) |
| 579 | return 1; |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Andrew Trick | ce96902 | 2011-04-05 18:54:36 +0000 | [diff] [blame] | 582 | // Before executing passes, print the final values of the LLVM options. |
| 583 | cl::PrintOptionValues(); |
| 584 | |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 585 | // If requested, run the pass manager over the same module again, |
| 586 | // to catch any bugs due to persistent state in the passes. Note that |
| 587 | // opt has the same functionality, so it may be worth abstracting this out |
| 588 | // in the future. |
| 589 | SmallVector<char, 0> CompileTwiceBuffer; |
| 590 | if (CompileTwice) { |
Rafael Espindola | 22b7724 | 2018-02-14 19:50:40 +0000 | [diff] [blame] | 591 | std::unique_ptr<Module> M2(llvm::CloneModule(*M)); |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 592 | PM.run(*M2); |
| 593 | CompileTwiceBuffer = Buffer; |
| 594 | Buffer.clear(); |
| 595 | } |
| 596 | |
Craig Topper | 4fca0dd | 2014-12-11 07:04:52 +0000 | [diff] [blame] | 597 | PM.run(*M); |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 598 | |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 599 | auto HasError = |
| 600 | ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError; |
| 601 | if (*HasError) |
Diana Picus | c412e39 | 2016-06-23 09:49:56 +0000 | [diff] [blame] | 602 | return 1; |
Renato Golin | 3b0d377 | 2016-05-16 14:28:02 +0000 | [diff] [blame] | 603 | |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 604 | // Compare the two outputs and make sure they're the same |
| 605 | if (CompileTwice) { |
| 606 | if (Buffer.size() != CompileTwiceBuffer.size() || |
| 607 | (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) != |
| 608 | 0)) { |
| 609 | errs() |
| 610 | << "Running the pass manager twice changed the output.\n" |
| 611 | "Writing the result of the second run to the specified output\n" |
| 612 | "To generate the one-run comparison binary, just run without\n" |
| 613 | "the compile-twice option\n"; |
| 614 | Out->os() << Buffer; |
| 615 | Out->keep(); |
| 616 | return 1; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (BOS) { |
| 621 | Out->os() << Buffer; |
| 622 | } |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 623 | } |
Dan Gohman | 99dca4f | 2010-05-11 19:57:55 +0000 | [diff] [blame] | 624 | |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 625 | // Declare success. |
| 626 | Out->keep(); |
Peter Collingbourne | 9ffe073 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 627 | if (DwoOut) |
| 628 | DwoOut->keep(); |
Chris Lattner | 1a73540 | 2007-05-06 04:55:19 +0000 | [diff] [blame] | 629 | |
| 630 | return 0; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 631 | } |