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