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 | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 23 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/CommandFlags.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 26 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 27 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 1b69ed8 | 2014-03-04 12:32:42 +0000 | [diff] [blame] | 28 | #include "llvm/IR/LegacyPassNameParser.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Module.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 31 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 32 | #include "llvm/InitializePasses.h" |
Jakub Staszak | 63e77d5 | 2013-01-10 21:56:40 +0000 | [diff] [blame] | 33 | #include "llvm/LinkAllIR.h" |
Chandler Carruth | 1fe21fc | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 34 | #include "llvm/LinkAllPasses.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 35 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 36 | #include "llvm/PassManager.h" |
David Greene | ed8a1de | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 38 | #include "llvm/Support/FileSystem.h" |
Chris Lattner | 76d4632 | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 40 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 4b2a6e2 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 41 | #include "llvm/Support/PrettyStackTrace.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Signals.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 43 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 44 | #include "llvm/Support/SystemUtils.h" |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 45 | #include "llvm/Support/TargetRegistry.h" |
Nadav Rotem | ac9a344 | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 46 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ToolOutputFile.h" |
| 48 | #include "llvm/Target/TargetLibraryInfo.h" |
| 49 | #include "llvm/Target/TargetMachine.h" |
Rafael Espindola | 591eaa4 | 2011-08-02 21:50:24 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 51 | #include <algorithm> |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 52 | #include <memory> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 53 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 54 | using namespace opt_tool; |
Chris Lattner | b86b11a | 2002-04-12 18:21:13 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 56 | // The OptimizationList is automatically populated with registered Passes by the |
| 57 | // PassNameParser. |
| 58 | // |
Chris Lattner | 8e5e13b | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 59 | static cl::list<const PassInfo*, bool, PassNameParser> |
| 60 | PassList(cl::desc("Optimizations available:")); |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 62 | // This flag specifies a textual description of the optimization pass pipeline |
| 63 | // to run over the module. This flag switches opt to use the new pass manager |
| 64 | // infrastructure, completely disabling all of the flags specific to the old |
| 65 | // pass management. |
| 66 | static cl::opt<std::string> PassPipeline( |
| 67 | "passes", |
| 68 | cl::desc("A textual description of the pass pipeline for optimizing"), |
| 69 | cl::Hidden); |
| 70 | |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 71 | // Other command line options... |
Chris Lattner | c90d6ba | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 72 | // |
Chris Lattner | 02a1683 | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 73 | static cl::opt<std::string> |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 74 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 75 | cl::init("-"), cl::value_desc("filename")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 02a1683 | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 77 | static cl::opt<std::string> |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 78 | OutputFilename("o", cl::desc("Override output filename"), |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 79 | cl::value_desc("filename")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 80 | |
| 81 | static cl::opt<bool> |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 82 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 83 | |
| 84 | static cl::opt<bool> |
| 85 | PrintEachXForm("p", cl::desc("Print module after each transformation")); |
| 86 | |
| 87 | static cl::opt<bool> |
Chris Lattner | 30f40d9 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 88 | NoOutput("disable-output", |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 89 | cl::desc("Do not write result bitcode file"), cl::Hidden); |
Chris Lattner | 4dbe59b | 2003-02-12 18:43:33 +0000 | [diff] [blame] | 90 | |
| 91 | static cl::opt<bool> |
Duncan Sands | 6f2ffce | 2009-10-14 20:01:39 +0000 | [diff] [blame] | 92 | OutputAssembly("S", cl::desc("Write output as LLVM assembly")); |
Daniel Dunbar | 6b3153b | 2009-09-05 11:34:53 +0000 | [diff] [blame] | 93 | |
| 94 | static cl::opt<bool> |
Chris Lattner | 30f40d9 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 95 | NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden); |
Chris Lattner | b8450599 | 2003-02-12 18:45:08 +0000 | [diff] [blame] | 96 | |
| 97 | static cl::opt<bool> |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 98 | VerifyEach("verify-each", cl::desc("Verify after each transform")); |
| 99 | |
| 100 | static cl::opt<bool> |
| 101 | StripDebug("strip-debug", |
| 102 | cl::desc("Strip debugger symbol info from translation unit")); |
| 103 | |
| 104 | static cl::opt<bool> |
| 105 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| 106 | |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 107 | static cl::opt<bool> |
| 108 | DisableOptimizations("disable-opt", |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 109 | cl::desc("Do not run any optimization passes")); |
| 110 | |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 111 | static cl::opt<bool> |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 112 | StandardCompileOpts("std-compile-opts", |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 113 | cl::desc("Include the standard compile time optimizations")); |
| 114 | |
| 115 | static cl::opt<bool> |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 116 | StandardLinkOpts("std-link-opts", |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 117 | cl::desc("Include the standard link time optimizations")); |
| 118 | |
| 119 | static cl::opt<bool> |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 120 | OptLevelO1("O1", |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 121 | cl::desc("Optimization level 1. Similar to clang -O1")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 122 | |
| 123 | static cl::opt<bool> |
| 124 | OptLevelO2("O2", |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 125 | cl::desc("Optimization level 2. Similar to clang -O2")); |
| 126 | |
| 127 | static cl::opt<bool> |
| 128 | OptLevelOs("Os", |
| 129 | cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); |
| 130 | |
| 131 | static cl::opt<bool> |
| 132 | OptLevelOz("Oz", |
| 133 | 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] | 134 | |
| 135 | static cl::opt<bool> |
| 136 | OptLevelO3("O3", |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 137 | cl::desc("Optimization level 3. Similar to clang -O3")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 138 | |
Joe Groff | 1b73869 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 139 | static cl::opt<std::string> |
| 140 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 141 | |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 142 | static cl::opt<bool> |
| 143 | UnitAtATime("funit-at-a-time", |
Rafael Espindola | 40bfd6d | 2014-08-21 19:22:24 +0000 | [diff] [blame] | 144 | 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] | 145 | cl::init(true)); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 146 | |
| 147 | static cl::opt<bool> |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 148 | DisableLoopUnrolling("disable-loop-unrolling", |
| 149 | cl::desc("Disable loop unrolling in all relevant passes"), |
| 150 | cl::init(false)); |
Arnold Schwaighofer | 46db725 | 2013-12-03 16:33:06 +0000 | [diff] [blame] | 151 | static cl::opt<bool> |
| 152 | DisableLoopVectorization("disable-loop-vectorization", |
| 153 | cl::desc("Disable the loop vectorization pass"), |
| 154 | cl::init(false)); |
| 155 | |
| 156 | static cl::opt<bool> |
| 157 | DisableSLPVectorization("disable-slp-vectorization", |
| 158 | cl::desc("Disable the slp vectorization pass"), |
| 159 | cl::init(false)); |
| 160 | |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 161 | |
| 162 | static cl::opt<bool> |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 163 | DisableSimplifyLibCalls("disable-simplify-libcalls", |
Devang Patel | 7293f0f | 2008-09-17 16:01:39 +0000 | [diff] [blame] | 164 | cl::desc("Disable simplify-libcalls")); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 165 | |
| 166 | static cl::opt<bool> |
Chris Lattner | 1553edc | 2004-05-27 20:32:10 +0000 | [diff] [blame] | 167 | Quiet("q", cl::desc("Obsolete option"), cl::Hidden); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 168 | |
Reid Spencer | c8878ed | 2004-05-27 16:28:54 +0000 | [diff] [blame] | 169 | static cl::alias |
| 170 | QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); |
| 171 | |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 172 | static cl::opt<bool> |
| 173 | AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); |
| 174 | |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 175 | static cl::opt<bool> |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 176 | PrintBreakpoints("print-breakpoints-for-testing", |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 177 | cl::desc("Print select breakpoints location for testing")); |
| 178 | |
Chris Lattner | cfa1911 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 179 | static cl::opt<std::string> |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 180 | DefaultDataLayout("default-data-layout", |
Chris Lattner | cfa1911 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 181 | cl::desc("data layout string to use if not specified by module"), |
| 182 | cl::value_desc("layout-string"), cl::init("")); |
| 183 | |
Reid Spencer | 378f7d5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 184 | |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 186 | static inline void addPass(PassManagerBase &PM, Pass *P) { |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 187 | // Add the pass to the pass manager... |
| 188 | PM.add(P); |
| 189 | |
| 190 | // If we are verifying all of the intermediate steps, add the verifier... |
Duncan P. N. Exon Smith | 6ef5f28 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 191 | if (VerifyEach) { |
| 192 | PM.add(createVerifierPass()); |
| 193 | PM.add(createDebugInfoVerifierPass()); |
| 194 | } |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Rafael Espindola | 40bfd6d | 2014-08-21 19:22:24 +0000 | [diff] [blame] | 197 | /// This routine adds optimization passes based on selected optimization level, |
| 198 | /// OptLevel. |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 199 | /// |
| 200 | /// OptLevel - Optimization Level |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 201 | static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM, |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 202 | unsigned OptLevel, unsigned SizeLevel) { |
Duncan P. N. Exon Smith | 6ef5f28 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 203 | FPM.add(createVerifierPass()); // Verify that input is correct |
| 204 | MPM.add(createDebugInfoVerifierPass()); // Verify that debug info is correct |
Duncan Sands | 0f19e91 | 2011-12-07 17:14:20 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 206 | PassManagerBuilder Builder; |
| 207 | Builder.OptLevel = OptLevel; |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 208 | Builder.SizeLevel = SizeLevel; |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 209 | |
Eli Friedman | eb0c52f | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 210 | if (DisableInline) { |
| 211 | // No inlining pass |
Eli Friedman | b68fe25 | 2011-06-06 22:13:27 +0000 | [diff] [blame] | 212 | } else if (OptLevel > 1) { |
Eli Bendersky | 49f6565 | 2014-03-12 16:12:36 +0000 | [diff] [blame] | 213 | Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel); |
Eli Friedman | eb0c52f | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 214 | } else { |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 215 | Builder.Inliner = createAlwaysInlinerPass(); |
Eli Friedman | eb0c52f | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 217 | Builder.DisableUnitAtATime = !UnitAtATime; |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 218 | Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ? |
| 219 | DisableLoopUnrolling : OptLevel == 0; |
Greg Bedwell | 1411aeb | 2013-10-09 08:55:27 +0000 | [diff] [blame] | 220 | |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 221 | // This is final, unless there is a #pragma vectorize enable |
| 222 | if (DisableLoopVectorization) |
| 223 | Builder.LoopVectorize = false; |
| 224 | // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize) |
| 225 | else if (!Builder.LoopVectorize) |
| 226 | Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2; |
| 227 | |
| 228 | // When #pragma vectorize is on for SLP, do the same as above |
Arnold Schwaighofer | 46db725 | 2013-12-03 16:33:06 +0000 | [diff] [blame] | 229 | Builder.SLPVectorize = |
| 230 | DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; |
Hal Finkel | 6d09904 | 2013-08-28 18:33:10 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 232 | Builder.populateFunctionPassManager(FPM); |
| 233 | Builder.populateModulePassManager(MPM); |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 236 | static void AddStandardCompilePasses(PassManagerBase &PM) { |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 237 | PM.add(createVerifierPass()); // Verify that input is correct |
| 238 | |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 239 | // If the -strip-debug command line option was specified, do it. |
| 240 | if (StripDebug) |
| 241 | addPass(PM, createStripSymbolsPass(true)); |
| 242 | |
Duncan P. N. Exon Smith | 6ef5f28 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 243 | // Verify debug info only after it's (possibly) stripped. |
| 244 | PM.add(createDebugInfoVerifierPass()); |
| 245 | |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 246 | if (DisableOptimizations) return; |
| 247 | |
Daniel Dunbar | 94c7b79 | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 248 | // -std-compile-opts adds the same module passes as -O3. |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 249 | PassManagerBuilder Builder; |
| 250 | if (!DisableInline) |
| 251 | Builder.Inliner = createFunctionInliningPass(); |
| 252 | Builder.OptLevel = 3; |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 253 | Builder.populateModulePassManager(PM); |
Reid Spencer | 22dbfb6 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 256 | static void AddStandardLinkPasses(PassManagerBase &PM) { |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 257 | PassManagerBuilder Builder; |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame^] | 258 | Builder.VerifyInput = true; |
| 259 | Builder.StripDebug = StripDebug; |
| 260 | if (DisableOptimizations) |
| 261 | Builder.OptLevel = 0; |
| 262 | |
Rafael Espindola | e07caad | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 263 | if (!DisableInline) |
| 264 | Builder.Inliner = createFunctionInliningPass(); |
| 265 | Builder.populateLTOPassManager(PM); |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // CodeGen-related helper functions. |
| 270 | // |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 271 | |
| 272 | CodeGenOpt::Level GetCodeGenOptLevel() { |
| 273 | if (OptLevelO1) |
| 274 | return CodeGenOpt::Less; |
| 275 | if (OptLevelO2) |
| 276 | return CodeGenOpt::Default; |
| 277 | if (OptLevelO3) |
| 278 | return CodeGenOpt::Aggressive; |
| 279 | return CodeGenOpt::None; |
| 280 | } |
| 281 | |
| 282 | // Returns the TargetMachine instance or zero if no triple is provided. |
Nadav Rotem | b1615b1 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 283 | static TargetMachine* GetTargetMachine(Triple TheTriple) { |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 284 | std::string Error; |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 285 | const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, |
| 286 | Error); |
Nadav Rotem | b1615b1 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 287 | // Some modules don't specify a triple, and this is okay. |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 288 | if (!TheTarget) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 289 | return nullptr; |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 290 | } |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 291 | |
| 292 | // Package up features to be passed to target/subtarget |
| 293 | std::string FeaturesStr; |
| 294 | if (MAttrs.size()) { |
| 295 | SubtargetFeatures Features; |
| 296 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 297 | Features.AddFeature(MAttrs[i]); |
| 298 | FeaturesStr = Features.getString(); |
| 299 | } |
| 300 | |
| 301 | return TheTarget->createTargetMachine(TheTriple.getTriple(), |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 302 | MCPU, FeaturesStr, |
| 303 | InitTargetOptionsFromCodeGenFlags(), |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 304 | RelocModel, CMModel, |
| 305 | GetCodeGenOptLevel()); |
| 306 | } |
Chris Lattner | 4db2f2c | 2002-02-01 04:54:11 +0000 | [diff] [blame] | 307 | |
Sebastian Pop | a59005b | 2014-03-14 04:04:14 +0000 | [diff] [blame] | 308 | #ifdef LINK_POLLY_INTO_TOOLS |
| 309 | namespace polly { |
| 310 | void initializePollyPasses(llvm::PassRegistry &Registry); |
| 311 | } |
| 312 | #endif |
| 313 | |
Chris Lattner | 5a48a24 | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
| 315 | // main for opt |
| 316 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 317 | int main(int argc, char **argv) { |
Chris Lattner | 4b2a6e2 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 318 | sys::PrintStackTraceOnErrorSignal(); |
| 319 | llvm::PrettyStackTraceProgram X(argc, argv); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 320 | |
David Greene | ed8a1de | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 321 | // Enable debug stream buffering. |
| 322 | EnableDebugBuffering = true; |
| 323 | |
Chris Lattner | 4b2a6e2 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 324 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Owen Anderson | 19251ec | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 325 | LLVMContext &Context = getGlobalContext(); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 326 | |
Nadav Rotem | ac9a344 | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 327 | InitializeAllTargets(); |
| 328 | InitializeAllTargetMCs(); |
Tobias Grosser | 9190e0d | 2014-06-13 16:12:08 +0000 | [diff] [blame] | 329 | InitializeAllAsmPrinters(); |
Nadav Rotem | ac9a344 | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 330 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 331 | // Initialize passes |
| 332 | PassRegistry &Registry = *PassRegistry::getPassRegistry(); |
| 333 | initializeCore(Registry); |
Daniel Malea | 3c5bed1 | 2013-05-08 20:44:14 +0000 | [diff] [blame] | 334 | initializeDebugIRPass(Registry); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 335 | initializeScalarOpts(Registry); |
Michael Gottesman | 79d8d81 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 336 | initializeObjCARCOpts(Registry); |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 337 | initializeVectorization(Registry); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 338 | initializeIPO(Registry); |
| 339 | initializeAnalysis(Registry); |
| 340 | initializeIPA(Registry); |
| 341 | initializeTransformUtils(Registry); |
| 342 | initializeInstCombine(Registry); |
| 343 | initializeInstrumentation(Registry); |
| 344 | initializeTarget(Registry); |
Quentin Colombet | a349084 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 345 | // For codegen passes, only passes that do IR to IR transformation are |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 346 | // supported. |
Quentin Colombet | a349084 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 347 | initializeCodeGenPreparePass(Registry); |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 348 | initializeAtomicExpandLoadLinkedPass(Registry); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 349 | |
Sebastian Pop | a59005b | 2014-03-14 04:04:14 +0000 | [diff] [blame] | 350 | #ifdef LINK_POLLY_INTO_TOOLS |
| 351 | polly::initializePollyPasses(Registry); |
| 352 | #endif |
| 353 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 354 | cl::ParseCommandLineOptions(argc, argv, |
| 355 | "llvm .bc -> .bc modular optimizer and analysis printer\n"); |
Chris Lattner | c90d6ba | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 356 | |
Tobias Grosser | 083379f | 2010-12-02 20:35:16 +0000 | [diff] [blame] | 357 | if (AnalyzeOnly && NoOutput) { |
| 358 | errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; |
| 359 | return 1; |
| 360 | } |
| 361 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 362 | SMDiagnostic Err; |
Vikram S. Adve | 82491b7 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 363 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 364 | // Load the input module... |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 365 | std::unique_ptr<Module> M; |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 366 | M.reset(ParseIRFile(InputFilename, Err, Context)); |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 367 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 368 | if (!M.get()) { |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 369 | Err.print(argv[0], errs()); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 370 | return 1; |
| 371 | } |
| 372 | |
Joe Groff | 1b73869 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 373 | // If we are supposed to override the target triple, do so now. |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 374 | if (!TargetTriple.empty()) |
Joe Groff | 1b73869 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 375 | M->setTargetTriple(Triple::normalize(TargetTriple)); |
Eric Christopher | 1f14031 | 2013-04-14 23:35:36 +0000 | [diff] [blame] | 376 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 377 | // Figure out what stream we are supposed to write to... |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 378 | std::unique_ptr<tool_output_file> Out; |
Dan Gohman | 083330a | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 379 | if (NoOutput) { |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 380 | if (!OutputFilename.empty()) |
| 381 | errs() << "WARNING: The -o (output filename) option is ignored when\n" |
Dan Gohman | 083330a | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 382 | "the --disable-output option is used.\n"; |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 383 | } else { |
| 384 | // Default to standard output. |
| 385 | if (OutputFilename.empty()) |
| 386 | OutputFilename = "-"; |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 387 | |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 388 | std::string ErrorInfo; |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 389 | Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 390 | sys::fs::F_None)); |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 391 | if (!ErrorInfo.empty()) { |
| 392 | errs() << ErrorInfo << '\n'; |
Dan Gohman | b01aed1 | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 393 | return 1; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 395 | } |
Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 396 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 397 | // If the output is set to be emitted to standard out, and standard out is a |
| 398 | // console, print out a warning message and refuse to do it. We don't |
| 399 | // impress anyone by spewing tons of binary goo to a terminal. |
Dan Gohman | 915ad96 | 2010-01-17 17:47:24 +0000 | [diff] [blame] | 400 | if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 401 | if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 402 | NoOutput = true; |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 403 | |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 404 | if (PassPipeline.getNumOccurrences() > 0) { |
| 405 | OutputKind OK = OK_NoOutput; |
| 406 | if (!NoOutput) |
| 407 | OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode; |
| 408 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 409 | VerifierKind VK = VK_VerifyInAndOut; |
| 410 | if (NoVerify) |
| 411 | VK = VK_NoVerifier; |
| 412 | else if (VerifyEach) |
| 413 | VK = VK_VerifyEachPass; |
| 414 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 415 | // The user has asked to use the new pass manager and provided a pipeline |
| 416 | // string. Hand off the rest of the functionality to the new code for that |
| 417 | // layer. |
| 418 | return runPassPipeline(argv[0], Context, *M.get(), Out.get(), PassPipeline, |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 419 | OK, VK) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 420 | ? 0 |
| 421 | : 1; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 422 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 423 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 424 | // 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] | 425 | // about to build. |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 426 | // |
| 427 | PassManager Passes; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 15c8b5e | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 429 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
Chris Lattner | 1341df9 | 2011-02-18 22:34:03 +0000 | [diff] [blame] | 430 | TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple())); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 1341df9 | 2011-02-18 22:34:03 +0000 | [diff] [blame] | 432 | // The -disable-simplify-libcalls flag actually disables all builtin optzns. |
| 433 | if (DisableSimplifyLibCalls) |
| 434 | TLI->disableAllFunctions(); |
| 435 | Passes.add(TLI); |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 436 | |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 437 | // Add an appropriate DataLayout instance for this module. |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 438 | const DataLayout *DL = M.get()->getDataLayout(); |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 439 | if (!DL && !DefaultDataLayout.empty()) { |
| 440 | M->setDataLayout(DefaultDataLayout); |
| 441 | DL = M.get()->getDataLayout(); |
| 442 | } |
Eric Christopher | 13637e9 | 2013-04-15 07:31:37 +0000 | [diff] [blame] | 443 | |
Rafael Espindola | f193902 | 2014-02-21 02:01:42 +0000 | [diff] [blame] | 444 | if (DL) |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 445 | Passes.add(new DataLayoutPass(M.get())); |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 446 | |
Nadav Rotem | b1615b1 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 447 | Triple ModuleTriple(M->getTargetTriple()); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 448 | TargetMachine *Machine = nullptr; |
Nadav Rotem | b1615b1 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 449 | if (ModuleTriple.getArch()) |
| 450 | Machine = GetTargetMachine(Triple(ModuleTriple)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 451 | std::unique_ptr<TargetMachine> TM(Machine); |
Nadav Rotem | b1615b1 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 452 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 453 | // Add internal analysis passes from the target machine. |
| 454 | if (TM.get()) |
| 455 | TM->addAnalysisPasses(Passes); |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 456 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 457 | std::unique_ptr<FunctionPassManager> FPasses; |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 458 | if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 459 | FPasses.reset(new FunctionPassManager(M.get())); |
Rafael Espindola | f193902 | 2014-02-21 02:01:42 +0000 | [diff] [blame] | 460 | if (DL) |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 461 | FPasses->add(new DataLayoutPass(M.get())); |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 462 | if (TM.get()) |
| 463 | TM->addAnalysisPasses(*FPasses); |
| 464 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 465 | } |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 466 | |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 467 | if (PrintBreakpoints) { |
| 468 | // Default to standard output. |
| 469 | if (!Out) { |
| 470 | if (OutputFilename.empty()) |
| 471 | OutputFilename = "-"; |
Andrew Trick | b826ae8 | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 472 | |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 473 | std::string ErrorInfo; |
| 474 | Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 475 | sys::fs::F_None)); |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 476 | if (!ErrorInfo.empty()) { |
| 477 | errs() << ErrorInfo << '\n'; |
| 478 | return 1; |
| 479 | } |
| 480 | } |
Eli Bendersky | 54dc283 | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 481 | Passes.add(createBreakpointPrinter(Out->os())); |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 482 | NoOutput = true; |
| 483 | } |
| 484 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 485 | // If the -strip-debug command line option was specified, add it. If |
| 486 | // -std-compile-opts was also specified, it will handle StripDebug. |
| 487 | if (StripDebug && !StandardCompileOpts) |
| 488 | addPass(Passes, createStripSymbolsPass(true)); |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 489 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 490 | // Create a new optimization pass for each one specified on the command line |
| 491 | for (unsigned i = 0; i < PassList.size(); ++i) { |
| 492 | // Check to see if -std-compile-opts was specified before this option. If |
| 493 | // so, handle it. |
| 494 | if (StandardCompileOpts && |
| 495 | StandardCompileOpts.getPosition() < PassList.getPosition(i)) { |
Chris Lattner | 501c3b6 | 2008-07-13 19:35:21 +0000 | [diff] [blame] | 496 | AddStandardCompilePasses(Passes); |
| 497 | StandardCompileOpts = false; |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 498 | } |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 499 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 500 | if (StandardLinkOpts && |
| 501 | StandardLinkOpts.getPosition() < PassList.getPosition(i)) { |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 502 | AddStandardLinkPasses(Passes); |
| 503 | StandardLinkOpts = false; |
Eric Christopher | e64061f | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 504 | } |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 505 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 506 | if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 507 | AddOptimizationPasses(Passes, *FPasses, 1, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 508 | OptLevelO1 = false; |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 509 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 510 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 511 | if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 512 | AddOptimizationPasses(Passes, *FPasses, 2, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 513 | OptLevelO2 = false; |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 514 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 515 | |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 516 | if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { |
| 517 | AddOptimizationPasses(Passes, *FPasses, 2, 1); |
| 518 | OptLevelOs = false; |
| 519 | } |
| 520 | |
| 521 | if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { |
| 522 | AddOptimizationPasses(Passes, *FPasses, 2, 2); |
| 523 | OptLevelOz = false; |
| 524 | } |
| 525 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 526 | if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 527 | AddOptimizationPasses(Passes, *FPasses, 3, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 528 | OptLevelO3 = false; |
Daniel Dunbar | 41f9a3a | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 529 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 530 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 531 | const PassInfo *PassInf = PassList[i]; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 532 | Pass *P = nullptr; |
Quentin Colombet | dc0b2ea | 2014-01-16 21:44:34 +0000 | [diff] [blame] | 533 | if (PassInf->getTargetMachineCtor()) |
| 534 | P = PassInf->getTargetMachineCtor()(TM.get()); |
| 535 | else if (PassInf->getNormalCtor()) |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 536 | P = PassInf->getNormalCtor()(); |
| 537 | else |
| 538 | errs() << argv[0] << ": cannot create pass: " |
| 539 | << PassInf->getPassName() << "\n"; |
| 540 | if (P) { |
Benjamin Kramer | 32c3d30 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 541 | PassKind Kind = P->getPassKind(); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 542 | addPass(Passes, P); |
| 543 | |
| 544 | if (AnalyzeOnly) { |
Benjamin Kramer | 32c3d30 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 545 | switch (Kind) { |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 546 | case PT_BasicBlock: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 547 | Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 548 | break; |
Tobias Grosser | 23c8341 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 549 | case PT_Region: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 550 | Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet)); |
Tobias Grosser | 23c8341 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 551 | break; |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 552 | case PT_Loop: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 553 | Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 554 | break; |
| 555 | case PT_Function: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 556 | Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 557 | break; |
| 558 | case PT_CallGraphSCC: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 559 | Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 560 | break; |
| 561 | default: |
Eli Bendersky | b60f838 | 2014-02-10 23:34:23 +0000 | [diff] [blame] | 562 | Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet)); |
Chris Lattner | c7a8eaf | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 563 | break; |
| 564 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 565 | } |
Devang Patel | 9966ccf | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 568 | if (PrintEachXForm) |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 569 | Passes.add(createPrintModulePass(errs())); |
Chris Lattner | c90d6ba | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 570 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 571 | |
| 572 | // If -std-compile-opts was specified at the end of the pass list, add them. |
| 573 | if (StandardCompileOpts) { |
| 574 | AddStandardCompilePasses(Passes); |
| 575 | StandardCompileOpts = false; |
| 576 | } |
| 577 | |
| 578 | if (StandardLinkOpts) { |
| 579 | AddStandardLinkPasses(Passes); |
| 580 | StandardLinkOpts = false; |
| 581 | } |
| 582 | |
| 583 | if (OptLevelO1) |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 584 | AddOptimizationPasses(Passes, *FPasses, 1, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 585 | |
| 586 | if (OptLevelO2) |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 587 | AddOptimizationPasses(Passes, *FPasses, 2, 0); |
| 588 | |
| 589 | if (OptLevelOs) |
| 590 | AddOptimizationPasses(Passes, *FPasses, 2, 1); |
| 591 | |
| 592 | if (OptLevelOz) |
| 593 | AddOptimizationPasses(Passes, *FPasses, 2, 2); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 594 | |
| 595 | if (OptLevelO3) |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 596 | AddOptimizationPasses(Passes, *FPasses, 3, 0); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 597 | |
Chandler Carruth | d8c08c2 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 598 | if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { |
Chris Lattner | ef8cf6d | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 599 | FPasses->doInitialization(); |
Chris Lattner | 3d70add | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 600 | for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F) |
| 601 | FPasses->run(*F); |
Chris Lattner | ef8cf6d | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 602 | FPasses->doFinalization(); |
| 603 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 604 | |
| 605 | // Check that the module is well formed on completion of optimization |
Duncan P. N. Exon Smith | 6ef5f28 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 606 | if (!NoVerify && !VerifyEach) { |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 607 | Passes.add(createVerifierPass()); |
Duncan P. N. Exon Smith | 6ef5f28 | 2014-04-15 16:27:38 +0000 | [diff] [blame] | 608 | Passes.add(createDebugInfoVerifierPass()); |
| 609 | } |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 610 | |
Dan Gohman | 083330a | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 611 | // Write bitcode or assembly to the output as the last step... |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 612 | if (!NoOutput && !AnalyzeOnly) { |
| 613 | if (OutputAssembly) |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 614 | Passes.add(createPrintModulePass(Out->os())); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 615 | else |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 616 | Passes.add(createBitcodeWriterPass(Out->os())); |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Andrew Trick | 1200401 | 2011-04-05 18:54:36 +0000 | [diff] [blame] | 619 | // Before executing passes, print the final values of the LLVM options. |
| 620 | cl::PrintOptionValues(); |
| 621 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 622 | // Now that we have all of the passes ready, run them. |
| 623 | Passes.run(*M.get()); |
| 624 | |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 625 | // Declare success. |
Devang Patel | 786a05e | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 626 | if (!NoOutput || PrintBreakpoints) |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 627 | Out->keep(); |
| 628 | |
Chris Lattner | cc81526 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 629 | return 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 630 | } |