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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 16 | #include "llvm/PassManager.h" |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 17 | #include "llvm/CallGraphSCCPass.h" |
Chris Lattner | b330e38 | 2007-05-06 02:42:03 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | ffa6f9c | 2001-10-19 15:39:14 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 22d26d7 | 2002-02-20 17:56:53 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Verifier.h" |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.h" |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CallGraph.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 2053a2a | 2002-07-26 21:09:32 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PassNameParser.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 26 | #include "llvm/System/Signals.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | b330e38 | 2007-05-06 02:42:03 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/PluginLoader.h" |
Bill Wendling | a5b31ca | 2006-11-28 23:33:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Streams.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/SystemUtils.h" |
Reid Spencer | 62c5105 | 2006-08-21 05:34:03 +0000 | [diff] [blame] | 32 | #include "llvm/LinkAllPasses.h" |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 33 | #include "llvm/LinkAllVMCore.h" |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 34 | #include <iostream> |
Chris Lattner | 73e11d7 | 2001-10-18 06:13:08 +0000 | [diff] [blame] | 35 | #include <fstream> |
Chris Lattner | 6320232 | 2001-11-26 19:22:39 +0000 | [diff] [blame] | 36 | #include <memory> |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Chris Lattner | 9d6e7eb | 2002-04-12 18:21:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 40 | // The OptimizationList is automatically populated with registered Passes by the |
| 41 | // PassNameParser. |
| 42 | // |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 43 | static cl::list<const PassInfo*, bool, PassNameParser> |
| 44 | PassList(cl::desc("Optimizations available:")); |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 45 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 46 | // Other command line options... |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 47 | // |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 48 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 49 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 50 | cl::init("-"), cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 52 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 53 | OutputFilename("o", cl::desc("Override output filename"), |
Chris Lattner | b592fc2 | 2003-12-10 14:41:33 +0000 | [diff] [blame] | 54 | cl::value_desc("filename"), cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | |
| 56 | static cl::opt<bool> |
| 57 | Force("f", cl::desc("Overwrite output files")); |
| 58 | |
| 59 | static cl::opt<bool> |
| 60 | PrintEachXForm("p", cl::desc("Print module after each transformation")); |
| 61 | |
| 62 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 63 | NoOutput("disable-output", |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 64 | cl::desc("Do not write result bitcode file"), cl::Hidden); |
Chris Lattner | d70b68e | 2003-02-12 18:43:33 +0000 | [diff] [blame] | 65 | |
| 66 | static cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 67 | NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden); |
Chris Lattner | f3bafc1 | 2003-02-12 18:45:08 +0000 | [diff] [blame] | 68 | |
| 69 | static cl::opt<bool> |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 70 | VerifyEach("verify-each", cl::desc("Verify after each transform")); |
| 71 | |
| 72 | static cl::opt<bool> |
| 73 | StripDebug("strip-debug", |
| 74 | cl::desc("Strip debugger symbol info from translation unit")); |
| 75 | |
| 76 | static cl::opt<bool> |
| 77 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| 78 | |
| 79 | static cl::opt<bool> |
| 80 | DisableOptimizations("disable-opt", |
| 81 | cl::desc("Do not run any optimization passes")); |
| 82 | |
| 83 | static cl::opt<bool> |
| 84 | StandardCompileOpts("std-compile-opts", |
| 85 | cl::desc("Include the standard compile time optimizations")); |
| 86 | |
| 87 | static cl::opt<bool> |
Chris Lattner | 3153e4f | 2004-05-27 20:32:10 +0000 | [diff] [blame] | 88 | Quiet("q", cl::desc("Obsolete option"), cl::Hidden); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 89 | |
Reid Spencer | ec7eb45 | 2004-05-27 16:28:54 +0000 | [diff] [blame] | 90 | static cl::alias |
| 91 | QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); |
| 92 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 93 | static cl::opt<bool> |
| 94 | AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); |
| 95 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 96 | // ---------- Define Printers for module and function passes ------------ |
| 97 | namespace { |
| 98 | |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 99 | struct CallGraphSCCPassPrinter : public CallGraphSCCPass { |
| 100 | static char ID; |
| 101 | const PassInfo *PassToPrint; |
| 102 | CallGraphSCCPassPrinter(const PassInfo *PI) : |
| 103 | CallGraphSCCPass((intptr_t)&ID), PassToPrint(PI) {} |
| 104 | |
| 105 | virtual bool runOnSCC(const std::vector<CallGraphNode *>&SCC) { |
| 106 | if (!Quiet) { |
| 107 | cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
| 108 | |
| 109 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 110 | Function *F = SCC[i]->getFunction(); |
| 111 | if (F) |
| 112 | getAnalysisID<Pass>(PassToPrint).print(cout, F->getParent()); |
| 113 | } |
| 114 | } |
| 115 | // Get and print pass... |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | virtual const char *getPassName() const { return "'Pass' Printer"; } |
| 120 | |
| 121 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 122 | AU.addRequiredID(PassToPrint); |
| 123 | AU.setPreservesAll(); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | char CallGraphSCCPassPrinter::ID = 0; |
| 128 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 129 | struct ModulePassPrinter : public ModulePass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 130 | static char ID; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 131 | const PassInfo *PassToPrint; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 132 | ModulePassPrinter(const PassInfo *PI) : ModulePass((intptr_t)&ID), |
| 133 | PassToPrint(PI) {} |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 134 | |
| 135 | virtual bool runOnModule(Module &M) { |
| 136 | if (!Quiet) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 137 | cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
| 138 | getAnalysisID<Pass>(PassToPrint).print(cout, &M); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Get and print pass... |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | virtual const char *getPassName() const { return "'Pass' Printer"; } |
| 146 | |
| 147 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 148 | AU.addRequiredID(PassToPrint); |
| 149 | AU.setPreservesAll(); |
| 150 | } |
| 151 | }; |
| 152 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 153 | char ModulePassPrinter::ID = 0; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 154 | struct FunctionPassPrinter : public FunctionPass { |
| 155 | const PassInfo *PassToPrint; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 156 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 157 | FunctionPassPrinter(const PassInfo *PI) : FunctionPass((intptr_t)&ID), |
| 158 | PassToPrint(PI) {} |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 159 | |
| 160 | virtual bool runOnFunction(Function &F) { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 161 | if (!Quiet) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 162 | cout << "Printing analysis '" << PassToPrint->getPassName() |
| 163 | << "' for function '" << F.getName() << "':\n"; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 164 | } |
| 165 | // Get and print pass... |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 166 | getAnalysisID<Pass>(PassToPrint).print(cout, F.getParent()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
| 170 | virtual const char *getPassName() const { return "FunctionPass Printer"; } |
| 171 | |
| 172 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 173 | AU.addRequiredID(PassToPrint); |
| 174 | AU.setPreservesAll(); |
| 175 | } |
| 176 | }; |
| 177 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 178 | char FunctionPassPrinter::ID = 0; |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 179 | |
| 180 | struct LoopPassPrinter : public LoopPass { |
| 181 | static char ID; |
| 182 | const PassInfo *PassToPrint; |
| 183 | LoopPassPrinter(const PassInfo *PI) : |
| 184 | LoopPass((intptr_t)&ID), PassToPrint(PI) {} |
| 185 | |
| 186 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { |
| 187 | if (!Quiet) { |
| 188 | cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; |
| 189 | getAnalysisID<Pass>(PassToPrint).print(cout, |
| 190 | L->getHeader()->getParent()->getParent()); |
| 191 | } |
| 192 | // Get and print pass... |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | virtual const char *getPassName() const { return "'Pass' Printer"; } |
| 197 | |
| 198 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 199 | AU.addRequiredID(PassToPrint); |
| 200 | AU.setPreservesAll(); |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | char LoopPassPrinter::ID = 0; |
| 205 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 206 | struct BasicBlockPassPrinter : public BasicBlockPass { |
| 207 | const PassInfo *PassToPrint; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 208 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 209 | BasicBlockPassPrinter(const PassInfo *PI) |
| 210 | : BasicBlockPass((intptr_t)&ID), PassToPrint(PI) {} |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 211 | |
| 212 | virtual bool runOnBasicBlock(BasicBlock &BB) { |
| 213 | if (!Quiet) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 214 | cout << "Printing Analysis info for BasicBlock '" << BB.getName() |
| 215 | << "': Pass " << PassToPrint->getPassName() << ":\n"; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Get and print pass... |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 219 | getAnalysisID<Pass>(PassToPrint).print(cout, BB.getParent()->getParent()); |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
| 223 | virtual const char *getPassName() const { return "BasicBlockPass Printer"; } |
| 224 | |
| 225 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 226 | AU.addRequiredID(PassToPrint); |
| 227 | AU.setPreservesAll(); |
| 228 | } |
| 229 | }; |
| 230 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 231 | char BasicBlockPassPrinter::ID = 0; |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 232 | inline void addPass(PassManager &PM, Pass *P) { |
| 233 | // Add the pass to the pass manager... |
| 234 | PM.add(P); |
| 235 | |
| 236 | // If we are verifying all of the intermediate steps, add the verifier... |
| 237 | if (VerifyEach) PM.add(createVerifierPass()); |
| 238 | } |
| 239 | |
| 240 | void AddStandardCompilePasses(PassManager &PM) { |
| 241 | PM.add(createVerifierPass()); // Verify that input is correct |
| 242 | |
| 243 | addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 244 | |
| 245 | // If the -strip-debug command line option was specified, do it. |
| 246 | if (StripDebug) |
| 247 | addPass(PM, createStripSymbolsPass(true)); |
| 248 | |
| 249 | if (DisableOptimizations) return; |
| 250 | |
| 251 | addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst |
| 252 | addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code |
| 253 | addPass(PM, createPromoteMemoryToRegisterPass());// Kill useless allocas |
| 254 | addPass(PM, createGlobalOptimizerPass()); // Optimize out global vars |
| 255 | addPass(PM, createGlobalDCEPass()); // Remove unused fns and globs |
| 256 | addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation |
| 257 | addPass(PM, createDeadArgEliminationPass()); // Dead argument elimination |
| 258 | addPass(PM, createInstructionCombiningPass()); // Clean up after IPCP & DAE |
| 259 | addPass(PM, createCFGSimplificationPass()); // Clean up after IPCP & DAE |
| 260 | |
| 261 | addPass(PM, createPruneEHPass()); // Remove dead EH info |
| 262 | |
| 263 | if (!DisableInline) |
| 264 | addPass(PM, createFunctionInliningPass()); // Inline small functions |
| 265 | addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args |
| 266 | |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 267 | addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code |
| 268 | addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl. |
| 269 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
Evan Cheng | a8b9a7b | 2007-07-17 01:52:05 +0000 | [diff] [blame] | 270 | // FIXME: Temporary! |
Evan Cheng | 74b1e14 | 2007-07-17 20:07:21 +0000 | [diff] [blame^] | 271 | addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 272 | addPass(PM, createInstructionCombiningPass()); // Combine silly seq's |
| 273 | addPass(PM, createCondPropagationPass()); // Propagate conditionals |
| 274 | |
| 275 | addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls |
| 276 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
| 277 | addPass(PM, createReassociatePass()); // Reassociate expressions |
Devang Patel | 0aa37f4 | 2007-04-10 15:43:36 +0000 | [diff] [blame] | 278 | addPass(PM, createLoopRotatePass()); |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 279 | addPass(PM, createLICMPass()); // Hoist loop invariants |
| 280 | addPass(PM, createLoopUnswitchPass()); // Unswitch loops. |
| 281 | addPass(PM, createInstructionCombiningPass()); // Clean up after LICM/reassoc |
| 282 | addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars |
| 283 | addPass(PM, createLoopUnrollPass()); // Unroll small loops |
| 284 | addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller |
| 285 | addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions |
| 286 | addPass(PM, createGCSEPass()); // Remove common subexprs |
| 287 | addPass(PM, createSCCPPass()); // Constant prop with SCCP |
| 288 | |
| 289 | // Run instcombine after redundancy elimination to exploit opportunities |
| 290 | // opened up by them. |
| 291 | addPass(PM, createInstructionCombiningPass()); |
| 292 | addPass(PM, createCondPropagationPass()); // Propagate conditionals |
| 293 | |
| 294 | addPass(PM, createDeadStoreEliminationPass()); // Delete dead stores |
| 295 | addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE' |
| 296 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
| 297 | addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations |
| 298 | addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types |
| 299 | addPass(PM, createConstantMergePass()); // Merge dup global constants |
| 300 | } |
| 301 | |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 302 | } // anonymous namespace |
| 303 | |
Chris Lattner | 0be4101 | 2002-02-01 04:54:11 +0000 | [diff] [blame] | 304 | |
Chris Lattner | c0ce68b | 2002-07-23 18:12:22 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
| 306 | // main for opt |
| 307 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 308 | int main(int argc, char **argv) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 309 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 310 | try { |
| 311 | cl::ParseCommandLineOptions(argc, argv, |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 312 | " llvm .bc -> .bc modular optimizer and analysis printer \n"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 313 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 315 | // Allocate a full target machine description only if necessary. |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 316 | // FIXME: The choice of target should be controllable on the command line. |
| 317 | std::auto_ptr<TargetMachine> target; |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 318 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 319 | std::string ErrorMessage; |
Vikram S. Adve | 18fdfc4 | 2002-09-16 16:09:43 +0000 | [diff] [blame] | 320 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 321 | // Load the input module... |
Chris Lattner | b330e38 | 2007-05-06 02:42:03 +0000 | [diff] [blame] | 322 | std::auto_ptr<Module> M; |
Chris Lattner | 065344d | 2007-05-06 23:45:49 +0000 | [diff] [blame] | 323 | if (MemoryBuffer *Buffer |
| 324 | = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) { |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 325 | M.reset(ParseBitcodeFile(Buffer, &ErrorMessage)); |
Chris Lattner | 065344d | 2007-05-06 23:45:49 +0000 | [diff] [blame] | 326 | delete Buffer; |
| 327 | } |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 328 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 329 | if (M.get() == 0) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 330 | cerr << argv[0] << ": "; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 331 | if (ErrorMessage.size()) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 332 | cerr << ErrorMessage << "\n"; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 333 | else |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 334 | cerr << "bitcode didn't read correctly.\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 335 | return 1; |
| 336 | } |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 337 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 338 | // Figure out what stream we are supposed to write to... |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 339 | // FIXME: cout is not binary! |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 340 | std::ostream *Out = &std::cout; // Default to printing to stdout... |
| 341 | if (OutputFilename != "-") { |
| 342 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 343 | // If force is not specified, make sure not to overwrite a file! |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 344 | cerr << argv[0] << ": error opening '" << OutputFilename |
| 345 | << "': file exists!\n" |
| 346 | << "Use -f command line argument to force output\n"; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 347 | return 1; |
| 348 | } |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 349 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 350 | std::ios::binary; |
| 351 | Out = new std::ofstream(OutputFilename.c_str(), io_mode); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 352 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 353 | if (!Out->good()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 354 | cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 355 | return 1; |
| 356 | } |
Chris Lattner | 76351aa | 2004-04-02 05:06:57 +0000 | [diff] [blame] | 357 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 358 | // Make sure that the Output file gets unlinked from the disk if we get a |
| 359 | // SIGINT |
| 360 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 361 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 362 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 363 | // 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] | 364 | // console, print out a warning message and refuse to do it. We don't |
| 365 | // impress anyone by spewing tons of binary goo to a terminal. |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 366 | if (!Force && !NoOutput && CheckBitcodeOutputToConsole(Out,!Quiet)) { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 367 | NoOutput = true; |
| 368 | } |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 369 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 370 | // Create a PassManager to hold and optimize the collection of passes we are |
| 371 | // about to build... |
| 372 | // |
| 373 | PassManager Passes; |
| 374 | |
| 375 | // Add an appropriate TargetData instance for this module... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 376 | Passes.add(new TargetData(M.get())); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 377 | |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 378 | // If -std-compile-opts is given, add in all the standard compilation |
| 379 | // optimizations first. This will handle -strip-debug, -disable-inline, |
| 380 | // and -disable-opt as well. |
| 381 | if (StandardCompileOpts) |
| 382 | AddStandardCompilePasses(Passes); |
| 383 | |
| 384 | // otherwise if the -strip-debug command line option was specified, add it. |
| 385 | else if (StripDebug) |
| 386 | addPass(Passes, createStripSymbolsPass(true)); |
| 387 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 388 | // 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] | 389 | for (unsigned i = 0; i < PassList.size(); ++i) { |
| 390 | const PassInfo *PassInf = PassList[i]; |
| 391 | Pass *P = 0; |
| 392 | if (PassInf->getNormalCtor()) |
| 393 | P = PassInf->getNormalCtor()(); |
Chris Lattner | cd950a5 | 2006-12-01 21:59:37 +0000 | [diff] [blame] | 394 | else |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 395 | cerr << argv[0] << ": cannot create pass: " |
| 396 | << PassInf->getPassName() << "\n"; |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 397 | if (P) { |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 398 | addPass(Passes, P); |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 399 | |
| 400 | if (AnalyzeOnly) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 401 | if (dynamic_cast<BasicBlockPass*>(P)) |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 402 | Passes.add(new BasicBlockPassPrinter(PassInf)); |
Devang Patel | 56fb164 | 2007-07-05 15:32:03 +0000 | [diff] [blame] | 403 | else if (dynamic_cast<LoopPass*>(P)) |
| 404 | Passes.add(new LoopPassPrinter(PassInf)); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 405 | else if (dynamic_cast<FunctionPass*>(P)) |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 406 | Passes.add(new FunctionPassPrinter(PassInf)); |
Devang Patel | 28552da | 2007-06-28 23:09:25 +0000 | [diff] [blame] | 407 | else if (dynamic_cast<CallGraphSCCPass*>(P)) |
| 408 | Passes.add(new CallGraphSCCPassPrinter(PassInf)); |
Chris Lattner | 7f500f7 | 2006-08-27 22:07:01 +0000 | [diff] [blame] | 409 | else |
| 410 | Passes.add(new ModulePassPrinter(PassInf)); |
| 411 | } |
| 412 | } |
| 413 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 414 | if (PrintEachXForm) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 415 | Passes.add(new PrintModulePass(&cerr)); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Check that the module is well formed on completion of optimization |
Reid Spencer | 74ed997 | 2007-02-02 14:46:29 +0000 | [diff] [blame] | 419 | if (!NoVerify && !VerifyEach) |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 420 | Passes.add(createVerifierPass()); |
| 421 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 422 | // Write bitcode out to disk or cout as the last step... |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 423 | if (!NoOutput && !AnalyzeOnly) |
| 424 | Passes.add(CreateBitcodeWriterPass(*Out)); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 425 | |
| 426 | // Now that we have all of the passes ready, run them. |
| 427 | Passes.run(*M.get()); |
| 428 | |
Chris Lattner | d44ae90 | 2007-05-06 19:17:23 +0000 | [diff] [blame] | 429 | // Delete the ofstream. |
| 430 | if (Out != &std::cout) |
| 431 | delete Out; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 432 | return 0; |
Reid Spencer | fd90dd5 | 2006-08-18 06:34:30 +0000 | [diff] [blame] | 433 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 434 | } catch (const std::string& msg) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 435 | cerr << argv[0] << ": " << msg << "\n"; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 436 | } catch (...) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 437 | cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Chris Lattner | fb1b3f1 | 2002-01-31 00:47:12 +0000 | [diff] [blame] | 438 | } |
Chris Lattner | 0331524 | 2007-01-31 04:45:28 +0000 | [diff] [blame] | 439 | llvm_shutdown(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 440 | return 1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 441 | } |