Chris Lattner | 76351aa | 2004-04-02 05:06:57 +0000 | [diff] [blame] | 1 | //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | // Optimizations may be specified an arbitrary number of times on the command |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 11 | // line, They are run in the order specified. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | 0eafc31 | 2001-10-18 06:05:15 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
Devang Patel | 1be9980 | 2011-04-04 19:51:17 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSet.h" |
Chris Lattner | 2a66aca | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 3251e81 | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopPass.h" |
| 21 | #include "llvm/Analysis/RegionPass.h" |
| 22 | #include "llvm/Analysis/Verifier.h" |
| 23 | #include "llvm/Assembly/PrintModulePass.h" |
| 24 | #include "llvm/Bitcode/ReaderWriter.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/CommandFlags.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 29 | #include "llvm/IRReader/IRReader.h" |
Jakub Staszak | 446991d | 2013-01-10 21:56:40 +0000 | [diff] [blame] | 30 | #include "llvm/LinkAllIR.h" |
Chandler Carruth | 90230c8 | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 31 | #include "llvm/LinkAllPasses.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 32 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 33 | #include "llvm/PassManager.h" |
David Greene | 08fc0d3 | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 36 | #include "llvm/Support/PassNameParser.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 37 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | c0d91b7 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 38 | #include "llvm/Support/PrettyStackTrace.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Signals.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 40 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 41 | #include "llvm/Support/SystemUtils.h" |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 42 | #include "llvm/Support/TargetRegistry.h" |
Nadav Rotem | 0873bea | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 43 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ToolOutputFile.h" |
| 45 | #include "llvm/Target/TargetLibraryInfo.h" |
| 46 | #include "llvm/Target/TargetMachine.h" |
Rafael Espindola | 3d453ac | 2011-08-02 21:50:24 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 49 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 50 | using namespace llvm; |
Chris Lattner | 9d6e7eb | 2002-04-12 18:21:13 +0000 | [diff] [blame] | 51 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 52 | // The OptimizationList is automatically populated with registered Passes by the |
| 53 | // PassNameParser. |
| 54 | // |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 55 | static cl::list<const PassInfo*, bool, PassNameParser> |
| 56 | PassList(cl::desc("Optimizations available:")); |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 57 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 58 | // Other command line options... |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 59 | // |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 60 | static cl::opt<std::string> |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 61 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 62 | cl::init("-"), cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 64 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 65 | OutputFilename("o", cl::desc("Override output filename"), |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 66 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 67 | |
| 68 | static cl::opt<bool> |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 69 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 70 | |
| 71 | static cl::opt<bool> |
| 72 | PrintEachXForm("p", cl::desc("Print module after each transformation")); |
| 73 | |
| 74 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 75 | NoOutput("disable-output", |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 76 | cl::desc("Do not write result bitcode file"), cl::Hidden); |
Chris Lattner | d70b68e | 2003-02-12 18:43:33 +0000 | [diff] [blame] | 77 | |
| 78 | static cl::opt<bool> |
Duncan Sands | 81b0b64 | 2009-10-14 20:01:39 +0000 | [diff] [blame] | 79 | OutputAssembly("S", cl::desc("Write output as LLVM assembly")); |
Daniel Dunbar | 8c042c2 | 2009-09-05 11:34:53 +0000 | [diff] [blame] | 80 | |
| 81 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 82 | NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden); |
Chris Lattner | f3bafc1 | 2003-02-12 18:45:08 +0000 | [diff] [blame] | 83 | |
| 84 | static cl::opt<bool> |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 85 | VerifyEach("verify-each", cl::desc("Verify after each transform")); |
| 86 | |
| 87 | static cl::opt<bool> |
| 88 | StripDebug("strip-debug", |
| 89 | cl::desc("Strip debugger symbol info from translation unit")); |
| 90 | |
| 91 | static cl::opt<bool> |
| 92 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| 93 | |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 94 | static cl::opt<bool> |
| 95 | DisableOptimizations("disable-opt", |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 96 | cl::desc("Do not run any optimization passes")); |
| 97 | |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 98 | static cl::opt<bool> |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 99 | DisableInternalize("disable-internalize", |
| 100 | cl::desc("Do not mark all symbols as internal")); |
| 101 | |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 102 | static cl::opt<bool> |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 103 | StandardCompileOpts("std-compile-opts", |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 104 | cl::desc("Include the standard compile time optimizations")); |
| 105 | |
| 106 | static cl::opt<bool> |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 107 | StandardLinkOpts("std-link-opts", |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 108 | cl::desc("Include the standard link time optimizations")); |
| 109 | |
| 110 | static cl::opt<bool> |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 111 | OptLevelO1("O1", |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 112 | cl::desc("Optimization level 1. Similar to clang -O1")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 113 | |
| 114 | static cl::opt<bool> |
| 115 | OptLevelO2("O2", |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 116 | cl::desc("Optimization level 2. Similar to clang -O2")); |
| 117 | |
| 118 | static cl::opt<bool> |
| 119 | OptLevelOs("Os", |
| 120 | cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); |
| 121 | |
| 122 | static cl::opt<bool> |
| 123 | OptLevelOz("Oz", |
| 124 | cl::desc("Like -Os but reduces code size further. Similar to clang -Oz")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 125 | |
| 126 | static cl::opt<bool> |
| 127 | OptLevelO3("O3", |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 128 | cl::desc("Optimization level 3. Similar to clang -O3")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 129 | |
Joe Groff | e652b52 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 130 | static cl::opt<std::string> |
| 131 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 132 | |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 133 | static cl::opt<bool> |
| 134 | UnitAtATime("funit-at-a-time", |
Eric Christopher | c4769ba | 2009-08-21 23:30:30 +0000 | [diff] [blame] | 135 | cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 136 | cl::init(true)); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 137 | |
| 138 | static cl::opt<bool> |
| 139 | DisableSimplifyLibCalls("disable-simplify-libcalls", |
Devang Patel | 442b1aa | 2008-09-17 16:01:39 +0000 | [diff] [blame] | 140 | cl::desc("Disable simplify-libcalls")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 141 | |
| 142 | static cl::opt<bool> |
Chris Lattner | 3153e4f | 2004-05-27 20:32:10 +0000 | [diff] [blame] | 143 | Quiet("q", cl::desc("Obsolete option"), cl::Hidden); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 144 | |
Reid Spencer | ec7eb45 | 2004-05-27 16:28:54 +0000 | [diff] [blame] | 145 | static cl::alias |
| 146 | QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); |
| 147 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 148 | static cl::opt<bool> |
| 149 | AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); |
| 150 | |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 151 | static cl::opt<bool> |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 152 | PrintBreakpoints("print-breakpoints-for-testing", |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 153 | cl::desc("Print select breakpoints location for testing")); |
| 154 | |
Chris Lattner | d331cb3 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 155 | static cl::opt<std::string> |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 156 | DefaultDataLayout("default-data-layout", |
Chris Lattner | d331cb3 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 157 | cl::desc("data layout string to use if not specified by module"), |
| 158 | cl::value_desc("layout-string"), cl::init("")); |
| 159 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 160 | // ---------- Define Printers for module and function passes ------------ |
| 161 | namespace { |
| 162 | |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 163 | struct CallGraphSCCPassPrinter : public CallGraphSCCPass { |
| 164 | static char ID; |
| 165 | const PassInfo *PassToPrint; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 166 | raw_ostream &Out; |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 167 | std::string PassName; |
| 168 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 169 | CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) : |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 170 | CallGraphSCCPass(ID), PassToPrint(PI), Out(out) { |
| 171 | std::string PassToPrintName = PassToPrint->getPassName(); |
| 172 | PassName = "CallGraphSCCPass Printer: " + PassToPrintName; |
| 173 | } |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 175 | virtual bool runOnSCC(CallGraphSCC &SCC) { |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 176 | if (!Quiet) |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 177 | Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 178 | |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 179 | // Get and print pass... |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 180 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 181 | Function *F = (*I)->getFunction(); |
| 182 | if (F) |
| 183 | getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, |
| 184 | F->getParent()); |
| 185 | } |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 188 | |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 189 | virtual const char *getPassName() const { return PassName.c_str(); } |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 190 | |
| 191 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 192 | AU.addRequiredID(PassToPrint->getTypeInfo()); |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 193 | AU.setPreservesAll(); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | char CallGraphSCCPassPrinter::ID = 0; |
| 198 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 199 | struct ModulePassPrinter : public ModulePass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 200 | static char ID; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 201 | const PassInfo *PassToPrint; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 202 | raw_ostream &Out; |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 203 | std::string PassName; |
| 204 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 205 | ModulePassPrinter(const PassInfo *PI, raw_ostream &out) |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 206 | : ModulePass(ID), PassToPrint(PI), Out(out) { |
| 207 | std::string PassToPrintName = PassToPrint->getPassName(); |
| 208 | PassName = "ModulePass Printer: " + PassToPrintName; |
| 209 | } |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 210 | |
| 211 | virtual bool runOnModule(Module &M) { |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 212 | if (!Quiet) |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 213 | Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 214 | |
| 215 | // Get and print pass... |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 216 | getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 220 | virtual const char *getPassName() const { return PassName.c_str(); } |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 221 | |
| 222 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 223 | AU.addRequiredID(PassToPrint->getTypeInfo()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 224 | AU.setPreservesAll(); |
| 225 | } |
| 226 | }; |
| 227 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 228 | char ModulePassPrinter::ID = 0; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 229 | struct FunctionPassPrinter : public FunctionPass { |
| 230 | const PassInfo *PassToPrint; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 231 | raw_ostream &Out; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 232 | static char ID; |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 233 | std::string PassName; |
| 234 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 235 | FunctionPassPrinter(const PassInfo *PI, raw_ostream &out) |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 236 | : FunctionPass(ID), PassToPrint(PI), Out(out) { |
| 237 | std::string PassToPrintName = PassToPrint->getPassName(); |
| 238 | PassName = "FunctionPass Printer: " + PassToPrintName; |
| 239 | } |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 240 | |
| 241 | virtual bool runOnFunction(Function &F) { |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 242 | if (!Quiet) |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 243 | Out << "Printing analysis '" << PassToPrint->getPassName() |
| 244 | << "' for function '" << F.getName() << "':\n"; |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 245 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 246 | // Get and print pass... |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 247 | getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 248 | F.getParent()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
| 251 | |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 252 | virtual const char *getPassName() const { return PassName.c_str(); } |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 253 | |
| 254 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 255 | AU.addRequiredID(PassToPrint->getTypeInfo()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 256 | AU.setPreservesAll(); |
| 257 | } |
| 258 | }; |
| 259 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 260 | char FunctionPassPrinter::ID = 0; |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 261 | |
| 262 | struct LoopPassPrinter : public LoopPass { |
| 263 | static char ID; |
| 264 | const PassInfo *PassToPrint; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 265 | raw_ostream &Out; |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 266 | std::string PassName; |
| 267 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 268 | LoopPassPrinter(const PassInfo *PI, raw_ostream &out) : |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 269 | LoopPass(ID), PassToPrint(PI), Out(out) { |
| 270 | std::string PassToPrintName = PassToPrint->getPassName(); |
| 271 | PassName = "LoopPass Printer: " + PassToPrintName; |
| 272 | } |
| 273 | |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 274 | |
| 275 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 276 | if (!Quiet) |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 277 | Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 278 | |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 279 | // Get and print pass... |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 280 | getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, |
| 281 | L->getHeader()->getParent()->getParent()); |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 282 | return false; |
| 283 | } |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 284 | |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 285 | virtual const char *getPassName() const { return PassName.c_str(); } |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 286 | |
| 287 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 288 | AU.addRequiredID(PassToPrint->getTypeInfo()); |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 289 | AU.setPreservesAll(); |
| 290 | } |
| 291 | }; |
| 292 | |
| 293 | char LoopPassPrinter::ID = 0; |
| 294 | |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 295 | struct RegionPassPrinter : public RegionPass { |
| 296 | static char ID; |
| 297 | const PassInfo *PassToPrint; |
| 298 | raw_ostream &Out; |
| 299 | std::string PassName; |
| 300 | |
| 301 | RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID), |
| 302 | PassToPrint(PI), Out(out) { |
| 303 | std::string PassToPrintName = PassToPrint->getPassName(); |
Tobias Grosser | 5839614 | 2011-01-20 21:03:20 +0000 | [diff] [blame] | 304 | PassName = "RegionPass Printer: " + PassToPrintName; |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | virtual bool runOnRegion(Region *R, RGPassManager &RGM) { |
| 308 | if (!Quiet) { |
| 309 | Out << "Printing analysis '" << PassToPrint->getPassName() << "' for " |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 310 | << "region: '" << R->getNameStr() << "' in function '" |
| 311 | << R->getEntry()->getParent()->getName() << "':\n"; |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 312 | } |
| 313 | // Get and print pass... |
| 314 | getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, |
| 315 | R->getEntry()->getParent()->getParent()); |
| 316 | return false; |
| 317 | } |
| 318 | |
Tobias Grosser | 5839614 | 2011-01-20 21:03:20 +0000 | [diff] [blame] | 319 | virtual const char *getPassName() const { return PassName.c_str(); } |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 320 | |
| 321 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 322 | AU.addRequiredID(PassToPrint->getTypeInfo()); |
| 323 | AU.setPreservesAll(); |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | char RegionPassPrinter::ID = 0; |
| 328 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 329 | struct BasicBlockPassPrinter : public BasicBlockPass { |
| 330 | const PassInfo *PassToPrint; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 331 | raw_ostream &Out; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 332 | static char ID; |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 333 | std::string PassName; |
| 334 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 335 | BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out) |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 336 | : BasicBlockPass(ID), PassToPrint(PI), Out(out) { |
| 337 | std::string PassToPrintName = PassToPrint->getPassName(); |
| 338 | PassName = "BasicBlockPass Printer: " + PassToPrintName; |
| 339 | } |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 340 | |
| 341 | virtual bool runOnBasicBlock(BasicBlock &BB) { |
Tobias Grosser | e9dcd03 | 2010-09-08 15:02:51 +0000 | [diff] [blame] | 342 | if (!Quiet) |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 343 | Out << "Printing Analysis info for BasicBlock '" << BB.getName() |
| 344 | << "': Pass " << PassToPrint->getPassName() << ":\n"; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 345 | |
| 346 | // Get and print pass... |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 347 | getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 348 | BB.getParent()->getParent()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Tobias Grosser | 4207d6f | 2010-09-08 15:02:47 +0000 | [diff] [blame] | 352 | virtual const char *getPassName() const { return PassName.c_str(); } |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 353 | |
| 354 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 355 | AU.addRequiredID(PassToPrint->getTypeInfo()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 356 | AU.setPreservesAll(); |
| 357 | } |
| 358 | }; |
| 359 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 360 | char BasicBlockPassPrinter::ID = 0; |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 361 | |
Devang Patel | 1be9980 | 2011-04-04 19:51:17 +0000 | [diff] [blame] | 362 | struct BreakpointPrinter : public ModulePass { |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 363 | raw_ostream &Out; |
| 364 | static char ID; |
| 365 | |
| 366 | BreakpointPrinter(raw_ostream &out) |
Devang Patel | 1be9980 | 2011-04-04 19:51:17 +0000 | [diff] [blame] | 367 | : ModulePass(ID), Out(out) { |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Devang Patel | 1be9980 | 2011-04-04 19:51:17 +0000 | [diff] [blame] | 370 | void getContextName(DIDescriptor Context, std::string &N) { |
| 371 | if (Context.isNameSpace()) { |
| 372 | DINameSpace NS(Context); |
| 373 | if (!NS.getName().empty()) { |
| 374 | getContextName(NS.getContext(), N); |
| 375 | N = N + NS.getName().str() + "::"; |
Devang Patel | 97f6d5b | 2011-01-31 21:36:24 +0000 | [diff] [blame] | 376 | } |
Devang Patel | 1be9980 | 2011-04-04 19:51:17 +0000 | [diff] [blame] | 377 | } else if (Context.isType()) { |
| 378 | DIType TY(Context); |
| 379 | if (!TY.getName().empty()) { |
| 380 | getContextName(TY.getContext(), N); |
| 381 | N = N + TY.getName().str() + "::"; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | virtual bool runOnModule(Module &M) { |
| 387 | StringSet<> Processed; |
| 388 | if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) |
| 389 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
| 390 | std::string Name; |
| 391 | DISubprogram SP(NMD->getOperand(i)); |
| 392 | if (SP.Verify()) |
| 393 | getContextName(SP.getContext(), Name); |
| 394 | Name = Name + SP.getDisplayName().str(); |
| 395 | if (!Name.empty() && Processed.insert(Name)) { |
| 396 | Out << Name << "\n"; |
| 397 | } |
| 398 | } |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
| 402 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 403 | AU.setPreservesAll(); |
| 404 | } |
| 405 | }; |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 406 | |
| 407 | } // anonymous namespace |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 408 | |
| 409 | char BreakpointPrinter::ID = 0; |
| 410 | |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 411 | static inline void addPass(PassManagerBase &PM, Pass *P) { |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 412 | // Add the pass to the pass manager... |
| 413 | PM.add(P); |
| 414 | |
| 415 | // If we are verifying all of the intermediate steps, add the verifier... |
| 416 | if (VerifyEach) PM.add(createVerifierPass()); |
| 417 | } |
| 418 | |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 419 | /// AddOptimizationPasses - This routine adds optimization passes |
| 420 | /// based on selected optimization level, OptLevel. This routine |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 421 | /// duplicates llvm-gcc behaviour. |
| 422 | /// |
| 423 | /// OptLevel - Optimization Level |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 424 | static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM, |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 425 | unsigned OptLevel, unsigned SizeLevel) { |
Duncan Sands | a3e585d | 2011-12-07 17:14:20 +0000 | [diff] [blame] | 426 | FPM.add(createVerifierPass()); // Verify that input is correct |
| 427 | |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 428 | PassManagerBuilder Builder; |
| 429 | Builder.OptLevel = OptLevel; |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 430 | Builder.SizeLevel = SizeLevel; |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 431 | |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 432 | if (DisableInline) { |
| 433 | // No inlining pass |
Eli Friedman | 42d41fd | 2011-06-06 22:13:27 +0000 | [diff] [blame] | 434 | } else if (OptLevel > 1) { |
Jakob Stoklund Olesen | e6c6cec | 2010-11-02 23:40:28 +0000 | [diff] [blame] | 435 | unsigned Threshold = 225; |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 436 | if (SizeLevel == 1) // -Os |
| 437 | Threshold = 75; |
| 438 | else if (SizeLevel == 2) // -Oz |
| 439 | Threshold = 25; |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 440 | if (OptLevel > 2) |
Jakob Stoklund Olesen | e6c6cec | 2010-11-02 23:40:28 +0000 | [diff] [blame] | 441 | Threshold = 275; |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 442 | Builder.Inliner = createFunctionInliningPass(Threshold); |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 443 | } else { |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 444 | Builder.Inliner = createAlwaysInlinerPass(); |
Eli Friedman | 74733a7 | 2010-01-18 22:38:31 +0000 | [diff] [blame] | 445 | } |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 446 | Builder.DisableUnitAtATime = !UnitAtATime; |
| 447 | Builder.DisableUnrollLoops = OptLevel == 0; |
| 448 | Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls; |
| 449 | |
| 450 | Builder.populateFunctionPassManager(FPM); |
| 451 | Builder.populateModulePassManager(MPM); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 454 | static void AddStandardCompilePasses(PassManagerBase &PM) { |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 455 | PM.add(createVerifierPass()); // Verify that input is correct |
| 456 | |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 457 | // If the -strip-debug command line option was specified, do it. |
| 458 | if (StripDebug) |
| 459 | addPass(PM, createStripSymbolsPass(true)); |
| 460 | |
| 461 | if (DisableOptimizations) return; |
| 462 | |
Daniel Dunbar | ca8131e | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 463 | // -std-compile-opts adds the same module passes as -O3. |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 464 | PassManagerBuilder Builder; |
| 465 | if (!DisableInline) |
| 466 | Builder.Inliner = createFunctionInliningPass(); |
| 467 | Builder.OptLevel = 3; |
| 468 | Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls; |
| 469 | Builder.populateModulePassManager(PM); |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 472 | static void AddStandardLinkPasses(PassManagerBase &PM) { |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 473 | PM.add(createVerifierPass()); // Verify that input is correct |
| 474 | |
| 475 | // If the -strip-debug command line option was specified, do it. |
| 476 | if (StripDebug) |
| 477 | addPass(PM, createStripSymbolsPass(true)); |
| 478 | |
| 479 | if (DisableOptimizations) return; |
| 480 | |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 481 | PassManagerBuilder Builder; |
| 482 | Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize, |
| 483 | /*RunInliner=*/ !DisableInline); |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 486 | //===----------------------------------------------------------------------===// |
| 487 | // CodeGen-related helper functions. |
| 488 | // |
| 489 | static TargetOptions GetTargetOptions() { |
| 490 | TargetOptions Options; |
| 491 | Options.LessPreciseFPMADOption = EnableFPMAD; |
| 492 | Options.NoFramePointerElim = DisableFPElim; |
| 493 | Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf; |
| 494 | Options.AllowFPOpFusion = FuseFPOps; |
| 495 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 496 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 497 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 498 | Options.HonorSignDependentRoundingFPMathOption = |
| 499 | EnableHonorSignDependentRoundingFPMath; |
| 500 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 501 | if (FloatABIForCalls != FloatABI::Default) |
| 502 | Options.FloatABIType = FloatABIForCalls; |
| 503 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 504 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 505 | Options.DisableTailCalls = DisableTailCalls; |
| 506 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 507 | Options.RealignStack = EnableRealignStack; |
| 508 | Options.TrapFuncName = TrapFuncName; |
| 509 | Options.PositionIndependentExecutable = EnablePIE; |
| 510 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 511 | Options.UseInitArray = UseInitArray; |
| 512 | Options.SSPBufferSize = SSPBufferSize; |
| 513 | return Options; |
| 514 | } |
| 515 | |
| 516 | CodeGenOpt::Level GetCodeGenOptLevel() { |
| 517 | if (OptLevelO1) |
| 518 | return CodeGenOpt::Less; |
| 519 | if (OptLevelO2) |
| 520 | return CodeGenOpt::Default; |
| 521 | if (OptLevelO3) |
| 522 | return CodeGenOpt::Aggressive; |
| 523 | return CodeGenOpt::None; |
| 524 | } |
| 525 | |
| 526 | // Returns the TargetMachine instance or zero if no triple is provided. |
Nadav Rotem | b338d89 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 527 | static TargetMachine* GetTargetMachine(Triple TheTriple) { |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 528 | std::string Error; |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 529 | const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, |
| 530 | Error); |
Nadav Rotem | b338d89 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 531 | // Some modules don't specify a triple, and this is okay. |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 532 | if (!TheTarget) { |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | // Package up features to be passed to target/subtarget |
| 537 | std::string FeaturesStr; |
| 538 | if (MAttrs.size()) { |
| 539 | SubtargetFeatures Features; |
| 540 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 541 | Features.AddFeature(MAttrs[i]); |
| 542 | FeaturesStr = Features.getString(); |
| 543 | } |
| 544 | |
| 545 | return TheTarget->createTargetMachine(TheTriple.getTriple(), |
| 546 | MCPU, FeaturesStr, GetTargetOptions(), |
| 547 | RelocModel, CMModel, |
| 548 | GetCodeGenOptLevel()); |
| 549 | } |
Chris Lattner | 0be4101 | 2002-02-01 04:54:11 +0000 | [diff] [blame] | 550 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 551 | //===----------------------------------------------------------------------===// |
| 552 | // main for opt |
| 553 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 554 | int main(int argc, char **argv) { |
Chris Lattner | c0d91b7 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 555 | sys::PrintStackTraceOnErrorSignal(); |
| 556 | llvm::PrettyStackTraceProgram X(argc, argv); |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 557 | |
David Greene | 08fc0d3 | 2010-01-05 01:30:32 +0000 | [diff] [blame] | 558 | // Enable debug stream buffering. |
| 559 | EnableDebugBuffering = true; |
| 560 | |
Chris Lattner | c0d91b7 | 2009-12-09 00:41:28 +0000 | [diff] [blame] | 561 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 562 | LLVMContext &Context = getGlobalContext(); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 563 | |
Nadav Rotem | 0873bea | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 564 | InitializeAllTargets(); |
| 565 | InitializeAllTargetMCs(); |
Nadav Rotem | 0873bea | 2012-10-24 17:23:50 +0000 | [diff] [blame] | 566 | |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 567 | // Initialize passes |
| 568 | PassRegistry &Registry = *PassRegistry::getPassRegistry(); |
| 569 | initializeCore(Registry); |
| 570 | initializeScalarOpts(Registry); |
Michael Gottesman | 24c4898 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 571 | initializeObjCARCOpts(Registry); |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 572 | initializeVectorization(Registry); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 573 | initializeIPO(Registry); |
| 574 | initializeAnalysis(Registry); |
| 575 | initializeIPA(Registry); |
| 576 | initializeTransformUtils(Registry); |
| 577 | initializeInstCombine(Registry); |
| 578 | initializeInstrumentation(Registry); |
| 579 | initializeTarget(Registry); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 581 | cl::ParseCommandLineOptions(argc, argv, |
| 582 | "llvm .bc -> .bc modular optimizer and analysis printer\n"); |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 583 | |
Tobias Grosser | 7593f34 | 2010-12-02 20:35:16 +0000 | [diff] [blame] | 584 | if (AnalyzeOnly && NoOutput) { |
| 585 | errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; |
| 586 | return 1; |
| 587 | } |
| 588 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 589 | SMDiagnostic Err; |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 591 | // Load the input module... |
| 592 | std::auto_ptr<Module> M; |
| 593 | M.reset(ParseIRFile(InputFilename, Err, Context)); |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 595 | if (M.get() == 0) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 596 | Err.print(argv[0], errs()); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 597 | return 1; |
| 598 | } |
| 599 | |
Joe Groff | e652b52 | 2012-04-17 23:05:48 +0000 | [diff] [blame] | 600 | // If we are supposed to override the target triple, do so now. |
| 601 | if (!TargetTriple.empty()) |
| 602 | M->setTargetTriple(Triple::normalize(TargetTriple)); |
| 603 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 604 | // Figure out what stream we are supposed to write to... |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 605 | OwningPtr<tool_output_file> Out; |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 606 | if (NoOutput) { |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 607 | if (!OutputFilename.empty()) |
| 608 | errs() << "WARNING: The -o (output filename) option is ignored when\n" |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 609 | "the --disable-output option is used.\n"; |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 610 | } else { |
| 611 | // Default to standard output. |
| 612 | if (OutputFilename.empty()) |
| 613 | OutputFilename = "-"; |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 614 | |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 615 | std::string ErrorInfo; |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 616 | Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
| 617 | raw_fd_ostream::F_Binary)); |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 618 | if (!ErrorInfo.empty()) { |
| 619 | errs() << ErrorInfo << '\n'; |
Dan Gohman | 86cbc1b | 2010-08-18 17:40:10 +0000 | [diff] [blame] | 620 | return 1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 621 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 622 | } |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 624 | // If the output is set to be emitted to standard out, and standard out is a |
| 625 | // console, print out a warning message and refuse to do it. We don't |
| 626 | // impress anyone by spewing tons of binary goo to a terminal. |
Dan Gohman | b56bf58 | 2010-01-17 17:47:24 +0000 | [diff] [blame] | 627 | if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 628 | if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 629 | NoOutput = true; |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 631 | // Create a PassManager to hold and optimize the collection of passes we are |
Chris Lattner | 2a66aca | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 632 | // about to build. |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 633 | // |
| 634 | PassManager Passes; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 2a66aca | 2011-02-18 22:13:01 +0000 | [diff] [blame] | 636 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
Chris Lattner | 188a7e0 | 2011-02-18 22:34:03 +0000 | [diff] [blame] | 637 | TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple())); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 188a7e0 | 2011-02-18 22:34:03 +0000 | [diff] [blame] | 639 | // The -disable-simplify-libcalls flag actually disables all builtin optzns. |
| 640 | if (DisableSimplifyLibCalls) |
| 641 | TLI->disableAllFunctions(); |
| 642 | Passes.add(TLI); |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 643 | |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 644 | // Add an appropriate DataLayout instance for this module. |
| 645 | DataLayout *TD = 0; |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 646 | const std::string &ModuleDataLayout = M.get()->getDataLayout(); |
| 647 | if (!ModuleDataLayout.empty()) |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 648 | TD = new DataLayout(ModuleDataLayout); |
Kenneth Uildriks | b908f8a | 2009-11-03 15:29:06 +0000 | [diff] [blame] | 649 | else if (!DefaultDataLayout.empty()) |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 650 | TD = new DataLayout(DefaultDataLayout); |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 652 | if (TD) |
| 653 | Passes.add(TD); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 654 | |
Nadav Rotem | b338d89 | 2013-01-01 08:00:32 +0000 | [diff] [blame] | 655 | Triple ModuleTriple(M->getTargetTriple()); |
| 656 | TargetMachine *Machine = 0; |
| 657 | if (ModuleTriple.getArch()) |
| 658 | Machine = GetTargetMachine(Triple(ModuleTriple)); |
| 659 | std::auto_ptr<TargetMachine> TM(Machine); |
| 660 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 661 | // Add internal analysis passes from the target machine. |
| 662 | if (TM.get()) |
| 663 | TM->addAnalysisPasses(Passes); |
Nadav Rotem | cbd9a19 | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 665 | OwningPtr<FunctionPassManager> FPasses; |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 666 | if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 667 | FPasses.reset(new FunctionPassManager(M.get())); |
Chris Lattner | d331cb3 | 2009-10-22 00:44:10 +0000 | [diff] [blame] | 668 | if (TD) |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 669 | FPasses->add(new DataLayout(*TD)); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 670 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 671 | |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 672 | if (PrintBreakpoints) { |
| 673 | // Default to standard output. |
| 674 | if (!Out) { |
| 675 | if (OutputFilename.empty()) |
| 676 | OutputFilename = "-"; |
Andrew Trick | a41af7a | 2011-04-05 18:41:31 +0000 | [diff] [blame] | 677 | |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 678 | std::string ErrorInfo; |
| 679 | Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
| 680 | raw_fd_ostream::F_Binary)); |
| 681 | if (!ErrorInfo.empty()) { |
| 682 | errs() << ErrorInfo << '\n'; |
| 683 | return 1; |
| 684 | } |
| 685 | } |
| 686 | Passes.add(new BreakpointPrinter(Out->os())); |
| 687 | NoOutput = true; |
| 688 | } |
| 689 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 690 | // If the -strip-debug command line option was specified, add it. If |
| 691 | // -std-compile-opts was also specified, it will handle StripDebug. |
| 692 | if (StripDebug && !StandardCompileOpts) |
| 693 | addPass(Passes, createStripSymbolsPass(true)); |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 694 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 695 | // Create a new optimization pass for each one specified on the command line |
| 696 | for (unsigned i = 0; i < PassList.size(); ++i) { |
| 697 | // Check to see if -std-compile-opts was specified before this option. If |
| 698 | // so, handle it. |
| 699 | if (StandardCompileOpts && |
| 700 | StandardCompileOpts.getPosition() < PassList.getPosition(i)) { |
Chris Lattner | 3dda08a | 2008-07-13 19:35:21 +0000 | [diff] [blame] | 701 | AddStandardCompilePasses(Passes); |
| 702 | StandardCompileOpts = false; |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 703 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 705 | if (StandardLinkOpts && |
| 706 | StandardLinkOpts.getPosition() < PassList.getPosition(i)) { |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 707 | AddStandardLinkPasses(Passes); |
| 708 | StandardLinkOpts = false; |
Eric Christopher | a887ae4 | 2009-08-21 23:29:40 +0000 | [diff] [blame] | 709 | } |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 711 | if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 712 | AddOptimizationPasses(Passes, *FPasses, 1, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 713 | OptLevelO1 = false; |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 714 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 716 | if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 717 | AddOptimizationPasses(Passes, *FPasses, 2, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 718 | OptLevelO2 = false; |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 719 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 720 | |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 721 | if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { |
| 722 | AddOptimizationPasses(Passes, *FPasses, 2, 1); |
| 723 | OptLevelOs = false; |
| 724 | } |
| 725 | |
| 726 | if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { |
| 727 | AddOptimizationPasses(Passes, *FPasses, 2, 2); |
| 728 | OptLevelOz = false; |
| 729 | } |
| 730 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 731 | if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 732 | AddOptimizationPasses(Passes, *FPasses, 3, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 733 | OptLevelO3 = false; |
Daniel Dunbar | adc8288 | 2009-07-17 18:09:39 +0000 | [diff] [blame] | 734 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 736 | const PassInfo *PassInf = PassList[i]; |
| 737 | Pass *P = 0; |
| 738 | if (PassInf->getNormalCtor()) |
| 739 | P = PassInf->getNormalCtor()(); |
| 740 | else |
| 741 | errs() << argv[0] << ": cannot create pass: " |
| 742 | << PassInf->getPassName() << "\n"; |
| 743 | if (P) { |
Benjamin Kramer | 3460f22 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 744 | PassKind Kind = P->getPassKind(); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 745 | addPass(Passes, P); |
| 746 | |
| 747 | if (AnalyzeOnly) { |
Benjamin Kramer | 3460f22 | 2010-02-18 12:57:05 +0000 | [diff] [blame] | 748 | switch (Kind) { |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 749 | case PT_BasicBlock: |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 750 | Passes.add(new BasicBlockPassPrinter(PassInf, Out->os())); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 751 | break; |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 752 | case PT_Region: |
| 753 | Passes.add(new RegionPassPrinter(PassInf, Out->os())); |
| 754 | break; |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 755 | case PT_Loop: |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 756 | Passes.add(new LoopPassPrinter(PassInf, Out->os())); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 757 | break; |
| 758 | case PT_Function: |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 759 | Passes.add(new FunctionPassPrinter(PassInf, Out->os())); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 760 | break; |
| 761 | case PT_CallGraphSCC: |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 762 | Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os())); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 763 | break; |
| 764 | default: |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 765 | Passes.add(new ModulePassPrinter(PassInf, Out->os())); |
Chris Lattner | 476e9bd | 2010-01-22 06:03:06 +0000 | [diff] [blame] | 766 | break; |
| 767 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 768 | } |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 771 | if (PrintEachXForm) |
| 772 | Passes.add(createPrintModulePass(&errs())); |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 773 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 774 | |
| 775 | // If -std-compile-opts was specified at the end of the pass list, add them. |
| 776 | if (StandardCompileOpts) { |
| 777 | AddStandardCompilePasses(Passes); |
| 778 | StandardCompileOpts = false; |
| 779 | } |
| 780 | |
| 781 | if (StandardLinkOpts) { |
| 782 | AddStandardLinkPasses(Passes); |
| 783 | StandardLinkOpts = false; |
| 784 | } |
| 785 | |
| 786 | if (OptLevelO1) |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 787 | AddOptimizationPasses(Passes, *FPasses, 1, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 788 | |
| 789 | if (OptLevelO2) |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 790 | AddOptimizationPasses(Passes, *FPasses, 2, 0); |
| 791 | |
| 792 | if (OptLevelOs) |
| 793 | AddOptimizationPasses(Passes, *FPasses, 2, 1); |
| 794 | |
| 795 | if (OptLevelOz) |
| 796 | AddOptimizationPasses(Passes, *FPasses, 2, 2); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 797 | |
| 798 | if (OptLevelO3) |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 799 | AddOptimizationPasses(Passes, *FPasses, 3, 0); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 800 | |
Chandler Carruth | 8bc3434 | 2012-05-16 08:32:49 +0000 | [diff] [blame] | 801 | if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { |
Chris Lattner | 3e8984a | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 802 | FPasses->doInitialization(); |
Chris Lattner | 52b2889 | 2011-05-22 00:21:33 +0000 | [diff] [blame] | 803 | for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F) |
| 804 | FPasses->run(*F); |
Chris Lattner | 3e8984a | 2011-05-22 06:44:19 +0000 | [diff] [blame] | 805 | FPasses->doFinalization(); |
| 806 | } |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 807 | |
| 808 | // Check that the module is well formed on completion of optimization |
| 809 | if (!NoVerify && !VerifyEach) |
| 810 | Passes.add(createVerifierPass()); |
| 811 | |
Dan Gohman | 4931b31 | 2010-08-18 17:42:59 +0000 | [diff] [blame] | 812 | // Write bitcode or assembly to the output as the last step... |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 813 | if (!NoOutput && !AnalyzeOnly) { |
| 814 | if (OutputAssembly) |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 815 | Passes.add(createPrintModulePass(&Out->os())); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 816 | else |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 817 | Passes.add(createBitcodeWriterPass(Out->os())); |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Andrew Trick | ce96902 | 2011-04-05 18:54:36 +0000 | [diff] [blame] | 820 | // Before executing passes, print the final values of the LLVM options. |
| 821 | cl::PrintOptionValues(); |
| 822 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 823 | // Now that we have all of the passes ready, run them. |
| 824 | Passes.run(*M.get()); |
| 825 | |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 826 | // Declare success. |
Devang Patel | f5f2300 | 2010-12-07 00:33:43 +0000 | [diff] [blame] | 827 | if (!NoOutput || PrintBreakpoints) |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 828 | Out->keep(); |
| 829 | |
Chris Lattner | 61db1a1 | 2009-10-22 00:46:41 +0000 | [diff] [blame] | 830 | return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 831 | } |