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 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 15 | #include "llvm/LLVMContext.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 17 | #include "llvm/ModuleProvider.h" |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 18 | #include "llvm/PassManager.h" |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 19 | #include "llvm/CallGraphSCCPass.h" |
Chris Lattner | b330e38 | 2007-05-06 02:42:03 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | ffa6f9c | 2001-10-19 15:39:14 +0000 | [diff] [blame] | 21 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 22d26d7 | 2002-02-20 17:56:53 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Verifier.h" |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopPass.h" |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CallGraph.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 2053a2a | 2002-07-26 21:09:32 +0000 | [diff] [blame] | 27 | #include "llvm/Support/PassNameParser.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 28 | #include "llvm/System/Signals.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | b330e38 | 2007-05-06 02:42:03 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/PluginLoader.h" |
Daniel Dunbar | ca8131e | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 32 | #include "llvm/Support/StandardPasses.h" |
Bill Wendling | a5b31ca | 2006-11-28 23:33:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Streams.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/SystemUtils.h" |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 62c5105 | 2006-08-21 05:34:03 +0000 | [diff] [blame] | 36 | #include "llvm/LinkAllPasses.h" |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 37 | #include "llvm/LinkAllVMCore.h" |
Chris Lattner | 6320232 | 2001-11-26 19:22:39 +0000 | [diff] [blame] | 38 | #include <memory> |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Chris Lattner | 9d6e7eb | 2002-04-12 18:21:13 +0000 | [diff] [blame] | 41 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 42 | // The OptimizationList is automatically populated with registered Passes by the |
| 43 | // PassNameParser. |
| 44 | // |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 45 | static cl::list<const PassInfo*, bool, PassNameParser> |
| 46 | PassList(cl::desc("Optimizations available:")); |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 47 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 48 | // Other command line options... |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 49 | // |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 50 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 51 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 52 | cl::init("-"), cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 54 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | OutputFilename("o", cl::desc("Override output filename"), |
Chris Lattner | b592fc2 | 2003-12-10 14:41:33 +0000 | [diff] [blame] | 56 | cl::value_desc("filename"), cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 57 | |
| 58 | static cl::opt<bool> |
| 59 | Force("f", cl::desc("Overwrite output files")); |
| 60 | |
| 61 | static cl::opt<bool> |
| 62 | PrintEachXForm("p", cl::desc("Print module after each transformation")); |
| 63 | |
| 64 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 65 | NoOutput("disable-output", |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 66 | cl::desc("Do not write result bitcode file"), cl::Hidden); |
Chris Lattner | d70b68e | 2003-02-12 18:43:33 +0000 | [diff] [blame] | 67 | |
| 68 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 69 | NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden); |
Chris Lattner | f3bafc1 | 2003-02-12 18:45:08 +0000 | [diff] [blame] | 70 | |
| 71 | static cl::opt<bool> |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 72 | VerifyEach("verify-each", cl::desc("Verify after each transform")); |
| 73 | |
| 74 | static cl::opt<bool> |
| 75 | StripDebug("strip-debug", |
| 76 | cl::desc("Strip debugger symbol info from translation unit")); |
| 77 | |
| 78 | static cl::opt<bool> |
| 79 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| 80 | |
| 81 | static cl::opt<bool> |
| 82 | DisableOptimizations("disable-opt", |
| 83 | cl::desc("Do not run any optimization passes")); |
| 84 | |
| 85 | static cl::opt<bool> |
| 86 | StandardCompileOpts("std-compile-opts", |
| 87 | cl::desc("Include the standard compile time optimizations")); |
| 88 | |
| 89 | static cl::opt<bool> |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 90 | OptLevelO1("O1", |
| 91 | cl::desc("Optimization level 1. Similar to llvm-gcc -O1")); |
| 92 | |
| 93 | static cl::opt<bool> |
| 94 | OptLevelO2("O2", |
Devang Patel | d9424ed | 2008-09-17 00:01:04 +0000 | [diff] [blame] | 95 | cl::desc("Optimization level 2. Similar to llvm-gcc -O2")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 96 | |
| 97 | static cl::opt<bool> |
| 98 | OptLevelO3("O3", |
Devang Patel | d9424ed | 2008-09-17 00:01:04 +0000 | [diff] [blame] | 99 | cl::desc("Optimization level 3. Similar to llvm-gcc -O3")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 100 | |
| 101 | static cl::opt<bool> |
| 102 | UnitAtATime("funit-at-a-time", |
Devang Patel | 442b1aa | 2008-09-17 16:01:39 +0000 | [diff] [blame] | 103 | cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 104 | |
| 105 | static cl::opt<bool> |
| 106 | DisableSimplifyLibCalls("disable-simplify-libcalls", |
Devang Patel | 442b1aa | 2008-09-17 16:01:39 +0000 | [diff] [blame] | 107 | cl::desc("Disable simplify-libcalls")); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 108 | |
| 109 | static cl::opt<bool> |
Chris Lattner | 3153e4f | 2004-05-27 20:32:10 +0000 | [diff] [blame] | 110 | Quiet("q", cl::desc("Obsolete option"), cl::Hidden); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 111 | |
Reid Spencer | ec7eb45 | 2004-05-27 16:28:54 +0000 | [diff] [blame] | 112 | static cl::alias |
| 113 | QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); |
| 114 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 115 | static cl::opt<bool> |
| 116 | AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); |
| 117 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 118 | // ---------- Define Printers for module and function passes ------------ |
| 119 | namespace { |
| 120 | |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 121 | struct CallGraphSCCPassPrinter : public CallGraphSCCPass { |
| 122 | static char ID; |
| 123 | const PassInfo *PassToPrint; |
| 124 | CallGraphSCCPassPrinter(const PassInfo *PI) : |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 125 | CallGraphSCCPass(&ID), PassToPrint(PI) {} |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 126 | |
| 127 | virtual bool runOnSCC(const std::vector<CallGraphNode *>&SCC) { |
| 128 | if (!Quiet) { |
| 129 | cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
| 130 | |
| 131 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 132 | Function *F = SCC[i]->getFunction(); |
| 133 | if (F) |
| 134 | getAnalysisID<Pass>(PassToPrint).print(cout, F->getParent()); |
| 135 | } |
| 136 | } |
| 137 | // Get and print pass... |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | virtual const char *getPassName() const { return "'Pass' Printer"; } |
| 142 | |
| 143 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 144 | AU.addRequiredID(PassToPrint); |
| 145 | AU.setPreservesAll(); |
| 146 | } |
| 147 | }; |
| 148 | |
| 149 | char CallGraphSCCPassPrinter::ID = 0; |
| 150 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 151 | struct ModulePassPrinter : public ModulePass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 152 | static char ID; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 153 | const PassInfo *PassToPrint; |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 154 | ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID), |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 155 | PassToPrint(PI) {} |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 156 | |
| 157 | virtual bool runOnModule(Module &M) { |
| 158 | if (!Quiet) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 159 | cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
| 160 | getAnalysisID<Pass>(PassToPrint).print(cout, &M); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Get and print pass... |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | virtual const char *getPassName() const { return "'Pass' Printer"; } |
| 168 | |
| 169 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 170 | AU.addRequiredID(PassToPrint); |
| 171 | AU.setPreservesAll(); |
| 172 | } |
| 173 | }; |
| 174 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 175 | char ModulePassPrinter::ID = 0; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 176 | struct FunctionPassPrinter : public FunctionPass { |
| 177 | const PassInfo *PassToPrint; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 178 | static char ID; |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 179 | FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID), |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 180 | PassToPrint(PI) {} |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 181 | |
| 182 | virtual bool runOnFunction(Function &F) { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 183 | if (!Quiet) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 184 | cout << "Printing analysis '" << PassToPrint->getPassName() |
| 185 | << "' for function '" << F.getName() << "':\n"; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 186 | } |
| 187 | // Get and print pass... |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 188 | getAnalysisID<Pass>(PassToPrint).print(cout, F.getParent()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
| 192 | virtual const char *getPassName() const { return "FunctionPass Printer"; } |
| 193 | |
| 194 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 195 | AU.addRequiredID(PassToPrint); |
| 196 | AU.setPreservesAll(); |
| 197 | } |
| 198 | }; |
| 199 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 200 | char FunctionPassPrinter::ID = 0; |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 201 | |
| 202 | struct LoopPassPrinter : public LoopPass { |
| 203 | static char ID; |
| 204 | const PassInfo *PassToPrint; |
| 205 | LoopPassPrinter(const PassInfo *PI) : |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 206 | LoopPass(&ID), PassToPrint(PI) {} |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 207 | |
| 208 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { |
| 209 | if (!Quiet) { |
| 210 | cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
| 211 | getAnalysisID<Pass>(PassToPrint).print(cout, |
| 212 | L->getHeader()->getParent()->getParent()); |
| 213 | } |
| 214 | // Get and print pass... |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | virtual const char *getPassName() const { return "'Pass' Printer"; } |
| 219 | |
| 220 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 221 | AU.addRequiredID(PassToPrint); |
| 222 | AU.setPreservesAll(); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | char LoopPassPrinter::ID = 0; |
| 227 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 228 | struct BasicBlockPassPrinter : public BasicBlockPass { |
| 229 | const PassInfo *PassToPrint; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 230 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 231 | BasicBlockPassPrinter(const PassInfo *PI) |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 232 | : BasicBlockPass(&ID), PassToPrint(PI) {} |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 233 | |
| 234 | virtual bool runOnBasicBlock(BasicBlock &BB) { |
| 235 | if (!Quiet) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 236 | cout << "Printing Analysis info for BasicBlock '" << BB.getName() |
| 237 | << "': Pass " << PassToPrint->getPassName() << ":\n"; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Get and print pass... |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 241 | getAnalysisID<Pass>(PassToPrint).print(cout, BB.getParent()->getParent()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
| 245 | virtual const char *getPassName() const { return "BasicBlockPass Printer"; } |
| 246 | |
| 247 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 248 | AU.addRequiredID(PassToPrint); |
| 249 | AU.setPreservesAll(); |
| 250 | } |
| 251 | }; |
| 252 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 253 | char BasicBlockPassPrinter::ID = 0; |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 254 | inline void addPass(PassManager &PM, Pass *P) { |
| 255 | // Add the pass to the pass manager... |
| 256 | PM.add(P); |
| 257 | |
| 258 | // If we are verifying all of the intermediate steps, add the verifier... |
| 259 | if (VerifyEach) PM.add(createVerifierPass()); |
| 260 | } |
| 261 | |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 262 | /// AddOptimizationPasses - This routine adds optimization passes |
| 263 | /// based on selected optimization level, OptLevel. This routine |
| 264 | /// duplicates llvm-gcc behaviour. |
| 265 | /// |
| 266 | /// OptLevel - Optimization Level |
Zhongxing Xu | 3253f4c | 2008-11-26 02:57:24 +0000 | [diff] [blame] | 267 | void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM, |
| 268 | unsigned OptLevel) { |
Daniel Dunbar | ca8131e | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 269 | createStandardFunctionPasses(&FPM, OptLevel); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 270 | |
Daniel Dunbar | ca8131e | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 271 | llvm::Pass *InliningPass = OptLevel > 1 ? createFunctionInliningPass() : 0; |
| 272 | createStandardModulePasses(&MPM, OptLevel, |
| 273 | /*OptimizeSize=*/ false, |
| 274 | UnitAtATime, |
| 275 | /*UnrollLoops=*/ OptLevel > 1, |
| 276 | !DisableSimplifyLibCalls, |
| 277 | /*HaveExceptions=*/ true, |
| 278 | InliningPass); |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 281 | void AddStandardCompilePasses(PassManager &PM) { |
| 282 | PM.add(createVerifierPass()); // Verify that input is correct |
| 283 | |
| 284 | addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 285 | |
| 286 | // If the -strip-debug command line option was specified, do it. |
| 287 | if (StripDebug) |
| 288 | addPass(PM, createStripSymbolsPass(true)); |
| 289 | |
| 290 | if (DisableOptimizations) return; |
| 291 | |
Daniel Dunbar | ca8131e | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 292 | llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0; |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 293 | |
Daniel Dunbar | ca8131e | 2009-06-03 18:22:15 +0000 | [diff] [blame] | 294 | // -std-compile-opts adds the same module passes as -O3. |
| 295 | createStandardModulePasses(&PM, 3, |
| 296 | /*OptimizeSize=*/ false, |
| 297 | /*UnitAtATime=*/ true, |
| 298 | /*UnrollLoops=*/ true, |
| 299 | /*SimplifyLibCalls=*/ true, |
| 300 | /*HaveExceptions=*/ true, |
| 301 | InliningPass); |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 304 | } // anonymous namespace |
| 305 | |
Chris Lattner | 0be4101 | 2002-02-01 04:54:11 +0000 | [diff] [blame] | 306 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
| 308 | // main for opt |
| 309 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 310 | int main(int argc, char **argv) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 311 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame^] | 312 | LLVMContext &Context = getGlobalContext(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 313 | try { |
| 314 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 315 | "llvm .bc -> .bc modular optimizer and analysis printer\n"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 316 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 317 | |
Devang Patel | ff5d06d | 2008-08-27 20:51:49 +0000 | [diff] [blame] | 318 | // Allocate a full target machine description only if necessary. |
| 319 | // FIXME: The choice of target should be controllable on the command line. |
| 320 | std::auto_ptr<TargetMachine> target; |
| 321 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 322 | std::string ErrorMessage; |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 323 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 324 | // Load the input module... |
Chris Lattner | b330e38 | 2007-05-06 02:42:03 +0000 | [diff] [blame] | 325 | std::auto_ptr<Module> M; |
Chris Lattner | 065344d | 2007-05-06 23:45:49 +0000 | [diff] [blame] | 326 | if (MemoryBuffer *Buffer |
| 327 | = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) { |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 328 | M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage)); |
Chris Lattner | 065344d | 2007-05-06 23:45:49 +0000 | [diff] [blame] | 329 | delete Buffer; |
| 330 | } |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 331 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 332 | if (M.get() == 0) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 333 | cerr << argv[0] << ": "; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 334 | if (ErrorMessage.size()) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 335 | cerr << ErrorMessage << "\n"; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 336 | else |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 337 | cerr << "bitcode didn't read correctly.\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 338 | return 1; |
| 339 | } |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 340 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 341 | // Figure out what stream we are supposed to write to... |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 342 | // FIXME: cout is not binary! |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 343 | raw_ostream *Out = &outs(); // Default to printing to stdout... |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 344 | if (OutputFilename != "-") { |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 345 | std::string ErrorInfo; |
| 346 | Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, |
| 347 | Force, ErrorInfo); |
| 348 | if (!ErrorInfo.empty()) { |
| 349 | errs() << ErrorInfo << '\n'; |
| 350 | if (!Force) |
| 351 | errs() << "Use -f command line argument to force output\n"; |
| 352 | delete Out; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 353 | return 1; |
| 354 | } |
Chris Lattner | 76351aa | 2004-04-02 05:06:57 +0000 | [diff] [blame] | 355 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 356 | // Make sure that the Output file gets unlinked from the disk if we get a |
| 357 | // SIGINT |
| 358 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 359 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 360 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 361 | // If the output is set to be emitted to standard out, and standard out is a |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 362 | // console, print out a warning message and refuse to do it. We don't |
| 363 | // impress anyone by spewing tons of binary goo to a terminal. |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 364 | if (!Force && !NoOutput && CheckBitcodeOutputToConsole(Out,!Quiet)) { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 365 | NoOutput = true; |
| 366 | } |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 367 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 368 | // Create a PassManager to hold and optimize the collection of passes we are |
| 369 | // about to build... |
| 370 | // |
| 371 | PassManager Passes; |
| 372 | |
| 373 | // Add an appropriate TargetData instance for this module... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 374 | Passes.add(new TargetData(M.get())); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 375 | |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 376 | FunctionPassManager *FPasses = NULL; |
| 377 | if (OptLevelO1 || OptLevelO2 || OptLevelO3) { |
| 378 | FPasses = new FunctionPassManager(new ExistingModuleProvider(M.get())); |
| 379 | FPasses->add(new TargetData(M.get())); |
| 380 | } |
| 381 | |
Chris Lattner | 3dda08a | 2008-07-13 19:35:21 +0000 | [diff] [blame] | 382 | // If the -strip-debug command line option was specified, add it. If |
| 383 | // -std-compile-opts was also specified, it will handle StripDebug. |
| 384 | if (StripDebug && !StandardCompileOpts) |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 385 | addPass(Passes, createStripSymbolsPass(true)); |
| 386 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 387 | // Create a new optimization pass for each one specified on the command line |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 388 | for (unsigned i = 0; i < PassList.size(); ++i) { |
Duncan Sands | 56eb133 | 2008-07-13 20:14:38 +0000 | [diff] [blame] | 389 | // Check to see if -std-compile-opts was specified before this option. If |
Chris Lattner | 3dda08a | 2008-07-13 19:35:21 +0000 | [diff] [blame] | 390 | // so, handle it. |
| 391 | if (StandardCompileOpts && |
| 392 | StandardCompileOpts.getPosition() < PassList.getPosition(i)) { |
| 393 | AddStandardCompilePasses(Passes); |
| 394 | StandardCompileOpts = false; |
| 395 | } |
| 396 | |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 397 | if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { |
| 398 | AddOptimizationPasses(Passes, *FPasses, 1); |
| 399 | OptLevelO1 = false; |
| 400 | } |
| 401 | |
| 402 | if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { |
| 403 | AddOptimizationPasses(Passes, *FPasses, 2); |
| 404 | OptLevelO2 = false; |
| 405 | } |
| 406 | |
| 407 | if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { |
| 408 | AddOptimizationPasses(Passes, *FPasses, 3); |
| 409 | OptLevelO3 = false; |
| 410 | } |
| 411 | |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 412 | const PassInfo *PassInf = PassList[i]; |
| 413 | Pass *P = 0; |
| 414 | if (PassInf->getNormalCtor()) |
| 415 | P = PassInf->getNormalCtor()(); |
Chris Lattner | cd950a5 | 2006-12-01 21:59:37 +0000 | [diff] [blame] | 416 | else |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 417 | cerr << argv[0] << ": cannot create pass: " |
| 418 | << PassInf->getPassName() << "\n"; |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 419 | if (P) { |
Nuno Lopes | 641397f | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 420 | bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0; |
| 421 | bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0; |
| 422 | bool isFPass = !isLPass && dynamic_cast<FunctionPass*>(P) != 0; |
| 423 | bool isCGSCCPass = !isFPass && dynamic_cast<CallGraphSCCPass*>(P) != 0; |
| 424 | |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 425 | addPass(Passes, P); |
Nuno Lopes | 641397f | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 427 | if (AnalyzeOnly) { |
Nuno Lopes | 641397f | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 428 | if (isBBPass) |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 429 | Passes.add(new BasicBlockPassPrinter(PassInf)); |
Nuno Lopes | 641397f | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 430 | else if (isLPass) |
| 431 | Passes.add(new LoopPassPrinter(PassInf)); |
| 432 | else if (isFPass) |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 433 | Passes.add(new FunctionPassPrinter(PassInf)); |
Nuno Lopes | 641397f | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 434 | else if (isCGSCCPass) |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 435 | Passes.add(new CallGraphSCCPassPrinter(PassInf)); |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 436 | else |
| 437 | Passes.add(new ModulePassPrinter(PassInf)); |
| 438 | } |
| 439 | } |
| 440 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 441 | if (PrintEachXForm) |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 442 | Passes.add(createPrintModulePass(&errs())); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 443 | } |
Chris Lattner | 3dda08a | 2008-07-13 19:35:21 +0000 | [diff] [blame] | 444 | |
| 445 | // If -std-compile-opts was specified at the end of the pass list, add them. |
| 446 | if (StandardCompileOpts) { |
| 447 | AddStandardCompilePasses(Passes); |
| 448 | StandardCompileOpts = false; |
| 449 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 450 | |
Devang Patel | 2d7551c | 2008-09-16 22:25:14 +0000 | [diff] [blame] | 451 | if (OptLevelO1) { |
| 452 | AddOptimizationPasses(Passes, *FPasses, 1); |
| 453 | } |
| 454 | |
| 455 | if (OptLevelO2) { |
| 456 | AddOptimizationPasses(Passes, *FPasses, 2); |
| 457 | } |
| 458 | |
| 459 | if (OptLevelO3) { |
| 460 | AddOptimizationPasses(Passes, *FPasses, 3); |
| 461 | } |
| 462 | |
| 463 | if (OptLevelO1 || OptLevelO2 || OptLevelO3) { |
| 464 | for (Module::iterator I = M.get()->begin(), E = M.get()->end(); |
| 465 | I != E; ++I) |
| 466 | FPasses->run(*I); |
| 467 | } |
| 468 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 469 | // Check that the module is well formed on completion of optimization |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 470 | if (!NoVerify && !VerifyEach) |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 471 | Passes.add(createVerifierPass()); |
| 472 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 473 | // Write bitcode out to disk or cout as the last step... |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 474 | if (!NoOutput && !AnalyzeOnly) |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 475 | Passes.add(createBitcodeWriterPass(*Out)); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 476 | |
| 477 | // Now that we have all of the passes ready, run them. |
| 478 | Passes.run(*M.get()); |
| 479 | |
Chris Lattner | d44ae90 | 2007-05-06 19:17:23 +0000 | [diff] [blame] | 480 | // Delete the ofstream. |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 481 | if (Out != &outs()) |
Chris Lattner | d44ae90 | 2007-05-06 19:17:23 +0000 | [diff] [blame] | 482 | delete Out; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 483 | return 0; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 484 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 485 | } catch (const std::string& msg) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 486 | cerr << argv[0] << ": " << msg << "\n"; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 487 | } catch (...) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 488 | cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 489 | } |
Chris Lattner | 0331524 | 2007-01-31 04:45:28 +0000 | [diff] [blame] | 490 | llvm_shutdown(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 491 | return 1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 492 | } |