blob: af37e5e8c21b77b47db70937d00398f63fecf266 [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"
Devang Patel1be99802011-04-04 19:51:17 +000016#include "llvm/ADT/StringSet.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000017#include "llvm/ADT/Triple.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "llvm/Analysis/CallGraph.h"
19#include "llvm/Analysis/LoopPass.h"
20#include "llvm/Analysis/RegionPass.h"
21#include "llvm/Analysis/Verifier.h"
22#include "llvm/Assembly/PrintModulePass.h"
23#include "llvm/Bitcode/ReaderWriter.h"
24#include "llvm/CallGraphSCCPass.h"
25#include "llvm/CodeGen/CommandFlags.h"
26#include "llvm/DataLayout.h"
27#include "llvm/DebugInfo.h"
28#include "llvm/LinkAllPasses.h"
29#include "llvm/LinkAllVMCore.h"
30#include "llvm/MC/SubtargetFeature.h"
31#include "llvm/Module.h"
32#include "llvm/PassManager.h"
NAKAMURA Takumif2a68db2012-12-11 05:53:37 +000033#include "llvm/TargetTransformInfo.h"
David Greene08fc0d32010-01-05 01:30:32 +000034#include "llvm/Support/Debug.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000035#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000036#include "llvm/Support/ManagedStatic.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000037#include "llvm/Support/PassNameParser.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/Support/PluginLoader.h"
Chris Lattnerc0d91b72009-12-09 00:41:28 +000039#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000040#include "llvm/Support/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/Support/SystemUtils.h"
Nadav Rotemcbd9a192012-10-18 23:22:48 +000042#include "llvm/Support/TargetRegistry.h"
Nadav Rotem0873bea2012-10-24 17:23:50 +000043#include "llvm/Support/TargetSelect.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000044#include "llvm/Support/ToolOutputFile.h"
45#include "llvm/Target/TargetLibraryInfo.h"
46#include "llvm/Target/TargetMachine.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000047#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000048#include <algorithm>
Chandler Carruthf010c462012-12-04 10:44:52 +000049#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000050using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000051
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000052// The OptimizationList is automatically populated with registered Passes by the
53// PassNameParser.
54//
Chris Lattner7f500f72006-08-27 22:07:01 +000055static cl::list<const PassInfo*, bool, PassNameParser>
56PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000057
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000058// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000059//
Chris Lattner6c8103f2003-05-22 20:13:16 +000060static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000061InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000062 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000063
Chris Lattner6c8103f2003-05-22 20:13:16 +000064static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000065OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000066 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000067
68static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000069Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000070
71static cl::opt<bool>
72PrintEachXForm("p", cl::desc("Print module after each transformation"));
73
74static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000075NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000076 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000077
78static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000079OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000080
81static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000082NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000083
84static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000085VerifyEach("verify-each", cl::desc("Verify after each transform"));
86
87static cl::opt<bool>
88StripDebug("strip-debug",
89 cl::desc("Strip debugger symbol info from translation unit"));
90
91static cl::opt<bool>
92DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
93
Eric Christophera887ae42009-08-21 23:29:40 +000094static cl::opt<bool>
95DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000096 cl::desc("Do not run any optimization passes"));
97
Eric Christophera887ae42009-08-21 23:29:40 +000098static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000099DisableInternalize("disable-internalize",
100 cl::desc("Do not mark all symbols as internal"));
101
Reid Spencer74ed9972007-02-02 14:46:29 +0000102static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000103StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +0000104 cl::desc("Include the standard compile time optimizations"));
105
106static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000107StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000108 cl::desc("Include the standard link time optimizations"));
109
110static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000111OptLevelO1("O1",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000112 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000113
114static cl::opt<bool>
115OptLevelO2("O2",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000116 cl::desc("Optimization level 2. Similar to clang -O2"));
117
118static cl::opt<bool>
119OptLevelOs("Os",
120 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
121
122static cl::opt<bool>
123OptLevelOz("Oz",
124 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000125
126static cl::opt<bool>
127OptLevelO3("O3",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000128 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000129
Joe Groffe652b522012-04-17 23:05:48 +0000130static cl::opt<std::string>
131TargetTriple("mtriple", cl::desc("Override target triple for module"));
132
Devang Patel2d7551c2008-09-16 22:25:14 +0000133static cl::opt<bool>
134UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000135 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000136 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000137
138static cl::opt<bool>
139DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000140 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000141
142static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000143Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000144
Reid Spencerec7eb452004-05-27 16:28:54 +0000145static cl::alias
146QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
147
Reid Spencerfd90dd52006-08-18 06:34:30 +0000148static cl::opt<bool>
149AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
150
Devang Patelf5f23002010-12-07 00:33:43 +0000151static cl::opt<bool>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000152PrintBreakpoints("print-breakpoints-for-testing",
Devang Patelf5f23002010-12-07 00:33:43 +0000153 cl::desc("Print select breakpoints location for testing"));
154
Chris Lattnerd331cb32009-10-22 00:44:10 +0000155static cl::opt<std::string>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000156DefaultDataLayout("default-data-layout",
Chris Lattnerd331cb32009-10-22 00:44:10 +0000157 cl::desc("data layout string to use if not specified by module"),
158 cl::value_desc("layout-string"), cl::init(""));
159
Reid Spencerfd90dd52006-08-18 06:34:30 +0000160// ---------- Define Printers for module and function passes ------------
161namespace {
162
Devang Patel28552da2007-06-28 23:09:25 +0000163struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
164 static char ID;
165 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000166 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000167 std::string PassName;
168
Dan Gohman4931b312010-08-18 17:42:59 +0000169 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000170 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
171 std::string PassToPrintName = PassToPrint->getPassName();
172 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
173 }
Devang Patel28552da2007-06-28 23:09:25 +0000174
Chris Lattner2decb222010-04-16 22:42:17 +0000175 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000176 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000177 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000178
Devang Patel28552da2007-06-28 23:09:25 +0000179 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000180 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
181 Function *F = (*I)->getFunction();
182 if (F)
183 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
184 F->getParent());
185 }
Devang Patel28552da2007-06-28 23:09:25 +0000186 return false;
187 }
Eric Christophera887ae42009-08-21 23:29:40 +0000188
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000189 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000190
191 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000192 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000193 AU.setPreservesAll();
194 }
195};
196
197char CallGraphSCCPassPrinter::ID = 0;
198
Reid Spencerfd90dd52006-08-18 06:34:30 +0000199struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000200 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000201 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000202 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000203 std::string PassName;
204
Dan Gohman4931b312010-08-18 17:42:59 +0000205 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000206 : ModulePass(ID), PassToPrint(PI), Out(out) {
207 std::string PassToPrintName = PassToPrint->getPassName();
208 PassName = "ModulePass Printer: " + PassToPrintName;
209 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000210
211 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000212 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000213 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000214
215 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000216 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000217 return false;
218 }
219
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000220 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000221
222 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000223 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000224 AU.setPreservesAll();
225 }
226};
227
Devang Patel19974732007-05-03 01:11:54 +0000228char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000229struct FunctionPassPrinter : public FunctionPass {
230 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000231 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000232 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000233 std::string PassName;
234
Dan Gohman4931b312010-08-18 17:42:59 +0000235 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000236 : FunctionPass(ID), PassToPrint(PI), Out(out) {
237 std::string PassToPrintName = PassToPrint->getPassName();
238 PassName = "FunctionPass Printer: " + PassToPrintName;
239 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000240
241 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000242 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000243 Out << "Printing analysis '" << PassToPrint->getPassName()
244 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000245
Reid Spencerfd90dd52006-08-18 06:34:30 +0000246 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000247 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000248 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000249 return false;
250 }
251
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000252 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000253
254 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000255 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000256 AU.setPreservesAll();
257 }
258};
259
Devang Patel19974732007-05-03 01:11:54 +0000260char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000261
262struct LoopPassPrinter : public LoopPass {
263 static char ID;
264 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000265 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000266 std::string PassName;
267
Dan Gohman4931b312010-08-18 17:42:59 +0000268 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000269 LoopPass(ID), PassToPrint(PI), Out(out) {
270 std::string PassToPrintName = PassToPrint->getPassName();
271 PassName = "LoopPass Printer: " + PassToPrintName;
272 }
273
Devang Patel56fb1642007-07-05 15:32:03 +0000274
275 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000276 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000277 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000278
Devang Patel56fb1642007-07-05 15:32:03 +0000279 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000280 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
281 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000282 return false;
283 }
Eric Christophera887ae42009-08-21 23:29:40 +0000284
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000285 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000286
287 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000288 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000289 AU.setPreservesAll();
290 }
291};
292
293char LoopPassPrinter::ID = 0;
294
Tobias Grosser65513602010-10-20 01:54:44 +0000295struct RegionPassPrinter : public RegionPass {
296 static char ID;
297 const PassInfo *PassToPrint;
298 raw_ostream &Out;
299 std::string PassName;
300
301 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
302 PassToPrint(PI), Out(out) {
303 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser58396142011-01-20 21:03:20 +0000304 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000305 }
306
307 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
308 if (!Quiet) {
309 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000310 << "region: '" << R->getNameStr() << "' in function '"
311 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser65513602010-10-20 01:54:44 +0000312 }
313 // Get and print pass...
314 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
315 R->getEntry()->getParent()->getParent());
316 return false;
317 }
318
Tobias Grosser58396142011-01-20 21:03:20 +0000319 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000320
321 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
322 AU.addRequiredID(PassToPrint->getTypeInfo());
323 AU.setPreservesAll();
324 }
325};
326
327char RegionPassPrinter::ID = 0;
328
Reid Spencerfd90dd52006-08-18 06:34:30 +0000329struct BasicBlockPassPrinter : public BasicBlockPass {
330 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000331 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000332 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000333 std::string PassName;
334
Dan Gohman4931b312010-08-18 17:42:59 +0000335 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000336 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
337 std::string PassToPrintName = PassToPrint->getPassName();
338 PassName = "BasicBlockPass Printer: " + PassToPrintName;
339 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000340
341 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000342 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000343 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
344 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000345
346 // Get and print pass...
Andrew Tricka41af7a2011-04-05 18:41:31 +0000347 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000348 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000349 return false;
350 }
351
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000352 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000353
354 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000355 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000356 AU.setPreservesAll();
357 }
358};
359
Devang Patel19974732007-05-03 01:11:54 +0000360char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000361
Devang Patel1be99802011-04-04 19:51:17 +0000362struct BreakpointPrinter : public ModulePass {
Devang Patelf5f23002010-12-07 00:33:43 +0000363 raw_ostream &Out;
364 static char ID;
365
366 BreakpointPrinter(raw_ostream &out)
Devang Patel1be99802011-04-04 19:51:17 +0000367 : ModulePass(ID), Out(out) {
Devang Patelf5f23002010-12-07 00:33:43 +0000368 }
369
Devang Patel1be99802011-04-04 19:51:17 +0000370 void getContextName(DIDescriptor Context, std::string &N) {
371 if (Context.isNameSpace()) {
372 DINameSpace NS(Context);
373 if (!NS.getName().empty()) {
374 getContextName(NS.getContext(), N);
375 N = N + NS.getName().str() + "::";
Devang Patel97f6d5b2011-01-31 21:36:24 +0000376 }
Devang Patel1be99802011-04-04 19:51:17 +0000377 } else if (Context.isType()) {
378 DIType TY(Context);
379 if (!TY.getName().empty()) {
380 getContextName(TY.getContext(), N);
381 N = N + TY.getName().str() + "::";
382 }
383 }
384 }
385
386 virtual bool runOnModule(Module &M) {
387 StringSet<> Processed;
388 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
389 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
390 std::string Name;
391 DISubprogram SP(NMD->getOperand(i));
392 if (SP.Verify())
393 getContextName(SP.getContext(), Name);
394 Name = Name + SP.getDisplayName().str();
395 if (!Name.empty() && Processed.insert(Name)) {
396 Out << Name << "\n";
397 }
398 }
Devang Patelf5f23002010-12-07 00:33:43 +0000399 return false;
400 }
401
402 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
403 AU.setPreservesAll();
404 }
405};
Chris Lattner52b28892011-05-22 00:21:33 +0000406
407} // anonymous namespace
Devang Patelf5f23002010-12-07 00:33:43 +0000408
409char BreakpointPrinter::ID = 0;
410
Chris Lattner52b28892011-05-22 00:21:33 +0000411static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000412 // Add the pass to the pass manager...
413 PM.add(P);
414
415 // If we are verifying all of the intermediate steps, add the verifier...
416 if (VerifyEach) PM.add(createVerifierPass());
417}
418
Eric Christophera887ae42009-08-21 23:29:40 +0000419/// AddOptimizationPasses - This routine adds optimization passes
420/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000421/// duplicates llvm-gcc behaviour.
422///
423/// OptLevel - Optimization Level
Chris Lattner52b28892011-05-22 00:21:33 +0000424static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruth8bc34342012-05-16 08:32:49 +0000425 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sandsa3e585d2011-12-07 17:14:20 +0000426 FPM.add(createVerifierPass()); // Verify that input is correct
427
Chris Lattner52b28892011-05-22 00:21:33 +0000428 PassManagerBuilder Builder;
429 Builder.OptLevel = OptLevel;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000430 Builder.SizeLevel = SizeLevel;
Devang Patel2d7551c2008-09-16 22:25:14 +0000431
Eli Friedman74733a72010-01-18 22:38:31 +0000432 if (DisableInline) {
433 // No inlining pass
Eli Friedman42d41fd2011-06-06 22:13:27 +0000434 } else if (OptLevel > 1) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000435 unsigned Threshold = 225;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000436 if (SizeLevel == 1) // -Os
437 Threshold = 75;
438 else if (SizeLevel == 2) // -Oz
439 Threshold = 25;
Eli Friedman74733a72010-01-18 22:38:31 +0000440 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000441 Threshold = 275;
Chris Lattner52b28892011-05-22 00:21:33 +0000442 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedman74733a72010-01-18 22:38:31 +0000443 } else {
Chris Lattner52b28892011-05-22 00:21:33 +0000444 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedman74733a72010-01-18 22:38:31 +0000445 }
Chris Lattner52b28892011-05-22 00:21:33 +0000446 Builder.DisableUnitAtATime = !UnitAtATime;
447 Builder.DisableUnrollLoops = OptLevel == 0;
448 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
449
450 Builder.populateFunctionPassManager(FPM);
451 Builder.populateModulePassManager(MPM);
Devang Patel2d7551c2008-09-16 22:25:14 +0000452}
453
Chris Lattner52b28892011-05-22 00:21:33 +0000454static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000455 PM.add(createVerifierPass()); // Verify that input is correct
456
Reid Spencer74ed9972007-02-02 14:46:29 +0000457 // If the -strip-debug command line option was specified, do it.
458 if (StripDebug)
459 addPass(PM, createStripSymbolsPass(true));
460
461 if (DisableOptimizations) return;
462
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000463 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner52b28892011-05-22 00:21:33 +0000464 PassManagerBuilder Builder;
465 if (!DisableInline)
466 Builder.Inliner = createFunctionInliningPass();
467 Builder.OptLevel = 3;
468 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
469 Builder.populateModulePassManager(PM);
Reid Spencer74ed9972007-02-02 14:46:29 +0000470}
471
Chris Lattner52b28892011-05-22 00:21:33 +0000472static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000473 PM.add(createVerifierPass()); // Verify that input is correct
474
475 // If the -strip-debug command line option was specified, do it.
476 if (StripDebug)
477 addPass(PM, createStripSymbolsPass(true));
478
479 if (DisableOptimizations) return;
480
Chris Lattner52b28892011-05-22 00:21:33 +0000481 PassManagerBuilder Builder;
482 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
483 /*RunInliner=*/ !DisableInline);
Daniel Dunbaradc82882009-07-17 18:09:39 +0000484}
485
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000486//===----------------------------------------------------------------------===//
487// CodeGen-related helper functions.
488//
489static TargetOptions GetTargetOptions() {
490 TargetOptions Options;
491 Options.LessPreciseFPMADOption = EnableFPMAD;
492 Options.NoFramePointerElim = DisableFPElim;
493 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
494 Options.AllowFPOpFusion = FuseFPOps;
495 Options.UnsafeFPMath = EnableUnsafeFPMath;
496 Options.NoInfsFPMath = EnableNoInfsFPMath;
497 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
498 Options.HonorSignDependentRoundingFPMathOption =
499 EnableHonorSignDependentRoundingFPMath;
500 Options.UseSoftFloat = GenerateSoftFloatCalls;
501 if (FloatABIForCalls != FloatABI::Default)
502 Options.FloatABIType = FloatABIForCalls;
503 Options.NoZerosInBSS = DontPlaceZerosInBSS;
504 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
505 Options.DisableTailCalls = DisableTailCalls;
506 Options.StackAlignmentOverride = OverrideStackAlignment;
507 Options.RealignStack = EnableRealignStack;
508 Options.TrapFuncName = TrapFuncName;
509 Options.PositionIndependentExecutable = EnablePIE;
510 Options.EnableSegmentedStacks = SegmentedStacks;
511 Options.UseInitArray = UseInitArray;
512 Options.SSPBufferSize = SSPBufferSize;
513 return Options;
514}
515
516CodeGenOpt::Level GetCodeGenOptLevel() {
517 if (OptLevelO1)
518 return CodeGenOpt::Less;
519 if (OptLevelO2)
520 return CodeGenOpt::Default;
521 if (OptLevelO3)
522 return CodeGenOpt::Aggressive;
523 return CodeGenOpt::None;
524}
525
526// Returns the TargetMachine instance or zero if no triple is provided.
Nadav Rotemb338d892013-01-01 08:00:32 +0000527static TargetMachine* GetTargetMachine(Triple TheTriple) {
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000528 std::string Error;
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000529 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
530 Error);
Nadav Rotemb338d892013-01-01 08:00:32 +0000531 // Some modules don't specify a triple, and this is okay.
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000532 if (!TheTarget) {
533 return 0;
534 }
535
536 // Package up features to be passed to target/subtarget
537 std::string FeaturesStr;
538 if (MAttrs.size()) {
539 SubtargetFeatures Features;
540 for (unsigned i = 0; i != MAttrs.size(); ++i)
541 Features.AddFeature(MAttrs[i]);
542 FeaturesStr = Features.getString();
543 }
544
545 return TheTarget->createTargetMachine(TheTriple.getTriple(),
546 MCPU, FeaturesStr, GetTargetOptions(),
547 RelocModel, CMModel,
548 GetCodeGenOptLevel());
549}
Chris Lattner0be41012002-02-01 04:54:11 +0000550
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000551//===----------------------------------------------------------------------===//
552// main for opt
553//
Chris Lattner00950542001-06-06 20:29:01 +0000554int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000555 sys::PrintStackTraceOnErrorSignal();
556 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000557
David Greene08fc0d32010-01-05 01:30:32 +0000558 // Enable debug stream buffering.
559 EnableDebugBuffering = true;
560
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000561 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000562 LLVMContext &Context = getGlobalContext();
Andrew Tricka41af7a2011-04-05 18:41:31 +0000563
Nadav Rotem0873bea2012-10-24 17:23:50 +0000564 InitializeAllTargets();
565 InitializeAllTargetMCs();
Nadav Rotem0873bea2012-10-24 17:23:50 +0000566
Owen Anderson081c34b2010-10-19 17:21:58 +0000567 // Initialize passes
568 PassRegistry &Registry = *PassRegistry::getPassRegistry();
569 initializeCore(Registry);
570 initializeScalarOpts(Registry);
Hal Finkelde5e5ec2012-02-01 03:51:43 +0000571 initializeVectorization(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000572 initializeIPO(Registry);
573 initializeAnalysis(Registry);
574 initializeIPA(Registry);
575 initializeTransformUtils(Registry);
576 initializeInstCombine(Registry);
577 initializeInstrumentation(Registry);
578 initializeTarget(Registry);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000579
Chris Lattner61db1a12009-10-22 00:46:41 +0000580 cl::ParseCommandLineOptions(argc, argv,
581 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000582
Tobias Grosser7593f342010-12-02 20:35:16 +0000583 if (AnalyzeOnly && NoOutput) {
584 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
585 return 1;
586 }
587
Chris Lattner61db1a12009-10-22 00:46:41 +0000588 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000589
Chris Lattner61db1a12009-10-22 00:46:41 +0000590 // Load the input module...
591 std::auto_ptr<Module> M;
592 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000593
Chris Lattner61db1a12009-10-22 00:46:41 +0000594 if (M.get() == 0) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000595 Err.print(argv[0], errs());
Chris Lattner61db1a12009-10-22 00:46:41 +0000596 return 1;
597 }
598
Joe Groffe652b522012-04-17 23:05:48 +0000599 // If we are supposed to override the target triple, do so now.
600 if (!TargetTriple.empty())
601 M->setTargetTriple(Triple::normalize(TargetTriple));
602
Chris Lattner61db1a12009-10-22 00:46:41 +0000603 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000604 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000605 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000606 if (!OutputFilename.empty())
607 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000608 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000609 } else {
610 // Default to standard output.
611 if (OutputFilename.empty())
612 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000613
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000614 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000615 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
616 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000617 if (!ErrorInfo.empty()) {
618 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000619 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000620 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000621 }
Chris Lattner76d12292002-04-18 19:55:25 +0000622
Chris Lattner61db1a12009-10-22 00:46:41 +0000623 // If the output is set to be emitted to standard out, and standard out is a
624 // console, print out a warning message and refuse to do it. We don't
625 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000626 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000627 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000628 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000629
Chris Lattner61db1a12009-10-22 00:46:41 +0000630 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000631 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000632 //
633 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000634
Chris Lattner2a66aca2011-02-18 22:13:01 +0000635 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner188a7e02011-02-18 22:34:03 +0000636 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Tricka41af7a2011-04-05 18:41:31 +0000637
Chris Lattner188a7e02011-02-18 22:34:03 +0000638 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
639 if (DisableSimplifyLibCalls)
640 TLI->disableAllFunctions();
641 Passes.add(TLI);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000642
Micah Villmow791cfc22012-10-08 16:39:34 +0000643 // Add an appropriate DataLayout instance for this module.
644 DataLayout *TD = 0;
Chris Lattner61db1a12009-10-22 00:46:41 +0000645 const std::string &ModuleDataLayout = M.get()->getDataLayout();
646 if (!ModuleDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000647 TD = new DataLayout(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000648 else if (!DefaultDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000649 TD = new DataLayout(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000650
Chris Lattner61db1a12009-10-22 00:46:41 +0000651 if (TD)
652 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000653
Nadav Rotemb338d892013-01-01 08:00:32 +0000654 Triple ModuleTriple(M->getTargetTriple());
655 TargetMachine *Machine = 0;
656 if (ModuleTriple.getArch())
657 Machine = GetTargetMachine(Triple(ModuleTriple));
658 std::auto_ptr<TargetMachine> TM(Machine);
659
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000660 if (TM.get()) {
661 Passes.add(new TargetTransformInfo(TM->getScalarTargetTransformInfo(),
662 TM->getVectorTargetTransformInfo()));
663 }
664
Chris Lattner52b28892011-05-22 00:21:33 +0000665 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000666 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner52b28892011-05-22 00:21:33 +0000667 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnerd331cb32009-10-22 00:44:10 +0000668 if (TD)
Micah Villmow791cfc22012-10-08 16:39:34 +0000669 FPasses->add(new DataLayout(*TD));
Chris Lattner61db1a12009-10-22 00:46:41 +0000670 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000671
Devang Patelf5f23002010-12-07 00:33:43 +0000672 if (PrintBreakpoints) {
673 // Default to standard output.
674 if (!Out) {
675 if (OutputFilename.empty())
676 OutputFilename = "-";
Andrew Tricka41af7a2011-04-05 18:41:31 +0000677
Devang Patelf5f23002010-12-07 00:33:43 +0000678 std::string ErrorInfo;
679 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
680 raw_fd_ostream::F_Binary));
681 if (!ErrorInfo.empty()) {
682 errs() << ErrorInfo << '\n';
683 return 1;
684 }
685 }
686 Passes.add(new BreakpointPrinter(Out->os()));
687 NoOutput = true;
688 }
689
Chris Lattner61db1a12009-10-22 00:46:41 +0000690 // If the -strip-debug command line option was specified, add it. If
691 // -std-compile-opts was also specified, it will handle StripDebug.
692 if (StripDebug && !StandardCompileOpts)
693 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000694
Chris Lattner61db1a12009-10-22 00:46:41 +0000695 // Create a new optimization pass for each one specified on the command line
696 for (unsigned i = 0; i < PassList.size(); ++i) {
697 // Check to see if -std-compile-opts was specified before this option. If
698 // so, handle it.
699 if (StandardCompileOpts &&
700 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000701 AddStandardCompilePasses(Passes);
702 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000703 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000704
Chris Lattner61db1a12009-10-22 00:46:41 +0000705 if (StandardLinkOpts &&
706 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000707 AddStandardLinkPasses(Passes);
708 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000709 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000710
Chris Lattner61db1a12009-10-22 00:46:41 +0000711 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000712 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000713 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000714 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000715
Chris Lattner61db1a12009-10-22 00:46:41 +0000716 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000717 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000718 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000719 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000720
Chandler Carruth8bc34342012-05-16 08:32:49 +0000721 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
722 AddOptimizationPasses(Passes, *FPasses, 2, 1);
723 OptLevelOs = false;
724 }
725
726 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
727 AddOptimizationPasses(Passes, *FPasses, 2, 2);
728 OptLevelOz = false;
729 }
730
Chris Lattner61db1a12009-10-22 00:46:41 +0000731 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000732 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000733 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000734 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000735
Chris Lattner61db1a12009-10-22 00:46:41 +0000736 const PassInfo *PassInf = PassList[i];
737 Pass *P = 0;
738 if (PassInf->getNormalCtor())
739 P = PassInf->getNormalCtor()();
740 else
741 errs() << argv[0] << ": cannot create pass: "
742 << PassInf->getPassName() << "\n";
743 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000744 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000745 addPass(Passes, P);
746
747 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000748 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000749 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000750 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000751 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000752 case PT_Region:
753 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
754 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000755 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000756 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000757 break;
758 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000759 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000760 break;
761 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000762 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000763 break;
764 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000765 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000766 break;
767 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000768 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000769 }
770
Chris Lattner61db1a12009-10-22 00:46:41 +0000771 if (PrintEachXForm)
772 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000773 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000774
775 // If -std-compile-opts was specified at the end of the pass list, add them.
776 if (StandardCompileOpts) {
777 AddStandardCompilePasses(Passes);
778 StandardCompileOpts = false;
779 }
780
781 if (StandardLinkOpts) {
782 AddStandardLinkPasses(Passes);
783 StandardLinkOpts = false;
784 }
785
786 if (OptLevelO1)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000787 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000788
789 if (OptLevelO2)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000790 AddOptimizationPasses(Passes, *FPasses, 2, 0);
791
792 if (OptLevelOs)
793 AddOptimizationPasses(Passes, *FPasses, 2, 1);
794
795 if (OptLevelOz)
796 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000797
798 if (OptLevelO3)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000799 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000800
Chandler Carruth8bc34342012-05-16 08:32:49 +0000801 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3e8984a2011-05-22 06:44:19 +0000802 FPasses->doInitialization();
Chris Lattner52b28892011-05-22 00:21:33 +0000803 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
804 FPasses->run(*F);
Chris Lattner3e8984a2011-05-22 06:44:19 +0000805 FPasses->doFinalization();
806 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000807
808 // Check that the module is well formed on completion of optimization
809 if (!NoVerify && !VerifyEach)
810 Passes.add(createVerifierPass());
811
Dan Gohman4931b312010-08-18 17:42:59 +0000812 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000813 if (!NoOutput && !AnalyzeOnly) {
814 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000815 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000816 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000817 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000818 }
819
Andrew Trickce969022011-04-05 18:54:36 +0000820 // Before executing passes, print the final values of the LLVM options.
821 cl::PrintOptionValues();
822
Chris Lattner61db1a12009-10-22 00:46:41 +0000823 // Now that we have all of the passes ready, run them.
824 Passes.run(*M.get());
825
Dan Gohmand5826a32010-08-20 01:07:01 +0000826 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000827 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000828 Out->keep();
829
Chris Lattner61db1a12009-10-22 00:46:41 +0000830 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000831}