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