blob: ffd2c21736e5043e942d846511b116a497ffd830 [file] [log] [blame]
Chris Lattner76351aa2004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner00950542001-06-06 20:29:01 +000010// Optimizations may be specified an arbitrary number of times on the command
Reid Spencerfd90dd52006-08-18 06:34:30 +000011// line, They are run in the order specified.
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattner0eafc312001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000014
Owen Anderson8b477ed2009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000017#include "llvm/PassManager.h"
Devang Patel28552da2007-06-28 23:09:25 +000018#include "llvm/CallGraphSCCPass.h"
Chris Lattnerb330e382007-05-06 02:42:03 +000019#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000020#include "llvm/Assembly/PrintModulePass.h"
Devang Patelf5f23002010-12-07 00:33:43 +000021#include "llvm/Analysis/DebugInfo.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000022#include "llvm/Analysis/Verifier.h"
Devang Patel1bc89362007-03-07 00:26:10 +000023#include "llvm/Analysis/LoopPass.h"
Tobias Grosser65513602010-10-20 01:54:44 +000024#include "llvm/Analysis/RegionPass.h"
Devang Patel28552da2007-06-28 23:09:25 +000025#include "llvm/Analysis/CallGraph.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000027#include "llvm/Target/TargetLibraryInfo.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000028#include "llvm/Target/TargetMachine.h"
Devang Patel1be99802011-04-04 19:51:17 +000029#include "llvm/ADT/StringSet.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000030#include "llvm/ADT/Triple.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000031#include "llvm/Support/PassNameParser.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
David Greene08fc0d32010-01-05 01:30:32 +000033#include "llvm/Support/Debug.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000034#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000035#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/PluginLoader.h"
Chris Lattnerc0d91b72009-12-09 00:41:28 +000037#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/Support/SystemUtils.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000039#include "llvm/Support/ToolOutputFile.h"
Reid Spencer62c51052006-08-21 05:34:03 +000040#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000041#include "llvm/LinkAllVMCore.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000042#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner63202322001-11-26 19:22:39 +000043#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000044#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000045using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000046
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000047// The OptimizationList is automatically populated with registered Passes by the
48// PassNameParser.
49//
Chris Lattner7f500f72006-08-27 22:07:01 +000050static cl::list<const PassInfo*, bool, PassNameParser>
51PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000052
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000053// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000054//
Chris Lattner6c8103f2003-05-22 20:13:16 +000055static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000056InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000057 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000058
Chris Lattner6c8103f2003-05-22 20:13:16 +000059static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000060OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000061 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000062
63static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000064Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000065
66static cl::opt<bool>
67PrintEachXForm("p", cl::desc("Print module after each transformation"));
68
69static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000070NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000071 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000072
73static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000074OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000075
76static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000077NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000078
79static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000080VerifyEach("verify-each", cl::desc("Verify after each transform"));
81
82static cl::opt<bool>
83StripDebug("strip-debug",
84 cl::desc("Strip debugger symbol info from translation unit"));
85
86static cl::opt<bool>
87DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
88
Eric Christophera887ae42009-08-21 23:29:40 +000089static cl::opt<bool>
90DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000091 cl::desc("Do not run any optimization passes"));
92
Eric Christophera887ae42009-08-21 23:29:40 +000093static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000094DisableInternalize("disable-internalize",
95 cl::desc("Do not mark all symbols as internal"));
96
Reid Spencer74ed9972007-02-02 14:46:29 +000097static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000098StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +000099 cl::desc("Include the standard compile time optimizations"));
100
101static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000102StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000103 cl::desc("Include the standard link time optimizations"));
104
105static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000106OptLevelO1("O1",
107 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
108
109static cl::opt<bool>
110OptLevelO2("O2",
Devang Pateld9424ed2008-09-17 00:01:04 +0000111 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000112
113static cl::opt<bool>
114OptLevelO3("O3",
Devang Pateld9424ed2008-09-17 00:01:04 +0000115 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000116
117static cl::opt<bool>
118UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000119 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000120 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000121
122static cl::opt<bool>
123DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000124 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000125
126static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000127Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000128
Reid Spencerec7eb452004-05-27 16:28:54 +0000129static cl::alias
130QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
131
Reid Spencerfd90dd52006-08-18 06:34:30 +0000132static cl::opt<bool>
133AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
134
Devang Patelf5f23002010-12-07 00:33:43 +0000135static cl::opt<bool>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000136PrintBreakpoints("print-breakpoints-for-testing",
Devang Patelf5f23002010-12-07 00:33:43 +0000137 cl::desc("Print select breakpoints location for testing"));
138
Chris Lattnerd331cb32009-10-22 00:44:10 +0000139static cl::opt<std::string>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000140DefaultDataLayout("default-data-layout",
Chris Lattnerd331cb32009-10-22 00:44:10 +0000141 cl::desc("data layout string to use if not specified by module"),
142 cl::value_desc("layout-string"), cl::init(""));
143
Reid Spencerfd90dd52006-08-18 06:34:30 +0000144// ---------- Define Printers for module and function passes ------------
145namespace {
146
Devang Patel28552da2007-06-28 23:09:25 +0000147struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
148 static char ID;
149 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000150 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000151 std::string PassName;
152
Dan Gohman4931b312010-08-18 17:42:59 +0000153 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000154 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
155 std::string PassToPrintName = PassToPrint->getPassName();
156 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
157 }
Devang Patel28552da2007-06-28 23:09:25 +0000158
Chris Lattner2decb222010-04-16 22:42:17 +0000159 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000160 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000161 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000162
Devang Patel28552da2007-06-28 23:09:25 +0000163 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000164 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
165 Function *F = (*I)->getFunction();
166 if (F)
167 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
168 F->getParent());
169 }
Devang Patel28552da2007-06-28 23:09:25 +0000170 return false;
171 }
Eric Christophera887ae42009-08-21 23:29:40 +0000172
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000173 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000174
175 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000176 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000177 AU.setPreservesAll();
178 }
179};
180
181char CallGraphSCCPassPrinter::ID = 0;
182
Reid Spencerfd90dd52006-08-18 06:34:30 +0000183struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000184 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000185 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000186 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000187 std::string PassName;
188
Dan Gohman4931b312010-08-18 17:42:59 +0000189 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000190 : ModulePass(ID), PassToPrint(PI), Out(out) {
191 std::string PassToPrintName = PassToPrint->getPassName();
192 PassName = "ModulePass Printer: " + PassToPrintName;
193 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000194
195 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000196 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000197 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000198
199 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000200 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000201 return false;
202 }
203
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000204 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000205
206 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000207 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000208 AU.setPreservesAll();
209 }
210};
211
Devang Patel19974732007-05-03 01:11:54 +0000212char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000213struct FunctionPassPrinter : public FunctionPass {
214 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000215 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000216 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000217 std::string PassName;
218
Dan Gohman4931b312010-08-18 17:42:59 +0000219 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000220 : FunctionPass(ID), PassToPrint(PI), Out(out) {
221 std::string PassToPrintName = PassToPrint->getPassName();
222 PassName = "FunctionPass Printer: " + PassToPrintName;
223 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000224
225 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000226 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000227 Out << "Printing analysis '" << PassToPrint->getPassName()
228 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000229
Reid Spencerfd90dd52006-08-18 06:34:30 +0000230 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000231 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000232 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000233 return false;
234 }
235
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000236 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000237
238 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000239 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000240 AU.setPreservesAll();
241 }
242};
243
Devang Patel19974732007-05-03 01:11:54 +0000244char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000245
246struct LoopPassPrinter : public LoopPass {
247 static char ID;
248 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000249 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000250 std::string PassName;
251
Dan Gohman4931b312010-08-18 17:42:59 +0000252 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000253 LoopPass(ID), PassToPrint(PI), Out(out) {
254 std::string PassToPrintName = PassToPrint->getPassName();
255 PassName = "LoopPass Printer: " + PassToPrintName;
256 }
257
Devang Patel56fb1642007-07-05 15:32:03 +0000258
259 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000260 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000261 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000262
Devang Patel56fb1642007-07-05 15:32:03 +0000263 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000264 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
265 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000266 return false;
267 }
Eric Christophera887ae42009-08-21 23:29:40 +0000268
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000269 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000270
271 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000272 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000273 AU.setPreservesAll();
274 }
275};
276
277char LoopPassPrinter::ID = 0;
278
Tobias Grosser65513602010-10-20 01:54:44 +0000279struct RegionPassPrinter : public RegionPass {
280 static char ID;
281 const PassInfo *PassToPrint;
282 raw_ostream &Out;
283 std::string PassName;
284
285 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
286 PassToPrint(PI), Out(out) {
287 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser58396142011-01-20 21:03:20 +0000288 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000289 }
290
291 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
292 if (!Quiet) {
293 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
294 << "region: '" << R->getNameStr() << "' in function '"
295 << R->getEntry()->getParent()->getNameStr() << "':\n";
296 }
297 // Get and print pass...
298 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
299 R->getEntry()->getParent()->getParent());
300 return false;
301 }
302
Tobias Grosser58396142011-01-20 21:03:20 +0000303 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000304
305 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
306 AU.addRequiredID(PassToPrint->getTypeInfo());
307 AU.setPreservesAll();
308 }
309};
310
311char RegionPassPrinter::ID = 0;
312
Reid Spencerfd90dd52006-08-18 06:34:30 +0000313struct BasicBlockPassPrinter : public BasicBlockPass {
314 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000315 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000316 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000317 std::string PassName;
318
Dan Gohman4931b312010-08-18 17:42:59 +0000319 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000320 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
321 std::string PassToPrintName = PassToPrint->getPassName();
322 PassName = "BasicBlockPass Printer: " + PassToPrintName;
323 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000324
325 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000326 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000327 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
328 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000329
330 // Get and print pass...
Andrew Tricka41af7a2011-04-05 18:41:31 +0000331 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000332 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000333 return false;
334 }
335
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000336 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000337
338 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000339 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000340 AU.setPreservesAll();
341 }
342};
343
Devang Patel19974732007-05-03 01:11:54 +0000344char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000345
Devang Patel1be99802011-04-04 19:51:17 +0000346struct BreakpointPrinter : public ModulePass {
Devang Patelf5f23002010-12-07 00:33:43 +0000347 raw_ostream &Out;
348 static char ID;
349
350 BreakpointPrinter(raw_ostream &out)
Devang Patel1be99802011-04-04 19:51:17 +0000351 : ModulePass(ID), Out(out) {
Devang Patelf5f23002010-12-07 00:33:43 +0000352 }
353
Devang Patel1be99802011-04-04 19:51:17 +0000354 void getContextName(DIDescriptor Context, std::string &N) {
355 if (Context.isNameSpace()) {
356 DINameSpace NS(Context);
357 if (!NS.getName().empty()) {
358 getContextName(NS.getContext(), N);
359 N = N + NS.getName().str() + "::";
Devang Patel97f6d5b2011-01-31 21:36:24 +0000360 }
Devang Patel1be99802011-04-04 19:51:17 +0000361 } else if (Context.isType()) {
362 DIType TY(Context);
363 if (!TY.getName().empty()) {
364 getContextName(TY.getContext(), N);
365 N = N + TY.getName().str() + "::";
366 }
367 }
368 }
369
370 virtual bool runOnModule(Module &M) {
371 StringSet<> Processed;
372 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
373 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
374 std::string Name;
375 DISubprogram SP(NMD->getOperand(i));
376 if (SP.Verify())
377 getContextName(SP.getContext(), Name);
378 Name = Name + SP.getDisplayName().str();
379 if (!Name.empty() && Processed.insert(Name)) {
380 Out << Name << "\n";
381 }
382 }
Devang Patelf5f23002010-12-07 00:33:43 +0000383 return false;
384 }
385
386 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
387 AU.setPreservesAll();
388 }
389};
Chris Lattner52b28892011-05-22 00:21:33 +0000390
391} // anonymous namespace
Devang Patelf5f23002010-12-07 00:33:43 +0000392
393char BreakpointPrinter::ID = 0;
394
Chris Lattner52b28892011-05-22 00:21:33 +0000395static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000396 // Add the pass to the pass manager...
397 PM.add(P);
398
399 // If we are verifying all of the intermediate steps, add the verifier...
400 if (VerifyEach) PM.add(createVerifierPass());
401}
402
Eric Christophera887ae42009-08-21 23:29:40 +0000403/// AddOptimizationPasses - This routine adds optimization passes
404/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000405/// duplicates llvm-gcc behaviour.
406///
407/// OptLevel - Optimization Level
Chris Lattner52b28892011-05-22 00:21:33 +0000408static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
409 unsigned OptLevel) {
410 PassManagerBuilder Builder;
411 Builder.OptLevel = OptLevel;
Devang Patel2d7551c2008-09-16 22:25:14 +0000412
Eli Friedman74733a72010-01-18 22:38:31 +0000413 if (DisableInline) {
414 // No inlining pass
Eli Friedman42d41fd2011-06-06 22:13:27 +0000415 } else if (OptLevel > 1) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000416 unsigned Threshold = 225;
Eli Friedman74733a72010-01-18 22:38:31 +0000417 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000418 Threshold = 275;
Chris Lattner52b28892011-05-22 00:21:33 +0000419 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedman74733a72010-01-18 22:38:31 +0000420 } else {
Chris Lattner52b28892011-05-22 00:21:33 +0000421 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedman74733a72010-01-18 22:38:31 +0000422 }
Chris Lattner52b28892011-05-22 00:21:33 +0000423 Builder.DisableUnitAtATime = !UnitAtATime;
424 Builder.DisableUnrollLoops = OptLevel == 0;
425 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
426
427 Builder.populateFunctionPassManager(FPM);
428 Builder.populateModulePassManager(MPM);
Devang Patel2d7551c2008-09-16 22:25:14 +0000429}
430
Chris Lattner52b28892011-05-22 00:21:33 +0000431static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000432 PM.add(createVerifierPass()); // Verify that input is correct
433
Reid Spencer74ed9972007-02-02 14:46:29 +0000434 // If the -strip-debug command line option was specified, do it.
435 if (StripDebug)
436 addPass(PM, createStripSymbolsPass(true));
437
438 if (DisableOptimizations) return;
439
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000440 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner52b28892011-05-22 00:21:33 +0000441 PassManagerBuilder Builder;
442 if (!DisableInline)
443 Builder.Inliner = createFunctionInliningPass();
444 Builder.OptLevel = 3;
445 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
446 Builder.populateModulePassManager(PM);
Reid Spencer74ed9972007-02-02 14:46:29 +0000447}
448
Chris Lattner52b28892011-05-22 00:21:33 +0000449static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000450 PM.add(createVerifierPass()); // Verify that input is correct
451
452 // If the -strip-debug command line option was specified, do it.
453 if (StripDebug)
454 addPass(PM, createStripSymbolsPass(true));
455
456 if (DisableOptimizations) return;
457
Chris Lattner52b28892011-05-22 00:21:33 +0000458 PassManagerBuilder Builder;
459 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
460 /*RunInliner=*/ !DisableInline);
Daniel Dunbaradc82882009-07-17 18:09:39 +0000461}
462
Chris Lattner0be41012002-02-01 04:54:11 +0000463
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000464//===----------------------------------------------------------------------===//
465// main for opt
466//
Chris Lattner00950542001-06-06 20:29:01 +0000467int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000468 sys::PrintStackTraceOnErrorSignal();
469 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000470
David Greene08fc0d32010-01-05 01:30:32 +0000471 // Enable debug stream buffering.
472 EnableDebugBuffering = true;
473
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000474 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000475 LLVMContext &Context = getGlobalContext();
Andrew Tricka41af7a2011-04-05 18:41:31 +0000476
Owen Anderson081c34b2010-10-19 17:21:58 +0000477 // Initialize passes
478 PassRegistry &Registry = *PassRegistry::getPassRegistry();
479 initializeCore(Registry);
480 initializeScalarOpts(Registry);
481 initializeIPO(Registry);
482 initializeAnalysis(Registry);
483 initializeIPA(Registry);
484 initializeTransformUtils(Registry);
485 initializeInstCombine(Registry);
486 initializeInstrumentation(Registry);
487 initializeTarget(Registry);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000488
Chris Lattner61db1a12009-10-22 00:46:41 +0000489 cl::ParseCommandLineOptions(argc, argv,
490 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000491
Tobias Grosser7593f342010-12-02 20:35:16 +0000492 if (AnalyzeOnly && NoOutput) {
493 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
494 return 1;
495 }
496
Chris Lattner61db1a12009-10-22 00:46:41 +0000497 // Allocate a full target machine description only if necessary.
498 // FIXME: The choice of target should be controllable on the command line.
499 std::auto_ptr<TargetMachine> target;
Devang Patelff5d06d2008-08-27 20:51:49 +0000500
Chris Lattner61db1a12009-10-22 00:46:41 +0000501 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000502
Chris Lattner61db1a12009-10-22 00:46:41 +0000503 // Load the input module...
504 std::auto_ptr<Module> M;
505 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000506
Chris Lattner61db1a12009-10-22 00:46:41 +0000507 if (M.get() == 0) {
508 Err.Print(argv[0], errs());
509 return 1;
510 }
511
512 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000513 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000514 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000515 if (!OutputFilename.empty())
516 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000517 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000518 } else {
519 // Default to standard output.
520 if (OutputFilename.empty())
521 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000522
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000523 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000524 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
525 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000526 if (!ErrorInfo.empty()) {
527 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000528 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000529 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000530 }
Chris Lattner76d12292002-04-18 19:55:25 +0000531
Chris Lattner61db1a12009-10-22 00:46:41 +0000532 // If the output is set to be emitted to standard out, and standard out is a
533 // console, print out a warning message and refuse to do it. We don't
534 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000535 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000536 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000537 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000538
Chris Lattner61db1a12009-10-22 00:46:41 +0000539 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000540 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000541 //
542 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000543
Chris Lattner2a66aca2011-02-18 22:13:01 +0000544 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner188a7e02011-02-18 22:34:03 +0000545 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Tricka41af7a2011-04-05 18:41:31 +0000546
Chris Lattner188a7e02011-02-18 22:34:03 +0000547 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
548 if (DisableSimplifyLibCalls)
549 TLI->disableAllFunctions();
550 Passes.add(TLI);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000551
Chris Lattner2a66aca2011-02-18 22:13:01 +0000552 // Add an appropriate TargetData instance for this module.
Chris Lattner61db1a12009-10-22 00:46:41 +0000553 TargetData *TD = 0;
554 const std::string &ModuleDataLayout = M.get()->getDataLayout();
555 if (!ModuleDataLayout.empty())
556 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000557 else if (!DefaultDataLayout.empty())
Chris Lattner61db1a12009-10-22 00:46:41 +0000558 TD = new TargetData(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000559
Chris Lattner61db1a12009-10-22 00:46:41 +0000560 if (TD)
561 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000562
Chris Lattner52b28892011-05-22 00:21:33 +0000563 OwningPtr<FunctionPassManager> FPasses;
Chris Lattner61db1a12009-10-22 00:46:41 +0000564 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Chris Lattner52b28892011-05-22 00:21:33 +0000565 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnerd331cb32009-10-22 00:44:10 +0000566 if (TD)
Chris Lattner61db1a12009-10-22 00:46:41 +0000567 FPasses->add(new TargetData(*TD));
568 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000569
Devang Patelf5f23002010-12-07 00:33:43 +0000570 if (PrintBreakpoints) {
571 // Default to standard output.
572 if (!Out) {
573 if (OutputFilename.empty())
574 OutputFilename = "-";
Andrew Tricka41af7a2011-04-05 18:41:31 +0000575
Devang Patelf5f23002010-12-07 00:33:43 +0000576 std::string ErrorInfo;
577 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
578 raw_fd_ostream::F_Binary));
579 if (!ErrorInfo.empty()) {
580 errs() << ErrorInfo << '\n';
581 return 1;
582 }
583 }
584 Passes.add(new BreakpointPrinter(Out->os()));
585 NoOutput = true;
586 }
587
Chris Lattner61db1a12009-10-22 00:46:41 +0000588 // If the -strip-debug command line option was specified, add it. If
589 // -std-compile-opts was also specified, it will handle StripDebug.
590 if (StripDebug && !StandardCompileOpts)
591 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000592
Chris Lattner61db1a12009-10-22 00:46:41 +0000593 // Create a new optimization pass for each one specified on the command line
594 for (unsigned i = 0; i < PassList.size(); ++i) {
595 // Check to see if -std-compile-opts was specified before this option. If
596 // so, handle it.
597 if (StandardCompileOpts &&
598 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000599 AddStandardCompilePasses(Passes);
600 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000601 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000602
Chris Lattner61db1a12009-10-22 00:46:41 +0000603 if (StandardLinkOpts &&
604 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000605 AddStandardLinkPasses(Passes);
606 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000607 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000608
Chris Lattner61db1a12009-10-22 00:46:41 +0000609 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000610 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner61db1a12009-10-22 00:46:41 +0000611 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000612 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000613
Chris Lattner61db1a12009-10-22 00:46:41 +0000614 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000615 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000616 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000617 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000618
Chris Lattner61db1a12009-10-22 00:46:41 +0000619 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000620 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner61db1a12009-10-22 00:46:41 +0000621 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000622 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000623
Chris Lattner61db1a12009-10-22 00:46:41 +0000624 const PassInfo *PassInf = PassList[i];
625 Pass *P = 0;
626 if (PassInf->getNormalCtor())
627 P = PassInf->getNormalCtor()();
628 else
629 errs() << argv[0] << ": cannot create pass: "
630 << PassInf->getPassName() << "\n";
631 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000632 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000633 addPass(Passes, P);
634
635 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000636 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000637 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000638 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000639 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000640 case PT_Region:
641 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
642 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000643 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000644 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000645 break;
646 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000647 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000648 break;
649 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000650 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000651 break;
652 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000653 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000654 break;
655 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000656 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000657 }
658
Chris Lattner61db1a12009-10-22 00:46:41 +0000659 if (PrintEachXForm)
660 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000661 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000662
663 // If -std-compile-opts was specified at the end of the pass list, add them.
664 if (StandardCompileOpts) {
665 AddStandardCompilePasses(Passes);
666 StandardCompileOpts = false;
667 }
668
669 if (StandardLinkOpts) {
670 AddStandardLinkPasses(Passes);
671 StandardLinkOpts = false;
672 }
673
674 if (OptLevelO1)
675 AddOptimizationPasses(Passes, *FPasses, 1);
676
677 if (OptLevelO2)
678 AddOptimizationPasses(Passes, *FPasses, 2);
679
680 if (OptLevelO3)
681 AddOptimizationPasses(Passes, *FPasses, 3);
682
Chris Lattner3e8984a2011-05-22 06:44:19 +0000683 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
684 FPasses->doInitialization();
Chris Lattner52b28892011-05-22 00:21:33 +0000685 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
686 FPasses->run(*F);
Chris Lattner3e8984a2011-05-22 06:44:19 +0000687 FPasses->doFinalization();
688 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000689
690 // Check that the module is well formed on completion of optimization
691 if (!NoVerify && !VerifyEach)
692 Passes.add(createVerifierPass());
693
Dan Gohman4931b312010-08-18 17:42:59 +0000694 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000695 if (!NoOutput && !AnalyzeOnly) {
696 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000697 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000698 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000699 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000700 }
701
Andrew Trickce969022011-04-05 18:54:36 +0000702 // Before executing passes, print the final values of the LLVM options.
703 cl::PrintOptionValues();
704
Chris Lattner61db1a12009-10-22 00:46:41 +0000705 // Now that we have all of the passes ready, run them.
706 Passes.run(*M.get());
707
Dan Gohmand5826a32010-08-20 01:07:01 +0000708 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000709 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000710 Out->keep();
711
Chris Lattner61db1a12009-10-22 00:46:41 +0000712 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000713}