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