Chris Lattner | 46e18c7 | 2004-04-02 05:06:57 +0000 | [diff] [blame] | 1 | //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | // Optimizations may be specified an arbitrary number of times on the command |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 11 | // line, They are run in the order specified. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | 6fc7ff4 | 2001-10-18 06:05:15 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | |
Eli Bendersky | 54dc283 | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 15 | #include "BreakpointPrinter.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 16 | #include "NewPMDriver.h" |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 17 | #include "PassPrinters.h" |
Chris Lattner | 15c8b5e | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.h" |
| 22 | #include "llvm/Analysis/RegionPass.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 25 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/CommandFlags.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
Duncan P. N. Exon Smith | 49e6a70 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 29 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LLVMContext.h" |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 31 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 1b69ed8 | 2014-03-04 12:32:42 +0000 | [diff] [blame] | 32 | #include "llvm/IR/LegacyPassNameParser.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Module.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 35 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 36 | #include "llvm/InitializePasses.h" |
Jakub Staszak | 63e77d5 | 2013-01-10 21:56:40 +0000 | [diff] [blame] | 37 | #include "llvm/LinkAllIR.h" |
Chandler Carruth | 1fe21fc | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 38 | #include "llvm/LinkAllPasses.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 39 | #include "llvm/MC/SubtargetFeature.h" |
David Greene | ed8a1de | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 41 | #include "llvm/Support/FileSystem.h" |
Craig Topper | d675e11 | 2015-04-01 05:32:04 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Host.h" |
Chris Lattner | 76d4632 | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 44 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 4b2a6e2 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 45 | #include "llvm/Support/PrettyStackTrace.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Signals.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 47 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 48 | #include "llvm/Support/SystemUtils.h" |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 49 | #include "llvm/Support/TargetRegistry.h" |
Nadav Rotem | ac9a344 | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 50 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 51 | #include "llvm/Support/ToolOutputFile.h" |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 52 | #include "llvm/Support/YAMLTraits.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetMachine.h" |
David Majnemer | 3d32b7e | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 54 | #include "llvm/Transforms/Coroutines.h" |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
Rafael Espindola | 591eaa4 | 2011-08-02 21:50:24 +0000 | [diff] [blame] | 56 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 58 | #include <algorithm> |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 59 | #include <memory> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 61 | using namespace opt_tool; |
Chris Lattner | b86b11a | 2002-04-12 18:21:13 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 63 | // The OptimizationList is automatically populated with registered Passes by the |
| 64 | // PassNameParser. |
| 65 | // |
Chris Lattner | 8e5e13b | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 66 | static cl::list<const PassInfo*, bool, PassNameParser> |
| 67 | PassList(cl::desc("Optimizations available:")); |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 68 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 69 | // This flag specifies a textual description of the optimization pass pipeline |
| 70 | // to run over the module. This flag switches opt to use the new pass manager |
| 71 | // infrastructure, completely disabling all of the flags specific to the old |
| 72 | // pass management. |
| 73 | static cl::opt<std::string> PassPipeline( |
| 74 | "passes", |
| 75 | cl::desc("A textual description of the pass pipeline for optimizing"), |
| 76 | cl::Hidden); |
| 77 | |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 78 | // Other command line options... |
Chris Lattner | c90d6ba | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 79 | // |
Chris Lattner | 02a1683 | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 80 | static cl::opt<std::string> |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 81 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 82 | cl::init("-"), cl::value_desc("filename")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 02a1683 | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 84 | static cl::opt<std::string> |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 85 | OutputFilename("o", cl::desc("Override output filename"), |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 86 | cl::value_desc("filename")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 87 | |
| 88 | static cl::opt<bool> |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 89 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 90 | |
| 91 | static cl::opt<bool> |
| 92 | PrintEachXForm("p", cl::desc("Print module after each transformation")); |
| 93 | |
| 94 | static cl::opt<bool> |
Chris Lattner | 30f40d9 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 95 | NoOutput("disable-output", |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 96 | cl::desc("Do not write result bitcode file"), cl::Hidden); |
Chris Lattner | 4dbe59b | 2003-02-12 18:43:33 +0000 | [diff] [blame] | 97 | |
| 98 | static cl::opt<bool> |
Duncan Sands | 6f2ffce | 2009-10-14 20:01:39 +0000 | [diff] [blame] | 99 | OutputAssembly("S", cl::desc("Write output as LLVM assembly")); |
Daniel Dunbar | 6b3153b | 2009-09-05 11:34:53 +0000 | [diff] [blame] | 100 | |
| 101 | static cl::opt<bool> |
Vedant Kumar | ae22c58 | 2016-03-10 06:58:53 +0000 | [diff] [blame] | 102 | NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden); |
Chris Lattner | b8450599 | 2003-02-12 18:45:08 +0000 | [diff] [blame] | 103 | |
| 104 | static cl::opt<bool> |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 105 | VerifyEach("verify-each", cl::desc("Verify after each transform")); |
| 106 | |
| 107 | static cl::opt<bool> |
Teresa Johnson | 07c7e25 | 2016-04-19 15:48:30 +0000 | [diff] [blame] | 108 | DisableDITypeMap("disable-debug-info-type-map", |
| 109 | cl::desc("Don't use a uniquing type map for debug info")); |
| 110 | |
| 111 | static cl::opt<bool> |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 112 | StripDebug("strip-debug", |
| 113 | cl::desc("Strip debugger symbol info from translation unit")); |
| 114 | |
| 115 | static cl::opt<bool> |
| 116 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| 117 | |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 118 | static cl::opt<bool> |
| 119 | DisableOptimizations("disable-opt", |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 120 | cl::desc("Do not run any optimization passes")); |
| 121 | |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 122 | static cl::opt<bool> |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 123 | StandardLinkOpts("std-link-opts", |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 124 | cl::desc("Include the standard link time optimizations")); |
| 125 | |
| 126 | static cl::opt<bool> |
Gor Nishanov | f3bb361 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 127 | OptLevelO0("O0", |
| 128 | cl::desc("Optimization level 0. Similar to clang -O0")); |
| 129 | |
| 130 | static cl::opt<bool> |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 131 | OptLevelO1("O1", |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 132 | cl::desc("Optimization level 1. Similar to clang -O1")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 133 | |
| 134 | static cl::opt<bool> |
| 135 | OptLevelO2("O2", |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 136 | cl::desc("Optimization level 2. Similar to clang -O2")); |
| 137 | |
| 138 | static cl::opt<bool> |
| 139 | OptLevelOs("Os", |
| 140 | cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); |
| 141 | |
| 142 | static cl::opt<bool> |
| 143 | OptLevelOz("Oz", |
| 144 | cl::desc("Like -Os but reduces code size further. Similar to clang -Oz")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 145 | |
| 146 | static cl::opt<bool> |
| 147 | OptLevelO3("O3", |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 148 | cl::desc("Optimization level 3. Similar to clang -O3")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 149 | |
Tim Northover | b629c77 | 2016-04-18 21:48:55 +0000 | [diff] [blame] | 150 | static cl::opt<unsigned> |
| 151 | CodeGenOptLevel("codegen-opt-level", |
| 152 | cl::desc("Override optimization level for codegen hooks")); |
| 153 | |
Joe Groff | 1b73869 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 154 | static cl::opt<std::string> |
| 155 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 156 | |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 157 | static cl::opt<bool> |
| 158 | UnitAtATime("funit-at-a-time", |
Rafael Espindola | 40bfd6d | 2014-08-21 19:22:24 +0000 | [diff] [blame] | 159 | cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"), |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 160 | cl::init(true)); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 161 | |
| 162 | static cl::opt<bool> |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 163 | DisableLoopUnrolling("disable-loop-unrolling", |
| 164 | cl::desc("Disable loop unrolling in all relevant passes"), |
| 165 | cl::init(false)); |
Arnold Schwaighofer | 46db725 | 2013-12-03 16:33:06 +0000 | [diff] [blame] | 166 | static cl::opt<bool> |
| 167 | DisableLoopVectorization("disable-loop-vectorization", |
| 168 | cl::desc("Disable the loop vectorization pass"), |
| 169 | cl::init(false)); |
| 170 | |
| 171 | static cl::opt<bool> |
| 172 | DisableSLPVectorization("disable-slp-vectorization", |
| 173 | cl::desc("Disable the slp vectorization pass"), |
| 174 | cl::init(false)); |
| 175 | |
Mehdi Amini | 68da426 | 2016-04-12 21:35:18 +0000 | [diff] [blame] | 176 | static cl::opt<bool> EmitSummaryIndex("module-summary", |
| 177 | cl::desc("Emit module summary index"), |
| 178 | cl::init(false)); |
| 179 | |
| 180 | static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), |
| 181 | cl::init(false)); |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 182 | |
| 183 | static cl::opt<bool> |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 184 | DisableSimplifyLibCalls("disable-simplify-libcalls", |
Devang Patel | 7293f0f | 2008-09-17 16:01:39 +0000 | [diff] [blame] | 185 | cl::desc("Disable simplify-libcalls")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 186 | |
| 187 | static cl::opt<bool> |
Chris Lattner | 1553edc | 2004-05-27 20:32:10 +0000 | [diff] [blame] | 188 | Quiet("q", cl::desc("Obsolete option"), cl::Hidden); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 189 | |
Reid Spencer | c8878ed | 2004-05-27 16:28:54 +0000 | [diff] [blame] | 190 | static cl::alias |
| 191 | QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); |
| 192 | |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 193 | static cl::opt<bool> |
| 194 | AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); |
| 195 | |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 196 | static cl::opt<bool> |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 197 | PrintBreakpoints("print-breakpoints-for-testing", |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 198 | cl::desc("Print select breakpoints location for testing")); |
| 199 | |
Chris Lattner | cfa1911 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 200 | static cl::opt<std::string> |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 201 | DefaultDataLayout("default-data-layout", |
Chris Lattner | cfa1911 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 202 | cl::desc("data layout string to use if not specified by module"), |
| 203 | cl::value_desc("layout-string"), cl::init("")); |
| 204 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 205 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 206 | "preserve-bc-uselistorder", |
| 207 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 208 | cl::init(true), cl::Hidden); |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 209 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 210 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 211 | "preserve-ll-uselistorder", |
| 212 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 213 | cl::init(false), cl::Hidden); |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 214 | |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 215 | static cl::opt<bool> |
| 216 | RunTwice("run-twice", |
| 217 | cl::desc("Run all passes twice, re-using the same pass manager."), |
| 218 | cl::init(false), cl::Hidden); |
| 219 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 220 | static cl::opt<bool> DiscardValueNames( |
| 221 | "discard-value-names", |
| 222 | cl::desc("Discard names from Value (other than GlobalValue)."), |
| 223 | cl::init(false), cl::Hidden); |
| 224 | |
David Majnemer | 3d32b7e | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 225 | static cl::opt<bool> Coroutines( |
| 226 | "enable-coroutines", |
| 227 | cl::desc("Enable coroutine passes."), |
| 228 | cl::init(false), cl::Hidden); |
| 229 | |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 230 | static cl::opt<bool> PassRemarksWithHotness( |
| 231 | "pass-remarks-with-hotness", |
| 232 | cl::desc("With PGO, include profile count in optimization remarks"), |
| 233 | cl::Hidden); |
| 234 | |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 235 | static cl::opt<std::string> |
| 236 | RemarksFilename("pass-remarks-output", |
| 237 | cl::desc("YAML output filename for pass remarks"), |
| 238 | cl::value_desc("filename")); |
| 239 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 240 | static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 241 | // Add the pass to the pass manager... |
| 242 | PM.add(P); |
| 243 | |
| 244 | // If we are verifying all of the intermediate steps, add the verifier... |
Duncan P. N. Exon Smith | ab58a56 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 245 | if (VerifyEach) |
Duncan P. N. Exon Smith | 6ef5f28 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 246 | PM.add(createVerifierPass()); |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Rafael Espindola | 40bfd6d | 2014-08-21 19:22:24 +0000 | [diff] [blame] | 249 | /// This routine adds optimization passes based on selected optimization level, |
| 250 | /// OptLevel. |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 251 | /// |
| 252 | /// OptLevel - Optimization Level |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 253 | static void AddOptimizationPasses(legacy::PassManagerBase &MPM, |
| 254 | legacy::FunctionPassManager &FPM, |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 255 | TargetMachine *TM, unsigned OptLevel, |
| 256 | unsigned SizeLevel) { |
Vedant Kumar | 37a1d62 | 2016-03-10 03:40:14 +0000 | [diff] [blame] | 257 | if (!NoVerify || VerifyEach) |
| 258 | FPM.add(createVerifierPass()); // Verify that input is correct |
Duncan Sands | 0f19e91 | 2011-12-07 17:14:20 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 260 | PassManagerBuilder Builder; |
| 261 | Builder.OptLevel = OptLevel; |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 262 | Builder.SizeLevel = SizeLevel; |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 263 | |
Eli Friedman | eb0c52f | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 264 | if (DisableInline) { |
| 265 | // No inlining pass |
Eli Friedman | b68fe25 | 2011-06-06 22:13:27 +0000 | [diff] [blame] | 266 | } else if (OptLevel > 1) { |
Eli Bendersky | 49f6565 | 2014-03-12 16:12:36 +0000 | [diff] [blame] | 267 | Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel); |
Eli Friedman | eb0c52f | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 268 | } else { |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 269 | Builder.Inliner = createAlwaysInlinerLegacyPass(); |
Eli Friedman | eb0c52f | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 271 | Builder.DisableUnitAtATime = !UnitAtATime; |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 272 | Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ? |
| 273 | DisableLoopUnrolling : OptLevel == 0; |
Greg Bedwell | 1411aeb | 2013-10-09 08:55:27 +0000 | [diff] [blame] | 274 | |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 275 | // This is final, unless there is a #pragma vectorize enable |
| 276 | if (DisableLoopVectorization) |
| 277 | Builder.LoopVectorize = false; |
| 278 | // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize) |
| 279 | else if (!Builder.LoopVectorize) |
| 280 | Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2; |
| 281 | |
| 282 | // When #pragma vectorize is on for SLP, do the same as above |
Arnold Schwaighofer | 46db725 | 2013-12-03 16:33:06 +0000 | [diff] [blame] | 283 | Builder.SLPVectorize = |
| 284 | DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 285 | |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 286 | // Add target-specific passes that need to run as early as possible. |
| 287 | if (TM) |
| 288 | Builder.addExtension( |
| 289 | PassManagerBuilder::EP_EarlyAsPossible, |
| 290 | [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { |
| 291 | TM->addEarlyAsPossiblePasses(PM); |
| 292 | }); |
| 293 | |
David Majnemer | 3d32b7e | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 294 | if (Coroutines) |
| 295 | addCoroutinePassesToExtensionPoints(Builder); |
| 296 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 297 | Builder.populateFunctionPassManager(FPM); |
| 298 | Builder.populateModulePassManager(MPM); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 301 | static void AddStandardLinkPasses(legacy::PassManagerBase &PM) { |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 302 | PassManagerBuilder Builder; |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 303 | Builder.VerifyInput = true; |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 304 | if (DisableOptimizations) |
| 305 | Builder.OptLevel = 0; |
| 306 | |
Rafael Espindola | e07caad | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 307 | if (!DisableInline) |
| 308 | Builder.Inliner = createFunctionInliningPass(); |
| 309 | Builder.populateLTOPassManager(PM); |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 312 | //===----------------------------------------------------------------------===// |
| 313 | // CodeGen-related helper functions. |
| 314 | // |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 315 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 316 | static CodeGenOpt::Level GetCodeGenOptLevel() { |
Tim Northover | b629c77 | 2016-04-18 21:48:55 +0000 | [diff] [blame] | 317 | if (CodeGenOptLevel.getNumOccurrences()) |
| 318 | return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel)); |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 319 | if (OptLevelO1) |
| 320 | return CodeGenOpt::Less; |
| 321 | if (OptLevelO2) |
| 322 | return CodeGenOpt::Default; |
| 323 | if (OptLevelO3) |
| 324 | return CodeGenOpt::Aggressive; |
| 325 | return CodeGenOpt::None; |
| 326 | } |
| 327 | |
| 328 | // Returns the TargetMachine instance or zero if no triple is provided. |
Akira Hatanaka | 32b3760 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 329 | static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 330 | StringRef FeaturesStr, |
| 331 | const TargetOptions &Options) { |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 332 | std::string Error; |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 333 | const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, |
| 334 | Error); |
Nadav Rotem | b1615b1 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 335 | // Some modules don't specify a triple, and this is okay. |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 336 | if (!TheTarget) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 337 | return nullptr; |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 338 | } |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 339 | |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 340 | return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, |
| 341 | FeaturesStr, Options, getRelocModel(), |
| 342 | CMModel, GetCodeGenOptLevel()); |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 343 | } |
Chris Lattner | 4db2f2c | 2002-02-01 04:54:11 +0000 | [diff] [blame] | 344 | |
Sebastian Pop | a59005b | 2014-03-14 04:04:14 +0000 | [diff] [blame] | 345 | #ifdef LINK_POLLY_INTO_TOOLS |
| 346 | namespace polly { |
| 347 | void initializePollyPasses(llvm::PassRegistry &Registry); |
| 348 | } |
| 349 | #endif |
| 350 | |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 351 | //===----------------------------------------------------------------------===// |
| 352 | // main for opt |
| 353 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 354 | int main(int argc, char **argv) { |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 355 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Chris Lattner | 4b2a6e2 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 356 | llvm::PrettyStackTraceProgram X(argc, argv); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 357 | |
David Greene | ed8a1de | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 358 | // Enable debug stream buffering. |
| 359 | EnableDebugBuffering = true; |
| 360 | |
Chris Lattner | 4b2a6e2 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 361 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 362 | LLVMContext Context; |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 363 | |
Nadav Rotem | ac9a344 | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 364 | InitializeAllTargets(); |
| 365 | InitializeAllTargetMCs(); |
Tobias Grosser | 9190e0d | 2014-06-13 16:12:08 +0000 | [diff] [blame] | 366 | InitializeAllAsmPrinters(); |
Teresa Johnson | 3624bdf | 2016-11-14 17:12:32 +0000 | [diff] [blame] | 367 | InitializeAllAsmParsers(); |
Nadav Rotem | ac9a344 | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 368 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 369 | // Initialize passes |
| 370 | PassRegistry &Registry = *PassRegistry::getPassRegistry(); |
| 371 | initializeCore(Registry); |
David Majnemer | 3d32b7e | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 372 | initializeCoroutines(Registry); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 373 | initializeScalarOpts(Registry); |
Michael Gottesman | 79d8d81 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 374 | initializeObjCARCOpts(Registry); |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 375 | initializeVectorization(Registry); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 376 | initializeIPO(Registry); |
| 377 | initializeAnalysis(Registry); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 378 | initializeTransformUtils(Registry); |
| 379 | initializeInstCombine(Registry); |
| 380 | initializeInstrumentation(Registry); |
| 381 | initializeTarget(Registry); |
Quentin Colombet | a349084 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 382 | // For codegen passes, only passes that do IR to IR transformation are |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 383 | // supported. |
Quentin Colombet | a349084 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 384 | initializeCodeGenPreparePass(Registry); |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 385 | initializeAtomicExpandPass(Registry); |
Michael Kuperstein | 39feb62 | 2016-07-25 20:52:00 +0000 | [diff] [blame] | 386 | initializeRewriteSymbolsLegacyPassPass(Registry); |
Reid Kleckner | 1185fce | 2015-01-29 00:41:44 +0000 | [diff] [blame] | 387 | initializeWinEHPreparePass(Registry); |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 388 | initializeDwarfEHPreparePass(Registry); |
Benjamin Kramer | 390c33c | 2016-01-27 16:53:42 +0000 | [diff] [blame] | 389 | initializeSafeStackPass(Registry); |
Reid Kleckner | c16b107 | 2015-07-09 21:48:40 +0000 | [diff] [blame] | 390 | initializeSjLjEHPreparePass(Registry); |
Michael Kuperstein | 82d5da5 | 2016-06-24 20:13:42 +0000 | [diff] [blame] | 391 | initializePreISelIntrinsicLoweringLegacyPassPass(Registry); |
Peter Collingbourne | fe12d0e | 2016-05-19 04:38:56 +0000 | [diff] [blame] | 392 | initializeGlobalMergePass(Registry); |
Matthew Simpson | 330a125 | 2016-05-19 20:08:32 +0000 | [diff] [blame] | 393 | initializeInterleavedAccessPass(Registry); |
Hal Finkel | 40d7f5c | 2016-09-01 09:42:39 +0000 | [diff] [blame] | 394 | initializeCountingFunctionInserterPass(Registry); |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 395 | initializeUnreachableBlockElimLegacyPassPass(Registry); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 396 | |
Sebastian Pop | a59005b | 2014-03-14 04:04:14 +0000 | [diff] [blame] | 397 | #ifdef LINK_POLLY_INTO_TOOLS |
| 398 | polly::initializePollyPasses(Registry); |
| 399 | #endif |
| 400 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 401 | cl::ParseCommandLineOptions(argc, argv, |
| 402 | "llvm .bc -> .bc modular optimizer and analysis printer\n"); |
Chris Lattner | c90d6ba | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 403 | |
Tobias Grosser | 083379f | 2010-12-02 20:35:16 +0000 | [diff] [blame] | 404 | if (AnalyzeOnly && NoOutput) { |
| 405 | errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; |
| 406 | return 1; |
| 407 | } |
| 408 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 409 | SMDiagnostic Err; |
Vikram S. Adve | 82491b7 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 410 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 411 | Context.setDiscardValueNames(DiscardValueNames); |
Teresa Johnson | 07c7e25 | 2016-04-19 15:48:30 +0000 | [diff] [blame] | 412 | if (!DisableDITypeMap) |
| 413 | Context.enableDebugTypeODRUniquing(); |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 414 | |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 415 | if (PassRemarksWithHotness) |
| 416 | Context.setDiagnosticHotnessRequested(true); |
| 417 | |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 418 | std::unique_ptr<tool_output_file> YamlFile; |
| 419 | if (RemarksFilename != "") { |
| 420 | std::error_code EC; |
| 421 | YamlFile = llvm::make_unique<tool_output_file>(RemarksFilename, EC, |
| 422 | sys::fs::F_None); |
| 423 | if (EC) { |
| 424 | errs() << EC.message() << '\n'; |
| 425 | return 1; |
| 426 | } |
Mehdi Amini | 6f40836 | 2016-11-19 18:19:41 +0000 | [diff] [blame] | 427 | Context.setDiagnosticsOutputFile( |
| 428 | llvm::make_unique<yaml::Output>(YamlFile->os())); |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 431 | // Load the input module... |
Rafael Espindola | d233b06 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 432 | std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context); |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 433 | |
Craig Topper | e2f17f0 | 2014-12-12 07:52:09 +0000 | [diff] [blame] | 434 | if (!M) { |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 435 | Err.print(argv[0], errs()); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 436 | return 1; |
| 437 | } |
| 438 | |
Duncan P. N. Exon Smith | 49e6a70 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 439 | // Strip debug info before running the verifier. |
| 440 | if (StripDebug) |
| 441 | StripDebugInfo(*M); |
| 442 | |
| 443 | // Immediately run the verifier to catch any problems before starting up the |
| 444 | // pass pipelines. Otherwise we can crash on broken code during |
| 445 | // doInitialization(). |
| 446 | if (!NoVerify && verifyModule(*M, &errs())) { |
Duncan P. N. Exon Smith | 4628282 | 2015-03-31 03:07:23 +0000 | [diff] [blame] | 447 | errs() << argv[0] << ": " << InputFilename |
| 448 | << ": error: input module is broken!\n"; |
Duncan P. N. Exon Smith | 49e6a70 | 2015-03-27 22:04:28 +0000 | [diff] [blame] | 449 | return 1; |
| 450 | } |
| 451 | |
Joe Groff | 1b73869 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 452 | // If we are supposed to override the target triple, do so now. |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 453 | if (!TargetTriple.empty()) |
Joe Groff | 1b73869 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 454 | M->setTargetTriple(Triple::normalize(TargetTriple)); |
Eric Christopher | 1f14031 | 2013-04-14 23:35:36 +0000 | [diff] [blame] | 455 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 456 | // Figure out what stream we are supposed to write to... |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 457 | std::unique_ptr<tool_output_file> Out; |
Dan Gohman | 083330a | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 458 | if (NoOutput) { |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 459 | if (!OutputFilename.empty()) |
| 460 | errs() << "WARNING: The -o (output filename) option is ignored when\n" |
Dan Gohman | 083330a | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 461 | "the --disable-output option is used.\n"; |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 462 | } else { |
| 463 | // Default to standard output. |
| 464 | if (OutputFilename.empty()) |
| 465 | OutputFilename = "-"; |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 466 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 467 | std::error_code EC; |
| 468 | Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None)); |
| 469 | if (EC) { |
| 470 | errs() << EC.message() << '\n'; |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 471 | return 1; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 472 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 473 | } |
Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 474 | |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 475 | Triple ModuleTriple(M->getTargetTriple()); |
Akira Hatanaka | 32b3760 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 476 | std::string CPUStr, FeaturesStr; |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 477 | TargetMachine *Machine = nullptr; |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 478 | const TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Akira Hatanaka | bd88183 | 2015-05-23 01:12:26 +0000 | [diff] [blame] | 479 | |
Akira Hatanaka | 32b3760 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 480 | if (ModuleTriple.getArch()) { |
| 481 | CPUStr = getCPUStr(); |
| 482 | FeaturesStr = getFeaturesStr(); |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 483 | Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); |
Akira Hatanaka | 32b3760 | 2015-05-06 23:49:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 486 | std::unique_ptr<TargetMachine> TM(Machine); |
| 487 | |
Akira Hatanaka | e36505c | 2015-05-26 20:17:20 +0000 | [diff] [blame] | 488 | // Override function attributes based on CPUStr, FeaturesStr, and command line |
| 489 | // flags. |
| 490 | setFunctionAttributes(CPUStr, FeaturesStr, *M); |
Akira Hatanaka | 3058d0f | 2015-05-06 23:54:14 +0000 | [diff] [blame] | 491 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 492 | // If the output is set to be emitted to standard out, and standard out is a |
| 493 | // console, print out a warning message and refuse to do it. We don't |
| 494 | // impress anyone by spewing tons of binary goo to a terminal. |
Dan Gohman | 915ad96 | 2010-01-17 17:47:24 +0000 | [diff] [blame] | 495 | if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 496 | if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 497 | NoOutput = true; |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 498 | |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 499 | if (PassPipeline.getNumOccurrences() > 0) { |
| 500 | OutputKind OK = OK_NoOutput; |
| 501 | if (!NoOutput) |
| 502 | OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode; |
| 503 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 504 | VerifierKind VK = VK_VerifyInAndOut; |
| 505 | if (NoVerify) |
| 506 | VK = VK_NoVerifier; |
| 507 | else if (VerifyEach) |
| 508 | VK = VK_VerifyEachPass; |
| 509 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 510 | // The user has asked to use the new pass manager and provided a pipeline |
| 511 | // string. Hand off the rest of the functionality to the new code for that |
| 512 | // layer. |
Davide Italiano | c5d0a5c | 2016-09-07 00:48:47 +0000 | [diff] [blame] | 513 | return runPassPipeline(argv[0], *M, TM.get(), Out.get(), |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 514 | PassPipeline, OK, VK, PreserveAssemblyUseListOrder, |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 515 | PreserveBitcodeUseListOrder, EmitSummaryIndex, |
| 516 | EmitModuleHash) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 517 | ? 0 |
| 518 | : 1; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 519 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 520 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 521 | // Create a PassManager to hold and optimize the collection of passes we are |
Chris Lattner | 15c8b5e | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 522 | // about to build. |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 523 | // |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 524 | legacy::PassManager Passes; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 15c8b5e | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 526 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 527 | TargetLibraryInfoImpl TLII(ModuleTriple); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 1341df9 | 2011-02-18 22:34:03 +0000 | [diff] [blame] | 529 | // The -disable-simplify-libcalls flag actually disables all builtin optzns. |
| 530 | if (DisableSimplifyLibCalls) |
Chandler Carruth | c029186 | 2015-01-24 02:06:09 +0000 | [diff] [blame] | 531 | TLII.disableAllFunctions(); |
| 532 | Passes.add(new TargetLibraryInfoWrapperPass(TLII)); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 533 | |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 534 | // Add an appropriate DataLayout instance for this module. |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 535 | const DataLayout &DL = M->getDataLayout(); |
| 536 | if (DL.isDefault() && !DefaultDataLayout.empty()) { |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 537 | M->setDataLayout(DefaultDataLayout); |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 538 | } |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 539 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 540 | // Add internal analysis passes from the target machine. |
Chandler Carruth | 5ec2b1d | 2015-02-01 12:26:09 +0000 | [diff] [blame] | 541 | Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() |
| 542 | : TargetIRAnalysis())); |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 543 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 544 | std::unique_ptr<legacy::FunctionPassManager> FPasses; |
Gor Nishanov | f3bb361 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 545 | if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || |
| 546 | OptLevelO3) { |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 547 | FPasses.reset(new legacy::FunctionPassManager(M.get())); |
Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 548 | FPasses->add(createTargetTransformInfoWrapperPass( |
Chandler Carruth | 5ec2b1d | 2015-02-01 12:26:09 +0000 | [diff] [blame] | 549 | TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis())); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 550 | } |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 551 | |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 552 | if (PrintBreakpoints) { |
| 553 | // Default to standard output. |
| 554 | if (!Out) { |
| 555 | if (OutputFilename.empty()) |
| 556 | OutputFilename = "-"; |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 557 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 558 | std::error_code EC; |
Craig Topper | c5c52f4 | 2014-12-12 07:52:14 +0000 | [diff] [blame] | 559 | Out = llvm::make_unique<tool_output_file>(OutputFilename, EC, |
| 560 | sys::fs::F_None); |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 561 | if (EC) { |
| 562 | errs() << EC.message() << '\n'; |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 563 | return 1; |
| 564 | } |
| 565 | } |
Eli Bendersky | 54dc283 | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 566 | Passes.add(createBreakpointPrinter(Out->os())); |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 567 | NoOutput = true; |
| 568 | } |
| 569 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 570 | // Create a new optimization pass for each one specified on the command line |
| 571 | for (unsigned i = 0; i < PassList.size(); ++i) { |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 572 | if (StandardLinkOpts && |
| 573 | StandardLinkOpts.getPosition() < PassList.getPosition(i)) { |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 574 | AddStandardLinkPasses(Passes); |
| 575 | StandardLinkOpts = false; |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 576 | } |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 577 | |
Gor Nishanov | f3bb361 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 578 | if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) { |
| 579 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); |
| 580 | OptLevelO0 = false; |
| 581 | } |
| 582 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 583 | if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 584 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 585 | OptLevelO1 = false; |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 586 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 587 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 588 | if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 589 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 590 | OptLevelO2 = false; |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 591 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 592 | |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 593 | if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 594 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 595 | OptLevelOs = false; |
| 596 | } |
| 597 | |
| 598 | if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 599 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 600 | OptLevelOz = false; |
| 601 | } |
| 602 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 603 | if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 604 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 605 | OptLevelO3 = false; |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 606 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 607 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 608 | const PassInfo *PassInf = PassList[i]; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 609 | Pass *P = nullptr; |
Quentin Colombet | dc0b2ea | 2014-01-16 21:44:34 +0000 | [diff] [blame] | 610 | if (PassInf->getTargetMachineCtor()) |
| 611 | P = PassInf->getTargetMachineCtor()(TM.get()); |
| 612 | else if (PassInf->getNormalCtor()) |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 613 | P = PassInf->getNormalCtor()(); |
| 614 | else |
| 615 | errs() << argv[0] << ": cannot create pass: " |
| 616 | << PassInf->getPassName() << "\n"; |
| 617 | if (P) { |
Benjamin Kramer | 32c3d30 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 618 | PassKind Kind = P->getPassKind(); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 619 | addPass(Passes, P); |
| 620 | |
| 621 | if (AnalyzeOnly) { |
Benjamin Kramer | 32c3d30 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 622 | switch (Kind) { |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 623 | case PT_BasicBlock: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 624 | Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 625 | break; |
Tobias Grosser | 23c8341 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 626 | case PT_Region: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 627 | Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet)); |
Tobias Grosser | 23c8341 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 628 | break; |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 629 | case PT_Loop: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 630 | Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 631 | break; |
| 632 | case PT_Function: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 633 | Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 634 | break; |
| 635 | case PT_CallGraphSCC: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 636 | Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 637 | break; |
| 638 | default: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 639 | Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 640 | break; |
| 641 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 642 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 645 | if (PrintEachXForm) |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 646 | Passes.add( |
| 647 | createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder)); |
Chris Lattner | c90d6ba | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 649 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 650 | if (StandardLinkOpts) { |
| 651 | AddStandardLinkPasses(Passes); |
| 652 | StandardLinkOpts = false; |
| 653 | } |
| 654 | |
Gor Nishanov | f3bb361 | 2016-08-05 16:27:33 +0000 | [diff] [blame] | 655 | if (OptLevelO0) |
| 656 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); |
| 657 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 658 | if (OptLevelO1) |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 659 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 660 | |
| 661 | if (OptLevelO2) |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 662 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 663 | |
| 664 | if (OptLevelOs) |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 665 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 666 | |
| 667 | if (OptLevelOz) |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 668 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 669 | |
| 670 | if (OptLevelO3) |
Justin Lebar | 6e1089b | 2016-04-27 19:08:24 +0000 | [diff] [blame] | 671 | AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 672 | |
Patrik Hagglund | 26daabe | 2016-06-15 10:32:00 +0000 | [diff] [blame] | 673 | if (FPasses) { |
Chris Lattner | ef8cf6d | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 674 | FPasses->doInitialization(); |
Craig Topper | dbe2d2e | 2014-12-12 07:52:11 +0000 | [diff] [blame] | 675 | for (Function &F : *M) |
| 676 | FPasses->run(F); |
Chris Lattner | ef8cf6d | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 677 | FPasses->doFinalization(); |
| 678 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 679 | |
| 680 | // Check that the module is well formed on completion of optimization |
Duncan P. N. Exon Smith | ab58a56 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 681 | if (!NoVerify && !VerifyEach) |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 682 | Passes.add(createVerifierPass()); |
| 683 | |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 684 | // In run twice mode, we want to make sure the output is bit-by-bit |
| 685 | // equivalent if we run the pass manager again, so setup two buffers and |
| 686 | // a stream to write to them. Note that llc does something similar and it |
| 687 | // may be worth to abstract this out in the future. |
| 688 | SmallVector<char, 0> Buffer; |
| 689 | SmallVector<char, 0> CompileTwiceBuffer; |
| 690 | std::unique_ptr<raw_svector_ostream> BOS; |
Keno Fischer | 38707c4 | 2015-12-05 00:06:37 +0000 | [diff] [blame] | 691 | raw_ostream *OS = nullptr; |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 692 | |
Dan Gohman | 083330a | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 693 | // Write bitcode or assembly to the output as the last step... |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 694 | if (!NoOutput && !AnalyzeOnly) { |
Keno Fischer | 38707c4 | 2015-12-05 00:06:37 +0000 | [diff] [blame] | 695 | assert(Out); |
| 696 | OS = &Out->os(); |
| 697 | if (RunTwice) { |
| 698 | BOS = make_unique<raw_svector_ostream>(Buffer); |
| 699 | OS = BOS.get(); |
| 700 | } |
Mehdi Amini | fe6df26 | 2016-04-13 17:20:10 +0000 | [diff] [blame] | 701 | if (OutputAssembly) { |
| 702 | if (EmitSummaryIndex) |
| 703 | report_fatal_error("Text output is incompatible with -module-summary"); |
| 704 | if (EmitModuleHash) |
| 705 | report_fatal_error("Text output is incompatible with -module-hash"); |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 706 | Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder)); |
Mehdi Amini | fe6df26 | 2016-04-13 17:20:10 +0000 | [diff] [blame] | 707 | } else |
Mehdi Amini | 68da426 | 2016-04-12 21:35:18 +0000 | [diff] [blame] | 708 | Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder, |
| 709 | EmitSummaryIndex, EmitModuleHash)); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Andrew Trick | 1200401 | 2011-04-05 18:54:36 +0000 | [diff] [blame] | 712 | // Before executing passes, print the final values of the LLVM options. |
| 713 | cl::PrintOptionValues(); |
| 714 | |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 715 | // If requested, run all passes again with the same pass manager to catch |
| 716 | // bugs caused by persistent state in the passes |
| 717 | if (RunTwice) { |
Keno Fischer | e54f58c | 2015-12-05 01:38:12 +0000 | [diff] [blame] | 718 | std::unique_ptr<Module> M2(CloneModule(M.get())); |
| 719 | Passes.run(*M2); |
| 720 | CompileTwiceBuffer = Buffer; |
| 721 | Buffer.clear(); |
| 722 | } |
| 723 | |
| 724 | // Now that we have all of the passes ready, run them. |
| 725 | Passes.run(*M); |
| 726 | |
| 727 | // Compare the two outputs and make sure they're the same |
| 728 | if (RunTwice) { |
Keno Fischer | 38707c4 | 2015-12-05 00:06:37 +0000 | [diff] [blame] | 729 | assert(Out); |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 730 | if (Buffer.size() != CompileTwiceBuffer.size() || |
| 731 | (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) != |
| 732 | 0)) { |
| 733 | errs() << "Running the pass manager twice changed the output.\n" |
Keno Fischer | e54f58c | 2015-12-05 01:38:12 +0000 | [diff] [blame] | 734 | "Writing the result of the second run to the specified output.\n" |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 735 | "To generate the one-run comparison binary, just run without\n" |
| 736 | "the compile-twice option\n"; |
| 737 | Out->os() << BOS->str(); |
| 738 | Out->keep(); |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 739 | if (YamlFile) |
| 740 | YamlFile->keep(); |
Keno Fischer | 04464cf | 2015-12-04 21:56:46 +0000 | [diff] [blame] | 741 | return 1; |
| 742 | } |
| 743 | Out->os() << BOS->str(); |
| 744 | } |
| 745 | |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 746 | // Declare success. |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 747 | if (!NoOutput || PrintBreakpoints) |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 748 | Out->keep(); |
| 749 | |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 750 | if (YamlFile) |
| 751 | YamlFile->keep(); |
| 752 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 753 | return 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 754 | } |