Chris Lattner | 76351aa | 2004-04-02 05:06:57 +0000 | [diff] [blame] | 1 | //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// |
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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | // Optimizations may be specified an arbitrary number of times on the command |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 11 | // line, They are run in the order specified. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | 0eafc31 | 2001-10-18 06:05:15 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 15 | #include "BreakpointPrinter.h" |
Vedant Kumar | a4afd1f | 2018-07-24 00:41:28 +0000 | [diff] [blame] | 16 | #include "Debugify.h" |
Chandler Carruth | 4e728be | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 17 | #include "NewPMDriver.h" |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 18 | #include "PassPrinters.h" |
Chris Lattner | 2a66aca | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 3251e81 | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopPass.h" |
| 23 | #include "llvm/Analysis/RegionPass.h" |
Chandler Carruth | bda1349 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | a6a87b5 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | e2dc71d | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 26 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
David Blaikie | 461bf52 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/CommandFlags.inc" |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/TargetPassConfig.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 29 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
Duncan P. N. Exon Smith | de89228 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 8a5351f | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 4e728be | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 33 | #include "llvm/IR/LLVMContext.h" |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 34 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | f7591dd | 2014-03-04 12:32:42 +0000 | [diff] [blame] | 35 | #include "llvm/IR/LegacyPassNameParser.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Module.h" |
Chandler Carruth | 56e1394 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 38 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 1decd56 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 39 | #include "llvm/InitializePasses.h" |
Jakub Staszak | 446991d | 2013-01-10 21:56:40 +0000 | [diff] [blame] | 40 | #include "llvm/LinkAllIR.h" |
Chandler Carruth | 90230c8 | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 41 | #include "llvm/LinkAllPasses.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 42 | #include "llvm/MC/SubtargetFeature.h" |
David Greene | 08fc0d3 | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 7259f14 | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 44 | #include "llvm/Support/FileSystem.h" |
Craig Topper | 57ff120 | 2015-04-01 05:32:04 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Host.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 46 | #include "llvm/Support/InitLLVM.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 47 | #include "llvm/Support/PluginLoader.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 48 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 49 | #include "llvm/Support/SystemUtils.h" |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 50 | #include "llvm/Support/TargetRegistry.h" |
Nadav Rotem | 0873bea | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 51 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ToolOutputFile.h" |
Adam Nemet | 47c0d49 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 53 | #include "llvm/Support/YAMLTraits.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetMachine.h" |
David Majnemer | 8801905 | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Coroutines.h" |
Chandler Carruth | b699f7b | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 56 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
Rafael Espindola | 3d453ac | 2011-08-02 21:50:24 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 59 | #include <algorithm> |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 60 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 61 | using namespace llvm; |
Chandler Carruth | 4a76032 | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 62 | using namespace opt_tool; |
Chris Lattner | 9d6e7eb | 2002-04-12 18:21:13 +0000 | [diff] [blame] | 63 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 64 | // The OptimizationList is automatically populated with registered Passes by the |
| 65 | // PassNameParser. |
| 66 | // |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 67 | static cl::list<const PassInfo*, bool, PassNameParser> |
| 68 | PassList(cl::desc("Optimizations available:")); |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 69 | |
Chandler Carruth | 4e728be | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 70 | // This flag specifies a textual description of the optimization pass pipeline |
| 71 | // to run over the module. This flag switches opt to use the new pass manager |
| 72 | // infrastructure, completely disabling all of the flags specific to the old |
| 73 | // pass management. |
| 74 | static cl::opt<std::string> PassPipeline( |
| 75 | "passes", |
| 76 | cl::desc("A textual description of the pass pipeline for optimizing"), |
| 77 | cl::Hidden); |
| 78 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 79 | // Other command line options... |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 80 | // |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 81 | static cl::opt<std::string> |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 82 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 83 | cl::init("-"), cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 85 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 86 | OutputFilename("o", cl::desc("Override output filename"), |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 87 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 88 | |
| 89 | static cl::opt<bool> |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 90 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 91 | |
| 92 | static cl::opt<bool> |
| 93 | PrintEachXForm("p", cl::desc("Print module after each transformation")); |
| 94 | |
| 95 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 96 | NoOutput("disable-output", |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 97 | cl::desc("Do not write result bitcode file"), cl::Hidden); |
Chris Lattner | d70b68e | 2003-02-12 18:43:33 +0000 | [diff] [blame] | 98 | |
| 99 | static cl::opt<bool> |
Duncan Sands | 81b0b64 | 2009-10-14 20:01:39 +0000 | [diff] [blame] | 100 | OutputAssembly("S", cl::desc("Write output as LLVM assembly")); |
Daniel Dunbar | 8c042c2 | 2009-09-05 11:34:53 +0000 | [diff] [blame] | 101 | |
| 102 | static cl::opt<bool> |
Peter Collingbourne | d150401 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 103 | OutputThinLTOBC("thinlto-bc", |
| 104 | cl::desc("Write output as ThinLTO-ready bitcode")); |
| 105 | |
Teresa Johnson | e393775 | 2019-01-11 18:31:57 +0000 | [diff] [blame] | 106 | static cl::opt<bool> |
| 107 | SplitLTOUnit("thinlto-split-lto-unit", |
| 108 | cl::desc("Enable splitting of a ThinLTO LTOUnit")); |
| 109 | |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 110 | static cl::opt<std::string> ThinLinkBitcodeFile( |
| 111 | "thin-link-bitcode-file", cl::value_desc("filename"), |
| 112 | cl::desc( |
| 113 | "A file in which to write minimized bitcode for the thin link only")); |
| 114 | |
Peter Collingbourne | d150401 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 115 | static cl::opt<bool> |
Vedant Kumar | ee68a6c | 2016-03-10 06:58:53 +0000 | [diff] [blame] | 116 | NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden); |
Chris Lattner | f3bafc1 | 2003-02-12 18:45:08 +0000 | [diff] [blame] | 117 | |
| 118 | static cl::opt<bool> |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 119 | VerifyEach("verify-each", cl::desc("Verify after each transform")); |
| 120 | |
| 121 | static cl::opt<bool> |
Teresa Johnson | 9507885 | 2016-04-19 15:48:30 +0000 | [diff] [blame] | 122 | DisableDITypeMap("disable-debug-info-type-map", |
| 123 | cl::desc("Don't use a uniquing type map for debug info")); |
| 124 | |
| 125 | static cl::opt<bool> |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 126 | StripDebug("strip-debug", |
| 127 | cl::desc("Strip debugger symbol info from translation unit")); |
| 128 | |
| 129 | static cl::opt<bool> |
Vedant Kumar | 9e729a3 | 2018-06-05 00:56:08 +0000 | [diff] [blame] | 130 | StripNamedMetadata("strip-named-metadata", |
| 131 | cl::desc("Strip module-level named metadata")); |
| 132 | |
| 133 | static cl::opt<bool> DisableInline("disable-inlining", |
| 134 | cl::desc("Do not run the inliner pass")); |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 135 | |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 136 | static cl::opt<bool> |
| 137 | DisableOptimizations("disable-opt", |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 138 | cl::desc("Do not run any optimization passes")); |
| 139 | |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 140 | static cl::opt<bool> |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 141 | StandardLinkOpts("std-link-opts", |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 142 | cl::desc("Include the standard link time optimizations")); |
| 143 | |
| 144 | static cl::opt<bool> |
Gor Nishanov | e648a06 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 145 | OptLevelO0("O0", |
| 146 | cl::desc("Optimization level 0. Similar to clang -O0")); |
| 147 | |
| 148 | static cl::opt<bool> |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 149 | OptLevelO1("O1", |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 150 | cl::desc("Optimization level 1. Similar to clang -O1")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 151 | |
| 152 | static cl::opt<bool> |
| 153 | OptLevelO2("O2", |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 154 | cl::desc("Optimization level 2. Similar to clang -O2")); |
| 155 | |
| 156 | static cl::opt<bool> |
| 157 | OptLevelOs("Os", |
| 158 | cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); |
| 159 | |
| 160 | static cl::opt<bool> |
| 161 | OptLevelOz("Oz", |
| 162 | cl::desc("Like -Os but reduces code size further. Similar to clang -Oz")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 163 | |
| 164 | static cl::opt<bool> |
| 165 | OptLevelO3("O3", |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 166 | cl::desc("Optimization level 3. Similar to clang -O3")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 167 | |
Tim Northover | 97c0826 | 2016-04-18 21:48:55 +0000 | [diff] [blame] | 168 | static cl::opt<unsigned> |
| 169 | CodeGenOptLevel("codegen-opt-level", |
| 170 | cl::desc("Override optimization level for codegen hooks")); |
| 171 | |
Joe Groff | e652b52 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 172 | static cl::opt<std::string> |
| 173 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 174 | |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 175 | static cl::opt<bool> |
| 176 | UnitAtATime("funit-at-a-time", |
Rafael Espindola | 9ca2a9a | 2014-08-21 19:22:24 +0000 | [diff] [blame] | 177 | cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 178 | cl::init(true)); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 179 | |
| 180 | static cl::opt<bool> |
Hal Finkel | 435798e | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 181 | DisableLoopUnrolling("disable-loop-unrolling", |
| 182 | cl::desc("Disable loop unrolling in all relevant passes"), |
| 183 | cl::init(false)); |
Arnold Schwaighofer | df9c9da | 2013-12-03 16:33:06 +0000 | [diff] [blame] | 184 | static cl::opt<bool> |
| 185 | DisableLoopVectorization("disable-loop-vectorization", |
| 186 | cl::desc("Disable the loop vectorization pass"), |
| 187 | cl::init(false)); |
| 188 | |
| 189 | static cl::opt<bool> |
| 190 | DisableSLPVectorization("disable-slp-vectorization", |
| 191 | cl::desc("Disable the slp vectorization pass"), |
| 192 | cl::init(false)); |
| 193 | |
Mehdi Amini | e58bfc9 | 2016-04-12 21:35:18 +0000 | [diff] [blame] | 194 | static cl::opt<bool> EmitSummaryIndex("module-summary", |
| 195 | cl::desc("Emit module summary index"), |
| 196 | cl::init(false)); |
| 197 | |
| 198 | static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), |
| 199 | cl::init(false)); |
Hal Finkel | 435798e | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 200 | |
| 201 | static cl::opt<bool> |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 202 | DisableSimplifyLibCalls("disable-simplify-libcalls", |
Devang Patel | 442b1aa | 2008-09-17 16:01:39 +0000 | [diff] [blame] | 203 | cl::desc("Disable simplify-libcalls")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 204 | |
| 205 | static cl::opt<bool> |
Chris Lattner | 3153e4f | 2004-05-27 20:32:10 +0000 | [diff] [blame] | 206 | Quiet("q", cl::desc("Obsolete option"), cl::Hidden); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 207 | |
Reid Spencer | ec7eb45 | 2004-05-27 16:28:54 +0000 | [diff] [blame] | 208 | static cl::alias |
| 209 | QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); |
| 210 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 211 | static cl::opt<bool> |
| 212 | AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); |
| 213 | |
Vedant Kumar | 48c582c | 2018-01-23 20:43:50 +0000 | [diff] [blame] | 214 | static cl::opt<bool> EnableDebugify( |
| 215 | "enable-debugify", |
| 216 | cl::desc( |
| 217 | "Start the pipeline with debugify and end it with check-debugify")); |
| 218 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 219 | static cl::opt<bool> DebugifyEach( |
| 220 | "debugify-each", |
| 221 | cl::desc( |
| 222 | "Start each pass with debugify and end it with check-debugify")); |
| 223 | |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 224 | static cl::opt<std::string> |
| 225 | DebugifyExport("debugify-export", |
| 226 | cl::desc("Export per-pass debugify statistics to this file"), |
| 227 | cl::value_desc("filename"), cl::init("")); |
| 228 | |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 229 | static cl::opt<bool> |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 230 | PrintBreakpoints("print-breakpoints-for-testing", |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 231 | cl::desc("Print select breakpoints location for testing")); |
| 232 | |
Peter Collingbourne | 4f50278 | 2017-02-17 17:36:52 +0000 | [diff] [blame] | 233 | static cl::opt<std::string> ClDataLayout("data-layout", |
| 234 | cl::desc("data layout string to use"), |
| 235 | cl::value_desc("layout-string"), |
| 236 | cl::init("")); |
Chris Lattner | d331cb3 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 237 | |
Duncan P. N. Exon Smith | 8d61ee9 | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 238 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 239 | "preserve-bc-uselistorder", |
| 240 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 241 | cl::init(true), cl::Hidden); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 242 | |
Duncan P. N. Exon Smith | 8d61ee9 | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 243 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 244 | "preserve-ll-uselistorder", |
| 245 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 246 | cl::init(false), cl::Hidden); |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 247 | |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 248 | static cl::opt<bool> |
| 249 | RunTwice("run-twice", |
| 250 | cl::desc("Run all passes twice, re-using the same pass manager."), |
| 251 | cl::init(false), cl::Hidden); |
| 252 | |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 253 | static cl::opt<bool> DiscardValueNames( |
| 254 | "discard-value-names", |
| 255 | cl::desc("Discard names from Value (other than GlobalValue)."), |
| 256 | cl::init(false), cl::Hidden); |
| 257 | |
David Majnemer | 8801905 | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 258 | static cl::opt<bool> Coroutines( |
| 259 | "enable-coroutines", |
| 260 | cl::desc("Enable coroutine passes."), |
| 261 | cl::init(false), cl::Hidden); |
| 262 | |
Adam Nemet | f8cec99 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 263 | static cl::opt<bool> PassRemarksWithHotness( |
| 264 | "pass-remarks-with-hotness", |
| 265 | cl::desc("With PGO, include profile count in optimization remarks"), |
| 266 | cl::Hidden); |
| 267 | |
Brian Gesiak | e330528 | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 268 | static cl::opt<unsigned> PassRemarksHotnessThreshold( |
| 269 | "pass-remarks-hotness-threshold", |
| 270 | cl::desc("Minimum profile count required for an optimization remark to be output"), |
| 271 | cl::Hidden); |
| 272 | |
Adam Nemet | 47c0d49 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 273 | static cl::opt<std::string> |
| 274 | RemarksFilename("pass-remarks-output", |
| 275 | cl::desc("YAML output filename for pass remarks"), |
| 276 | cl::value_desc("filename")); |
| 277 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 278 | class OptCustomPassManager : public legacy::PassManager { |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 279 | DebugifyStatsMap DIStatsMap; |
| 280 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 281 | public: |
| 282 | using super = legacy::PassManager; |
| 283 | |
| 284 | void add(Pass *P) override { |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 285 | // Wrap each pass with (-check)-debugify passes if requested, making |
| 286 | // exceptions for passes which shouldn't see -debugify instrumentation. |
Vedant Kumar | f66cfed | 2018-06-04 00:11:49 +0000 | [diff] [blame] | 287 | bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() && |
| 288 | !isIRPrintingPass(P) && !isBitcodeWriterPass(P); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 289 | if (!WrapWithDebugify) { |
| 290 | super::add(P); |
| 291 | return; |
| 292 | } |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 293 | |
| 294 | // Apply -debugify/-check-debugify before/after each pass and collect |
| 295 | // debug info loss statistics. |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 296 | PassKind Kind = P->getPassKind(); |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 297 | StringRef Name = P->getPassName(); |
| 298 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 299 | // TODO: Implement Debugify for BasicBlockPass, LoopPass. |
| 300 | switch (Kind) { |
| 301 | case PT_Function: |
| 302 | super::add(createDebugifyFunctionPass()); |
| 303 | super::add(P); |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 304 | super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap)); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 305 | break; |
| 306 | case PT_Module: |
| 307 | super::add(createDebugifyModulePass()); |
| 308 | super::add(P); |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 309 | super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap)); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 310 | break; |
| 311 | default: |
| 312 | super::add(P); |
| 313 | break; |
| 314 | } |
| 315 | } |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 316 | |
| 317 | const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; } |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 320 | static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 321 | // Add the pass to the pass manager... |
| 322 | PM.add(P); |
| 323 | |
| 324 | // If we are verifying all of the intermediate steps, add the verifier... |
Duncan P. N. Exon Smith | a60d430 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 325 | if (VerifyEach) |
Duncan P. N. Exon Smith | 32791b0 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 326 | PM.add(createVerifierPass()); |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Rafael Espindola | 9ca2a9a | 2014-08-21 19:22:24 +0000 | [diff] [blame] | 329 | /// This routine adds optimization passes based on selected optimization level, |
| 330 | /// OptLevel. |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 331 | /// |
| 332 | /// OptLevel - Optimization Level |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 333 | static void AddOptimizationPasses(legacy::PassManagerBase &MPM, |
| 334 | legacy::FunctionPassManager &FPM, |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 335 | TargetMachine *TM, unsigned OptLevel, |
| 336 | unsigned SizeLevel) { |
Vedant Kumar | d449fa8 | 2016-03-10 03:40:14 +0000 | [diff] [blame] | 337 | if (!NoVerify || VerifyEach) |
| 338 | FPM.add(createVerifierPass()); // Verify that input is correct |
Duncan Sands | a3e585d | 2011-12-07 17:14:20 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 52b28896 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 340 | PassManagerBuilder Builder; |
| 341 | Builder.OptLevel = OptLevel; |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 342 | Builder.SizeLevel = SizeLevel; |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 343 | |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 344 | if (DisableInline) { |
| 345 | // No inlining pass |
Eli Friedman | 42d41fd | 2011-06-06 22:13:27 +0000 | [diff] [blame] | 346 | } else if (OptLevel > 1) { |
Dehao Chen | 287fe25 | 2017-03-21 19:55:36 +0000 | [diff] [blame] | 347 | Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false); |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 348 | } else { |
Chandler Carruth | b699f7b | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 349 | Builder.Inliner = createAlwaysInlinerLegacyPass(); |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | 52b28896 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 351 | Builder.DisableUnitAtATime = !UnitAtATime; |
Hal Finkel | 435798e | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 352 | Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ? |
| 353 | DisableLoopUnrolling : OptLevel == 0; |
Greg Bedwell | 0568ba6 | 2013-10-09 08:55:27 +0000 | [diff] [blame] | 354 | |
Renato Golin | 07d9471 | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 355 | // This is final, unless there is a #pragma vectorize enable |
| 356 | if (DisableLoopVectorization) |
| 357 | Builder.LoopVectorize = false; |
| 358 | // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize) |
| 359 | else if (!Builder.LoopVectorize) |
| 360 | Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2; |
| 361 | |
| 362 | // When #pragma vectorize is on for SLP, do the same as above |
Arnold Schwaighofer | df9c9da | 2013-12-03 16:33:06 +0000 | [diff] [blame] | 363 | Builder.SLPVectorize = |
| 364 | DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; |
Hal Finkel | 435798e | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 365 | |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 366 | if (TM) |
Stanislav Mekhanoshin | be4948c | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 367 | TM->adjustPassManager(Builder); |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 368 | |
David Majnemer | 8801905 | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 369 | if (Coroutines) |
| 370 | addCoroutinePassesToExtensionPoints(Builder); |
| 371 | |
Chris Lattner | 52b28896 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 372 | Builder.populateFunctionPassManager(FPM); |
| 373 | Builder.populateModulePassManager(MPM); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 376 | static void AddStandardLinkPasses(legacy::PassManagerBase &PM) { |
Chris Lattner | 52b28896 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 377 | PassManagerBuilder Builder; |
Rafael Espindola | 7b4eb02 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 378 | Builder.VerifyInput = true; |
Rafael Espindola | 7b4eb02 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 379 | if (DisableOptimizations) |
| 380 | Builder.OptLevel = 0; |
| 381 | |
Rafael Espindola | 0b994a70 | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 382 | if (!DisableInline) |
| 383 | Builder.Inliner = createFunctionInliningPass(); |
| 384 | Builder.populateLTOPassManager(PM); |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 387 | //===----------------------------------------------------------------------===// |
| 388 | // CodeGen-related helper functions. |
| 389 | // |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 390 | |
Benjamin Kramer | 0df4e22 | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 391 | static CodeGenOpt::Level GetCodeGenOptLevel() { |
Tim Northover | 97c0826 | 2016-04-18 21:48:55 +0000 | [diff] [blame] | 392 | if (CodeGenOptLevel.getNumOccurrences()) |
| 393 | return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel)); |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 394 | if (OptLevelO1) |
| 395 | return CodeGenOpt::Less; |
| 396 | if (OptLevelO2) |
| 397 | return CodeGenOpt::Default; |
| 398 | if (OptLevelO3) |
| 399 | return CodeGenOpt::Aggressive; |
| 400 | return CodeGenOpt::None; |
| 401 | } |
| 402 | |
| 403 | // Returns the TargetMachine instance or zero if no triple is provided. |
Akira Hatanaka | 4f28a76 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 404 | static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, |
Akira Hatanaka | 0146120 | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 405 | StringRef FeaturesStr, |
| 406 | const TargetOptions &Options) { |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 407 | std::string Error; |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 408 | const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, |
| 409 | Error); |
Nadav Rotem | b338d89 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 410 | // Some modules don't specify a triple, and this is okay. |
Eric Christopher | 60d20a8 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 411 | if (!TheTarget) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 412 | return nullptr; |
Eric Christopher | 60d20a8 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 413 | } |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 414 | |
Rafael Espindola | ac8db59 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 415 | return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, |
| 416 | FeaturesStr, Options, getRelocModel(), |
Rafael Espindola | 9aafb85 | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 417 | getCodeModel(), GetCodeGenOptLevel()); |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 418 | } |
Chris Lattner | 0be4101 | 2002-02-01 04:54:11 +0000 | [diff] [blame] | 419 | |
Sebastian Pop | beaa95d | 2014-03-14 04:04:14 +0000 | [diff] [blame] | 420 | #ifdef LINK_POLLY_INTO_TOOLS |
| 421 | namespace polly { |
| 422 | void initializePollyPasses(llvm::PassRegistry &Registry); |
| 423 | } |
| 424 | #endif |
| 425 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 426 | //===----------------------------------------------------------------------===// |
| 427 | // main for opt |
| 428 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 429 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 430 | InitLLVM X(argc, argv); |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 431 | |
David Greene | 08fc0d3 | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 432 | // Enable debug stream buffering. |
| 433 | EnableDebugBuffering = true; |
| 434 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 435 | LLVMContext Context; |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 436 | |
Nadav Rotem | 0873bea | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 437 | InitializeAllTargets(); |
| 438 | InitializeAllTargetMCs(); |
Tobias Grosser | df46288 | 2014-06-13 16:12:08 +0000 | [diff] [blame] | 439 | InitializeAllAsmPrinters(); |
Teresa Johnson | 8e04a1b | 2016-11-14 17:12:32 +0000 | [diff] [blame] | 440 | InitializeAllAsmParsers(); |
Nadav Rotem | 0873bea | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 441 | |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 442 | // Initialize passes |
| 443 | PassRegistry &Registry = *PassRegistry::getPassRegistry(); |
| 444 | initializeCore(Registry); |
David Majnemer | 8801905 | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 445 | initializeCoroutines(Registry); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 446 | initializeScalarOpts(Registry); |
Michael Gottesman | 24c4898 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 447 | initializeObjCARCOpts(Registry); |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 448 | initializeVectorization(Registry); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 449 | initializeIPO(Registry); |
| 450 | initializeAnalysis(Registry); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 451 | initializeTransformUtils(Registry); |
| 452 | initializeInstCombine(Registry); |
Craig Topper | 043b235 | 2018-04-24 00:05:21 +0000 | [diff] [blame] | 453 | initializeAggressiveInstCombine(Registry); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 454 | initializeInstrumentation(Registry); |
| 455 | initializeTarget(Registry); |
Quentin Colombet | 8048c44 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 456 | // For codegen passes, only passes that do IR to IR transformation are |
Tim Northover | 09da6b5 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 457 | // supported. |
Clement Courbet | 3d45601 | 2017-11-03 12:12:27 +0000 | [diff] [blame] | 458 | initializeExpandMemCmpPassPass(Registry); |
Ayman Musa | eadb58f | 2017-05-15 11:30:54 +0000 | [diff] [blame] | 459 | initializeScalarizeMaskedMemIntrinPass(Registry); |
Quentin Colombet | 8048c44 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 460 | initializeCodeGenPreparePass(Registry); |
Robin Morisset | cf165c3 | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 461 | initializeAtomicExpandPass(Registry); |
Michael Kuperstein | b339184 | 2016-07-25 20:52:00 +0000 | [diff] [blame] | 462 | initializeRewriteSymbolsLegacyPassPass(Registry); |
Reid Kleckner | f77571a | 2015-01-29 00:41:44 +0000 | [diff] [blame] | 463 | initializeWinEHPreparePass(Registry); |
Reid Kleckner | f89d9b1 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 464 | initializeDwarfEHPreparePass(Registry); |
Ahmed Bougacha | 756714d | 2017-05-10 00:39:22 +0000 | [diff] [blame] | 465 | initializeSafeStackLegacyPassPass(Registry); |
Reid Kleckner | 827f211 | 2015-07-09 21:48:40 +0000 | [diff] [blame] | 466 | initializeSjLjEHPreparePass(Registry); |
Michael Kuperstein | f3f8fc6 | 2016-06-24 20:13:42 +0000 | [diff] [blame] | 467 | initializePreISelIntrinsicLoweringLegacyPassPass(Registry); |
Peter Collingbourne | 28f667b | 2016-05-19 04:38:56 +0000 | [diff] [blame] | 468 | initializeGlobalMergePass(Registry); |
Chandler Carruth | fd5a872 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 469 | initializeIndirectBrExpandPassPass(Registry); |
Martin Elshuber | 5e067bb | 2018-11-19 14:26:10 +0000 | [diff] [blame] | 470 | initializeInterleavedLoadCombinePass(Registry); |
Matthew Simpson | d06ea8a | 2016-05-19 20:08:32 +0000 | [diff] [blame] | 471 | initializeInterleavedAccessPass(Registry); |
Hans Wennborg | 5765d84 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 472 | initializeEntryExitInstrumenterPass(Registry); |
| 473 | initializePostInlineEntryExitInstrumenterPass(Registry); |
Wei Mi | 302c35d | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 474 | initializeUnreachableBlockElimLegacyPassPass(Registry); |
Amara Emerson | 0dd30f8 | 2017-05-10 09:42:49 +0000 | [diff] [blame] | 475 | initializeExpandReductionsPass(Registry); |
Heejin Ahn | a6e37da | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 476 | initializeWasmEHPreparePass(Registry); |
Michael Kruse | d9d01c9 | 2017-10-24 17:17:27 +0000 | [diff] [blame] | 477 | initializeWriteBitcodePassPass(Registry); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 478 | |
Sebastian Pop | beaa95d | 2014-03-14 04:04:14 +0000 | [diff] [blame] | 479 | #ifdef LINK_POLLY_INTO_TOOLS |
| 480 | polly::initializePollyPasses(Registry); |
| 481 | #endif |
| 482 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 483 | cl::ParseCommandLineOptions(argc, argv, |
| 484 | "llvm .bc -> .bc modular optimizer and analysis printer\n"); |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 485 | |
Tobias Grosser | 7593f34 | 2010-12-02 20:35:16 +0000 | [diff] [blame] | 486 | if (AnalyzeOnly && NoOutput) { |
| 487 | errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; |
| 488 | return 1; |
| 489 | } |
| 490 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 491 | SMDiagnostic Err; |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 492 | |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 493 | Context.setDiscardValueNames(DiscardValueNames); |
Teresa Johnson | 9507885 | 2016-04-19 15:48:30 +0000 | [diff] [blame] | 494 | if (!DisableDITypeMap) |
| 495 | Context.enableDebugTypeODRUniquing(); |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 496 | |
Adam Nemet | f8cec99 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 497 | if (PassRemarksWithHotness) |
Brian Gesiak | 8e8ec78 | 2017-06-30 18:13:59 +0000 | [diff] [blame] | 498 | Context.setDiagnosticsHotnessRequested(true); |
Adam Nemet | f8cec99 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 499 | |
Brian Gesiak | e330528 | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 500 | if (PassRemarksHotnessThreshold) |
| 501 | Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold); |
| 502 | |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 503 | std::unique_ptr<ToolOutputFile> OptRemarkFile; |
Adam Nemet | 47c0d49 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 504 | if (RemarksFilename != "") { |
| 505 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 506 | OptRemarkFile = |
| 507 | llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None); |
Adam Nemet | 47c0d49 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 508 | if (EC) { |
| 509 | errs() << EC.message() << '\n'; |
| 510 | return 1; |
| 511 | } |
Mehdi Amini | 1bb4b12 | 2016-11-19 18:19:41 +0000 | [diff] [blame] | 512 | Context.setDiagnosticsOutputFile( |
Sam Elliott | 74a34d9 | 2017-08-20 01:30:45 +0000 | [diff] [blame] | 513 | llvm::make_unique<yaml::Output>(OptRemarkFile->os())); |
Adam Nemet | 47c0d49 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 516 | // Load the input module... |
Adrian Prantl | 733fe2f | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 517 | std::unique_ptr<Module> M = |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 518 | parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout); |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 519 | |
Craig Topper | 3248223 | 2014-12-12 07:52:09 +0000 | [diff] [blame] | 520 | if (!M) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 521 | Err.print(argv[0], errs()); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 522 | return 1; |
| 523 | } |
| 524 | |
Duncan P. N. Exon Smith | de89228 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 525 | // Strip debug info before running the verifier. |
| 526 | if (StripDebug) |
| 527 | StripDebugInfo(*M); |
| 528 | |
Vedant Kumar | 9e729a3 | 2018-06-05 00:56:08 +0000 | [diff] [blame] | 529 | // Erase module-level named metadata, if requested. |
| 530 | if (StripNamedMetadata) { |
| 531 | while (!M->named_metadata_empty()) { |
| 532 | NamedMDNode *NMD = &*M->named_metadata_begin(); |
| 533 | M->eraseNamedMetadata(NMD); |
| 534 | } |
| 535 | } |
Vedant Kumar | 701cfb8 | 2018-06-04 00:11:48 +0000 | [diff] [blame] | 536 | |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 537 | // If we are supposed to override the target triple or data layout, do so now. |
| 538 | if (!TargetTriple.empty()) |
| 539 | M->setTargetTriple(Triple::normalize(TargetTriple)); |
| 540 | |
Duncan P. N. Exon Smith | de89228 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 541 | // Immediately run the verifier to catch any problems before starting up the |
| 542 | // pass pipelines. Otherwise we can crash on broken code during |
| 543 | // doInitialization(). |
| 544 | if (!NoVerify && verifyModule(*M, &errs())) { |
Duncan P. N. Exon Smith | 4e5fdbf | 2015-03-31 03:07:23 +0000 | [diff] [blame] | 545 | errs() << argv[0] << ": " << InputFilename |
| 546 | << ": error: input module is broken!\n"; |
Duncan P. N. Exon Smith | de89228 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 547 | return 1; |
| 548 | } |
| 549 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 550 | // Figure out what stream we are supposed to write to... |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 551 | std::unique_ptr<ToolOutputFile> Out; |
| 552 | std::unique_ptr<ToolOutputFile> ThinLinkOut; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 553 | if (NoOutput) { |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 554 | if (!OutputFilename.empty()) |
| 555 | errs() << "WARNING: The -o (output filename) option is ignored when\n" |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 556 | "the --disable-output option is used.\n"; |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 557 | } else { |
| 558 | // Default to standard output. |
| 559 | if (OutputFilename.empty()) |
| 560 | OutputFilename = "-"; |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 561 | |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 562 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 563 | Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 564 | if (EC) { |
| 565 | errs() << EC.message() << '\n'; |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 566 | return 1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 567 | } |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 568 | |
| 569 | if (!ThinLinkBitcodeFile.empty()) { |
| 570 | ThinLinkOut.reset( |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 571 | new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None)); |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 572 | if (EC) { |
| 573 | errs() << EC.message() << '\n'; |
| 574 | return 1; |
| 575 | } |
| 576 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 577 | } |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 578 | |
Chandler Carruth | 7724e8e | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 579 | Triple ModuleTriple(M->getTargetTriple()); |
Akira Hatanaka | 4f28a76 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 580 | std::string CPUStr, FeaturesStr; |
Chandler Carruth | 7724e8e | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 581 | TargetMachine *Machine = nullptr; |
Akira Hatanaka | 0146120 | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 582 | const TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Akira Hatanaka | c17da71 | 2015-05-23 01:12:26 +0000 | [diff] [blame] | 583 | |
Akira Hatanaka | 4f28a76 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 584 | if (ModuleTriple.getArch()) { |
| 585 | CPUStr = getCPUStr(); |
| 586 | FeaturesStr = getFeaturesStr(); |
Akira Hatanaka | 0146120 | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 587 | Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); |
Akira Hatanaka | 4f28a76 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Chandler Carruth | 7724e8e | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 590 | std::unique_ptr<TargetMachine> TM(Machine); |
| 591 | |
Akira Hatanaka | 6f49135 | 2015-05-26 20:17:20 +0000 | [diff] [blame] | 592 | // Override function attributes based on CPUStr, FeaturesStr, and command line |
| 593 | // flags. |
| 594 | setFunctionAttributes(CPUStr, FeaturesStr, *M); |
Akira Hatanaka | e6f0494 | 2015-05-06 23:54:14 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 596 | // If the output is set to be emitted to standard out, and standard out is a |
| 597 | // console, print out a warning message and refuse to do it. We don't |
| 598 | // impress anyone by spewing tons of binary goo to a terminal. |
Dan Gohman | b56bf58 | 2010-01-17 17:47:24 +0000 | [diff] [blame] | 599 | if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 600 | if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 601 | NoOutput = true; |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 602 | |
Teresa Johnson | e393775 | 2019-01-11 18:31:57 +0000 | [diff] [blame] | 603 | if (OutputThinLTOBC) |
| 604 | M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit); |
| 605 | |
Chandler Carruth | 4a76032 | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 606 | if (PassPipeline.getNumOccurrences() > 0) { |
| 607 | OutputKind OK = OK_NoOutput; |
| 608 | if (!NoOutput) |
Tim Shen | 2e37885 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 609 | OK = OutputAssembly |
| 610 | ? OK_OutputAssembly |
| 611 | : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode); |
Chandler Carruth | 4a76032 | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 612 | |
Chandler Carruth | 1d9ab25 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 613 | VerifierKind VK = VK_VerifyInAndOut; |
| 614 | if (NoVerify) |
| 615 | VK = VK_NoVerifier; |
| 616 | else if (VerifyEach) |
| 617 | VK = VK_VerifyEachPass; |
| 618 | |
Chandler Carruth | 4e728be | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 619 | // The user has asked to use the new pass manager and provided a pipeline |
| 620 | // string. Hand off the rest of the functionality to the new code for that |
| 621 | // layer. |
Tim Shen | 2e37885 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 622 | return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(), |
Sam Elliott | 74a34d9 | 2017-08-20 01:30:45 +0000 | [diff] [blame] | 623 | OptRemarkFile.get(), PassPipeline, OK, VK, |
| 624 | PreserveAssemblyUseListOrder, |
Teresa Johnson | 3143f33 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 625 | PreserveBitcodeUseListOrder, EmitSummaryIndex, |
Vedant Kumar | b8c067b | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 626 | EmitModuleHash, EnableDebugify) |
Chandler Carruth | 4e728be | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 627 | ? 0 |
| 628 | : 1; |
Chandler Carruth | 4a76032 | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 629 | } |
Chandler Carruth | 4e728be | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 631 | // Create a PassManager to hold and optimize the collection of passes we are |
Chris Lattner | 2a66aca | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 632 | // about to build. |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 633 | OptCustomPassManager Passes; |
| 634 | bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 2a66aca | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 636 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
Chandler Carruth | 7724e8e | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 637 | TargetLibraryInfoImpl TLII(ModuleTriple); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 188a7e0 | 2011-02-18 22:34:03 +0000 | [diff] [blame] | 639 | // The -disable-simplify-libcalls flag actually disables all builtin optzns. |
| 640 | if (DisableSimplifyLibCalls) |
Chandler Carruth | 6f409cb | 2015-01-24 02:06:09 +0000 | [diff] [blame] | 641 | TLII.disableAllFunctions(); |
| 642 | Passes.add(new TargetLibraryInfoWrapperPass(TLII)); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 643 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 644 | // Add internal analysis passes from the target machine. |
Chandler Carruth | 276f405 | 2015-02-01 12:26:09 +0000 | [diff] [blame] | 645 | Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() |
| 646 | : TargetIRAnalysis())); |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 647 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 648 | if (AddOneTimeDebugifyPasses) |
| 649 | Passes.add(createDebugifyModulePass()); |
Vedant Kumar | 48c582c | 2018-01-23 20:43:50 +0000 | [diff] [blame] | 650 | |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 651 | std::unique_ptr<legacy::FunctionPassManager> FPasses; |
Gor Nishanov | e648a06 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 652 | if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || |
| 653 | OptLevelO3) { |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 654 | FPasses.reset(new legacy::FunctionPassManager(M.get())); |
Chandler Carruth | 1937233 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 655 | FPasses->add(createTargetTransformInfoWrapperPass( |
Chandler Carruth | 276f405 | 2015-02-01 12:26:09 +0000 | [diff] [blame] | 656 | TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis())); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 657 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 658 | |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 659 | if (PrintBreakpoints) { |
| 660 | // Default to standard output. |
| 661 | if (!Out) { |
| 662 | if (OutputFilename.empty()) |
| 663 | OutputFilename = "-"; |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 664 | |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 665 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 666 | Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC, |
| 667 | sys::fs::F_None); |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 668 | if (EC) { |
| 669 | errs() << EC.message() << '\n'; |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 670 | return 1; |
| 671 | } |
| 672 | } |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 673 | Passes.add(createBreakpointPrinter(Out->os())); |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 674 | NoOutput = true; |
| 675 | } |
| 676 | |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 677 | if (TM) { |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 678 | // FIXME: We should dyn_cast this when supported. |
| 679 | auto <M = static_cast<LLVMTargetMachine &>(*TM); |
| 680 | Pass *TPC = LTM.createPassConfig(Passes); |
| 681 | Passes.add(TPC); |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 684 | // Create a new optimization pass for each one specified on the command line |
| 685 | for (unsigned i = 0; i < PassList.size(); ++i) { |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 686 | if (StandardLinkOpts && |
| 687 | StandardLinkOpts.getPosition() < PassList.getPosition(i)) { |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 688 | AddStandardLinkPasses(Passes); |
| 689 | StandardLinkOpts = false; |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 690 | } |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 691 | |
Gor Nishanov | e648a06 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 692 | if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) { |
| 693 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); |
| 694 | OptLevelO0 = false; |
| 695 | } |
| 696 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 697 | if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 698 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 699 | OptLevelO1 = false; |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 700 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 702 | if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 703 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 704 | OptLevelO2 = false; |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 705 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 706 | |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 707 | if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 708 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 709 | OptLevelOs = false; |
| 710 | } |
| 711 | |
| 712 | if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 713 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 714 | OptLevelOz = false; |
| 715 | } |
| 716 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 717 | if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 718 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 719 | OptLevelO3 = false; |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 720 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 722 | const PassInfo *PassInf = PassList[i]; |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 723 | Pass *P = nullptr; |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 724 | if (PassInf->getNormalCtor()) |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 725 | P = PassInf->getNormalCtor()(); |
| 726 | else |
| 727 | errs() << argv[0] << ": cannot create pass: " |
| 728 | << PassInf->getPassName() << "\n"; |
| 729 | if (P) { |
Benjamin Kramer | 3460f22 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 730 | PassKind Kind = P->getPassKind(); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 731 | addPass(Passes, P); |
| 732 | |
| 733 | if (AnalyzeOnly) { |
Benjamin Kramer | 3460f22 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 734 | switch (Kind) { |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 735 | case PT_BasicBlock: |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 736 | Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 737 | break; |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 738 | case PT_Region: |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 739 | Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet)); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 740 | break; |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 741 | case PT_Loop: |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 742 | Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 743 | break; |
| 744 | case PT_Function: |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 745 | Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 746 | break; |
| 747 | case PT_CallGraphSCC: |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 748 | Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 749 | break; |
| 750 | default: |
Eli Bendersky | 8ed971b | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 751 | Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 752 | break; |
| 753 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 754 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 757 | if (PrintEachXForm) |
Duncan P. N. Exon Smith | 8d61ee9 | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 758 | Passes.add( |
| 759 | createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder)); |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 760 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 762 | if (StandardLinkOpts) { |
| 763 | AddStandardLinkPasses(Passes); |
| 764 | StandardLinkOpts = false; |
| 765 | } |
| 766 | |
Gor Nishanov | e648a06 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 767 | if (OptLevelO0) |
| 768 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); |
| 769 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 770 | if (OptLevelO1) |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 771 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 772 | |
| 773 | if (OptLevelO2) |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 774 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 775 | |
| 776 | if (OptLevelOs) |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 777 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 778 | |
| 779 | if (OptLevelOz) |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 780 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 781 | |
| 782 | if (OptLevelO3) |
Justin Lebar | ae889d3 | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 783 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 784 | |
Patrik Hagglund | bed36c4 | 2016-06-15 10:32:00 +0000 | [diff] [blame] | 785 | if (FPasses) { |
Chris Lattner | 3e8984a | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 786 | FPasses->doInitialization(); |
Craig Topper | 0ea6f98 | 2014-12-12 07:52:11 +0000 | [diff] [blame] | 787 | for (Function &F : *M) |
| 788 | FPasses->run(F); |
Chris Lattner | 3e8984a | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 789 | FPasses->doFinalization(); |
| 790 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 791 | |
| 792 | // Check that the module is well formed on completion of optimization |
Duncan P. N. Exon Smith | a60d430 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 793 | if (!NoVerify && !VerifyEach) |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 794 | Passes.add(createVerifierPass()); |
| 795 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 796 | if (AddOneTimeDebugifyPasses) |
| 797 | Passes.add(createCheckDebugifyModulePass(false)); |
Vedant Kumar | 48c582c | 2018-01-23 20:43:50 +0000 | [diff] [blame] | 798 | |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 799 | // In run twice mode, we want to make sure the output is bit-by-bit |
| 800 | // equivalent if we run the pass manager again, so setup two buffers and |
| 801 | // a stream to write to them. Note that llc does something similar and it |
| 802 | // may be worth to abstract this out in the future. |
| 803 | SmallVector<char, 0> Buffer; |
Roman Tereshin | 9899cf0 | 2018-04-13 21:23:11 +0000 | [diff] [blame] | 804 | SmallVector<char, 0> FirstRunBuffer; |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 805 | std::unique_ptr<raw_svector_ostream> BOS; |
Keno Fischer | 357108c | 2015-12-05 00:06:37 +0000 | [diff] [blame] | 806 | raw_ostream *OS = nullptr; |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 807 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 808 | // Write bitcode or assembly to the output as the last step... |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 809 | if (!NoOutput && !AnalyzeOnly) { |
Keno Fischer | 357108c | 2015-12-05 00:06:37 +0000 | [diff] [blame] | 810 | assert(Out); |
| 811 | OS = &Out->os(); |
| 812 | if (RunTwice) { |
| 813 | BOS = make_unique<raw_svector_ostream>(Buffer); |
| 814 | OS = BOS.get(); |
| 815 | } |
Mehdi Amini | bd3bdfc | 2016-04-13 17:20:10 +0000 | [diff] [blame] | 816 | if (OutputAssembly) { |
| 817 | if (EmitSummaryIndex) |
| 818 | report_fatal_error("Text output is incompatible with -module-summary"); |
| 819 | if (EmitModuleHash) |
| 820 | report_fatal_error("Text output is incompatible with -module-hash"); |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 821 | Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder)); |
Peter Collingbourne | d150401 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 822 | } else if (OutputThinLTOBC) |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 823 | Passes.add(createWriteThinLTOBitcodePass( |
| 824 | *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr)); |
Peter Collingbourne | d150401 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 825 | else |
Mehdi Amini | e58bfc9 | 2016-04-12 21:35:18 +0000 | [diff] [blame] | 826 | Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder, |
| 827 | EmitSummaryIndex, EmitModuleHash)); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Andrew Trick | ce96902 | 2011-04-05 18:54:36 +0000 | [diff] [blame] | 830 | // Before executing passes, print the final values of the LLVM options. |
| 831 | cl::PrintOptionValues(); |
| 832 | |
Roman Tereshin | 883750c | 2018-04-13 21:22:24 +0000 | [diff] [blame] | 833 | if (!RunTwice) { |
| 834 | // Now that we have all of the passes ready, run them. |
| 835 | Passes.run(*M); |
| 836 | } else { |
| 837 | // If requested, run all passes twice with the same pass manager to catch |
| 838 | // bugs caused by persistent state in the passes. |
Rafael Espindola | 22b7724 | 2018-02-14 19:50:40 +0000 | [diff] [blame] | 839 | std::unique_ptr<Module> M2(CloneModule(*M)); |
Roman Tereshin | 883750c | 2018-04-13 21:22:24 +0000 | [diff] [blame] | 840 | // Run all passes on the original module first, so the second run processes |
| 841 | // the clone to catch CloneModule bugs. |
| 842 | Passes.run(*M); |
Roman Tereshin | 9899cf0 | 2018-04-13 21:23:11 +0000 | [diff] [blame] | 843 | FirstRunBuffer = Buffer; |
Rafael Espindola | 22b7724 | 2018-02-14 19:50:40 +0000 | [diff] [blame] | 844 | Buffer.clear(); |
Keno Fischer | 3687382 | 2015-12-05 01:38:12 +0000 | [diff] [blame] | 845 | |
Roman Tereshin | 883750c | 2018-04-13 21:22:24 +0000 | [diff] [blame] | 846 | Passes.run(*M2); |
Keno Fischer | 3687382 | 2015-12-05 01:38:12 +0000 | [diff] [blame] | 847 | |
Roman Tereshin | 883750c | 2018-04-13 21:22:24 +0000 | [diff] [blame] | 848 | // Compare the two outputs and make sure they're the same |
Keno Fischer | 357108c | 2015-12-05 00:06:37 +0000 | [diff] [blame] | 849 | assert(Out); |
Roman Tereshin | 9899cf0 | 2018-04-13 21:23:11 +0000 | [diff] [blame] | 850 | if (Buffer.size() != FirstRunBuffer.size() || |
| 851 | (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) { |
| 852 | errs() |
| 853 | << "Running the pass manager twice changed the output.\n" |
| 854 | "Writing the result of the second run to the specified output.\n" |
| 855 | "To generate the one-run comparison binary, just run without\n" |
| 856 | "the compile-twice option\n"; |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 857 | Out->os() << BOS->str(); |
| 858 | Out->keep(); |
Sam Elliott | 74a34d9 | 2017-08-20 01:30:45 +0000 | [diff] [blame] | 859 | if (OptRemarkFile) |
| 860 | OptRemarkFile->keep(); |
Keno Fischer | 6f41c13 | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 861 | return 1; |
| 862 | } |
| 863 | Out->os() << BOS->str(); |
| 864 | } |
| 865 | |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 866 | if (DebugifyEach && !DebugifyExport.empty()) |
| 867 | exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap()); |
| 868 | |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 869 | // Declare success. |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 870 | if (!NoOutput || PrintBreakpoints) |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 871 | Out->keep(); |
| 872 | |
Sam Elliott | 74a34d9 | 2017-08-20 01:30:45 +0000 | [diff] [blame] | 873 | if (OptRemarkFile) |
| 874 | OptRemarkFile->keep(); |
Adam Nemet | 47c0d49 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 875 | |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 876 | if (ThinLinkOut) |
| 877 | ThinLinkOut->keep(); |
| 878 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 879 | return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 880 | } |