blob: bb8d143e813c4d8fc20752f6cdc7487ce2358de0 [file] [log] [blame]
Chris Lattner46e18c72004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner2f7c9632001-06-06 20:29:01 +000010// Optimizations may be specified an arbitrary number of times on the command
Reid Spencer378f7d52006-08-18 06:34:30 +000011// line, They are run in the order specified.
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//
Chris Lattner6fc7ff42001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000014
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/LLVMContext.h"
Devang Patel0abc4632011-04-04 19:51:17 +000016#include "llvm/ADT/StringSet.h"
Chris Lattner15c8b5e2011-02-18 22:13:01 +000017#include "llvm/ADT/Triple.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000019#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/Analysis/LoopPass.h"
21#include "llvm/Analysis/RegionPass.h"
22#include "llvm/Analysis/Verifier.h"
23#include "llvm/Assembly/PrintModulePass.h"
24#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/CodeGen/CommandFlags.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000029#include "llvm/IRReader/IRReader.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000030#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000031#include "llvm/LinkAllPasses.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000032#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000033#include "llvm/PassManager.h"
David Greeneed8a1de2010-01-05 01:30:32 +000034#include "llvm/Support/Debug.h"
Chris Lattner76d46322006-12-06 01:18:01 +000035#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000036#include "llvm/Support/PassNameParser.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000037#include "llvm/Support/PluginLoader.h"
Chris Lattner4b2a6e22009-12-09 00:41:28 +000038#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000040#include "llvm/Support/SourceMgr.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000041#include "llvm/Support/SystemUtils.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000042#include "llvm/Support/TargetRegistry.h"
Nadav Rotemac9a3442012-10-24 17:23:50 +000043#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000044#include "llvm/Support/ToolOutputFile.h"
45#include "llvm/Target/TargetLibraryInfo.h"
46#include "llvm/Target/TargetMachine.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000047#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner5a48a242002-07-23 18:12:22 +000048#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000049#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000050using namespace llvm;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000051
Chris Lattner5a48a242002-07-23 18:12:22 +000052// The OptimizationList is automatically populated with registered Passes by the
53// PassNameParser.
54//
Chris Lattner8e5e13b2006-08-27 22:07:01 +000055static cl::list<const PassInfo*, bool, PassNameParser>
56PassList(cl::desc("Optimizations available:"));
Chris Lattner5a48a242002-07-23 18:12:22 +000057
Chris Lattner5a48a242002-07-23 18:12:22 +000058// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000059//
Chris Lattner02a16832003-05-22 20:13:16 +000060static cl::opt<std::string>
Eric Christophere64061f2009-08-21 23:29:40 +000061InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencer378f7d52006-08-18 06:34:30 +000062 cl::init("-"), cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000063
Chris Lattner02a16832003-05-22 20:13:16 +000064static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000065OutputFilename("o", cl::desc("Override output filename"),
Dan Gohmanb01aed12010-08-18 17:40:10 +000066 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000067
68static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000069Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-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 Lattner30f40d92003-02-26 20:00:41 +000075NoOutput("disable-output",
Gabor Greife16561c2007-07-05 17:07:56 +000076 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000077
78static cl::opt<bool>
Duncan Sands6f2ffce2009-10-14 20:01:39 +000079OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar6b3153b2009-09-05 11:34:53 +000080
81static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000082NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +000083
84static cl::opt<bool>
Reid Spencer22dbfb62007-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 Christophere64061f2009-08-21 23:29:40 +000094static cl::opt<bool>
95DisableOptimizations("disable-opt",
Reid Spencer22dbfb62007-02-02 14:46:29 +000096 cl::desc("Do not run any optimization passes"));
97
Eric Christophere64061f2009-08-21 23:29:40 +000098static cl::opt<bool>
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +000099DisableInternalize("disable-internalize",
100 cl::desc("Do not mark all symbols as internal"));
101
Reid Spencer22dbfb62007-02-02 14:46:29 +0000102static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000103StandardCompileOpts("std-compile-opts",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000104 cl::desc("Include the standard compile time optimizations"));
105
106static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000107StandardLinkOpts("std-link-opts",
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000108 cl::desc("Include the standard link time optimizations"));
109
110static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000111OptLevelO1("O1",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000112 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000113
114static cl::opt<bool>
115OptLevelO2("O2",
Chandler Carruthd8c08c22012-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 Patel9966ccf2008-09-16 22:25:14 +0000125
126static cl::opt<bool>
127OptLevelO3("O3",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000128 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000129
Joe Groff1b738692012-04-17 23:05:48 +0000130static cl::opt<std::string>
131TargetTriple("mtriple", cl::desc("Override target triple for module"));
132
Devang Patel9966ccf2008-09-16 22:25:14 +0000133static cl::opt<bool>
134UnitAtATime("funit-at-a-time",
Eric Christopherc8f625b2009-08-21 23:30:30 +0000135 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000136 cl::init(true));
Devang Patel9966ccf2008-09-16 22:25:14 +0000137
138static cl::opt<bool>
139DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000140 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000141
142static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000143Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000144
Reid Spencerc8878ed2004-05-27 16:28:54 +0000145static cl::alias
146QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
147
Reid Spencer378f7d52006-08-18 06:34:30 +0000148static cl::opt<bool>
149AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
150
Devang Patel786a05e2010-12-07 00:33:43 +0000151static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000152PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000153 cl::desc("Print select breakpoints location for testing"));
154
Chris Lattnercfa19112009-10-22 00:44:10 +0000155static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000156DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-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 Spencer378f7d52006-08-18 06:34:30 +0000160// ---------- Define Printers for module and function passes ------------
161namespace {
162
Devang Patel9a259a22007-06-28 23:09:25 +0000163struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
164 static char ID;
165 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000166 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000167 std::string PassName;
168
Dan Gohman083330a2010-08-18 17:42:59 +0000169 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grossere5fcde72010-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 Patel9a259a22007-06-28 23:09:25 +0000174
Chris Lattner4422d312010-04-16 22:42:17 +0000175 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000176 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000177 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel9a259a22007-06-28 23:09:25 +0000178
Devang Patel9a259a22007-06-28 23:09:25 +0000179 // Get and print pass...
Tobias Grosser011d79a2010-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 Patel9a259a22007-06-28 23:09:25 +0000186 return false;
187 }
Eric Christophere64061f2009-08-21 23:29:40 +0000188
Tobias Grossere5fcde72010-09-08 15:02:47 +0000189 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel9a259a22007-06-28 23:09:25 +0000190
191 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000192 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel9a259a22007-06-28 23:09:25 +0000193 AU.setPreservesAll();
194 }
195};
196
197char CallGraphSCCPassPrinter::ID = 0;
198
Reid Spencer378f7d52006-08-18 06:34:30 +0000199struct ModulePassPrinter : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +0000200 static char ID;
Reid Spencer378f7d52006-08-18 06:34:30 +0000201 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000202 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000203 std::string PassName;
204
Dan Gohman083330a2010-08-18 17:42:59 +0000205 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-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 Spencer378f7d52006-08-18 06:34:30 +0000210
211 virtual bool runOnModule(Module &M) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000212 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000213 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencer378f7d52006-08-18 06:34:30 +0000214
215 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000216 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencer378f7d52006-08-18 06:34:30 +0000217 return false;
218 }
219
Tobias Grossere5fcde72010-09-08 15:02:47 +0000220 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000221
222 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000223 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000224 AU.setPreservesAll();
225 }
226};
227
Devang Patel8c78a0b2007-05-03 01:11:54 +0000228char ModulePassPrinter::ID = 0;
Reid Spencer378f7d52006-08-18 06:34:30 +0000229struct FunctionPassPrinter : public FunctionPass {
230 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000231 raw_ostream &Out;
Devang Patel8c78a0b2007-05-03 01:11:54 +0000232 static char ID;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000233 std::string PassName;
234
Dan Gohman083330a2010-08-18 17:42:59 +0000235 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-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 Spencer378f7d52006-08-18 06:34:30 +0000240
241 virtual bool runOnFunction(Function &F) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000242 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000243 Out << "Printing analysis '" << PassToPrint->getPassName()
244 << "' for function '" << F.getName() << "':\n";
Tobias Grosser011d79a2010-09-08 15:02:51 +0000245
Reid Spencer378f7d52006-08-18 06:34:30 +0000246 // Get and print pass...
Dan Gohman083330a2010-08-18 17:42:59 +0000247 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Andersona7aed182010-08-06 18:33:48 +0000248 F.getParent());
Reid Spencer378f7d52006-08-18 06:34:30 +0000249 return false;
250 }
251
Tobias Grossere5fcde72010-09-08 15:02:47 +0000252 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000253
254 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000255 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000256 AU.setPreservesAll();
257 }
258};
259
Devang Patel8c78a0b2007-05-03 01:11:54 +0000260char FunctionPassPrinter::ID = 0;
Devang Patelf5a15dd2007-07-05 15:32:03 +0000261
262struct LoopPassPrinter : public LoopPass {
263 static char ID;
264 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000265 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000266 std::string PassName;
267
Dan Gohman083330a2010-08-18 17:42:59 +0000268 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grossere5fcde72010-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 Patelf5a15dd2007-07-05 15:32:03 +0000274
275 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000276 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000277 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grosser011d79a2010-09-08 15:02:51 +0000278
Devang Patelf5a15dd2007-07-05 15:32:03 +0000279 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000280 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
281 L->getHeader()->getParent()->getParent());
Devang Patelf5a15dd2007-07-05 15:32:03 +0000282 return false;
283 }
Eric Christophere64061f2009-08-21 23:29:40 +0000284
Tobias Grossere5fcde72010-09-08 15:02:47 +0000285 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patelf5a15dd2007-07-05 15:32:03 +0000286
287 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000288 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patelf5a15dd2007-07-05 15:32:03 +0000289 AU.setPreservesAll();
290 }
291};
292
293char LoopPassPrinter::ID = 0;
294
Tobias Grosser23c83412010-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 Grosser7dbb4822011-01-20 21:03:20 +0000304 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser23c83412010-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 Kramer1f97a5a2011-11-15 16:27:03 +0000310 << "region: '" << R->getNameStr() << "' in function '"
311 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser23c83412010-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 Grosser7dbb4822011-01-20 21:03:20 +0000319 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser23c83412010-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 Spencer378f7d52006-08-18 06:34:30 +0000329struct BasicBlockPassPrinter : public BasicBlockPass {
330 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000331 raw_ostream &Out;
Devang Patel8c78a0b2007-05-03 01:11:54 +0000332 static char ID;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000333 std::string PassName;
334
Dan Gohman083330a2010-08-18 17:42:59 +0000335 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-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 Spencer378f7d52006-08-18 06:34:30 +0000340
341 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000342 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000343 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
344 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencer378f7d52006-08-18 06:34:30 +0000345
346 // Get and print pass...
Andrew Trickb826ae82011-04-05 18:41:31 +0000347 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Andersona7aed182010-08-06 18:33:48 +0000348 BB.getParent()->getParent());
Reid Spencer378f7d52006-08-18 06:34:30 +0000349 return false;
350 }
351
Tobias Grossere5fcde72010-09-08 15:02:47 +0000352 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000353
354 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000355 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000356 AU.setPreservesAll();
357 }
358};
359
Devang Patel8c78a0b2007-05-03 01:11:54 +0000360char BasicBlockPassPrinter::ID = 0;
Devang Patel786a05e2010-12-07 00:33:43 +0000361
Devang Patel0abc4632011-04-04 19:51:17 +0000362struct BreakpointPrinter : public ModulePass {
Devang Patel786a05e2010-12-07 00:33:43 +0000363 raw_ostream &Out;
364 static char ID;
365
366 BreakpointPrinter(raw_ostream &out)
Devang Patel0abc4632011-04-04 19:51:17 +0000367 : ModulePass(ID), Out(out) {
Devang Patel786a05e2010-12-07 00:33:43 +0000368 }
369
Devang Patel0abc4632011-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 Patelb07ec832011-01-31 21:36:24 +0000376 }
Devang Patel0abc4632011-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));
Manman Ren983a16c2013-06-28 05:43:10 +0000392 assert((!SP || SP.isSubprogram()) &&
393 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
394 if (!SP)
395 continue;
396 getContextName(SP.getContext(), Name);
Devang Patel0abc4632011-04-04 19:51:17 +0000397 Name = Name + SP.getDisplayName().str();
398 if (!Name.empty() && Processed.insert(Name)) {
399 Out << Name << "\n";
400 }
401 }
Devang Patel786a05e2010-12-07 00:33:43 +0000402 return false;
403 }
404
405 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
406 AU.setPreservesAll();
407 }
408};
Eric Christopher13637e92013-04-15 07:31:37 +0000409
Chris Lattner3d70add2011-05-22 00:21:33 +0000410} // anonymous namespace
Devang Patel786a05e2010-12-07 00:33:43 +0000411
412char BreakpointPrinter::ID = 0;
413
Chris Lattner3d70add2011-05-22 00:21:33 +0000414static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000415 // Add the pass to the pass manager...
416 PM.add(P);
417
418 // If we are verifying all of the intermediate steps, add the verifier...
419 if (VerifyEach) PM.add(createVerifierPass());
420}
421
Eric Christophere64061f2009-08-21 23:29:40 +0000422/// AddOptimizationPasses - This routine adds optimization passes
423/// based on selected optimization level, OptLevel. This routine
Devang Patel9966ccf2008-09-16 22:25:14 +0000424/// duplicates llvm-gcc behaviour.
425///
426/// OptLevel - Optimization Level
Chris Lattner3d70add2011-05-22 00:21:33 +0000427static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000428 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sands0f19e912011-12-07 17:14:20 +0000429 FPM.add(createVerifierPass()); // Verify that input is correct
430
Chris Lattner3d70add2011-05-22 00:21:33 +0000431 PassManagerBuilder Builder;
432 Builder.OptLevel = OptLevel;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000433 Builder.SizeLevel = SizeLevel;
Devang Patel9966ccf2008-09-16 22:25:14 +0000434
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000435 if (DisableInline) {
436 // No inlining pass
Eli Friedmanb68fe252011-06-06 22:13:27 +0000437 } else if (OptLevel > 1) {
Jakob Stoklund Olesen1d73c0e2010-11-02 23:40:28 +0000438 unsigned Threshold = 225;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000439 if (SizeLevel == 1) // -Os
440 Threshold = 75;
441 else if (SizeLevel == 2) // -Oz
442 Threshold = 25;
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000443 if (OptLevel > 2)
Jakob Stoklund Olesen1d73c0e2010-11-02 23:40:28 +0000444 Threshold = 275;
Chris Lattner3d70add2011-05-22 00:21:33 +0000445 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000446 } else {
Chris Lattner3d70add2011-05-22 00:21:33 +0000447 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000448 }
Chris Lattner3d70add2011-05-22 00:21:33 +0000449 Builder.DisableUnitAtATime = !UnitAtATime;
450 Builder.DisableUnrollLoops = OptLevel == 0;
Eric Christopher13637e92013-04-15 07:31:37 +0000451
Chris Lattner3d70add2011-05-22 00:21:33 +0000452 Builder.populateFunctionPassManager(FPM);
453 Builder.populateModulePassManager(MPM);
Devang Patel9966ccf2008-09-16 22:25:14 +0000454}
455
Chris Lattner3d70add2011-05-22 00:21:33 +0000456static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000457 PM.add(createVerifierPass()); // Verify that input is correct
458
Reid Spencer22dbfb62007-02-02 14:46:29 +0000459 // If the -strip-debug command line option was specified, do it.
460 if (StripDebug)
461 addPass(PM, createStripSymbolsPass(true));
462
463 if (DisableOptimizations) return;
464
Daniel Dunbar94c7b792009-06-03 18:22:15 +0000465 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner3d70add2011-05-22 00:21:33 +0000466 PassManagerBuilder Builder;
467 if (!DisableInline)
468 Builder.Inliner = createFunctionInliningPass();
469 Builder.OptLevel = 3;
Chris Lattner3d70add2011-05-22 00:21:33 +0000470 Builder.populateModulePassManager(PM);
Reid Spencer22dbfb62007-02-02 14:46:29 +0000471}
472
Chris Lattner3d70add2011-05-22 00:21:33 +0000473static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000474 PM.add(createVerifierPass()); // Verify that input is correct
475
476 // If the -strip-debug command line option was specified, do it.
477 if (StripDebug)
478 addPass(PM, createStripSymbolsPass(true));
479
480 if (DisableOptimizations) return;
481
Chris Lattner3d70add2011-05-22 00:21:33 +0000482 PassManagerBuilder Builder;
483 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
484 /*RunInliner=*/ !DisableInline);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000485}
486
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000487//===----------------------------------------------------------------------===//
488// CodeGen-related helper functions.
489//
490static TargetOptions GetTargetOptions() {
491 TargetOptions Options;
492 Options.LessPreciseFPMADOption = EnableFPMAD;
493 Options.NoFramePointerElim = DisableFPElim;
494 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
495 Options.AllowFPOpFusion = FuseFPOps;
496 Options.UnsafeFPMath = EnableUnsafeFPMath;
497 Options.NoInfsFPMath = EnableNoInfsFPMath;
498 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
499 Options.HonorSignDependentRoundingFPMathOption =
500 EnableHonorSignDependentRoundingFPMath;
501 Options.UseSoftFloat = GenerateSoftFloatCalls;
502 if (FloatABIForCalls != FloatABI::Default)
503 Options.FloatABIType = FloatABIForCalls;
504 Options.NoZerosInBSS = DontPlaceZerosInBSS;
505 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
506 Options.DisableTailCalls = DisableTailCalls;
507 Options.StackAlignmentOverride = OverrideStackAlignment;
508 Options.RealignStack = EnableRealignStack;
509 Options.TrapFuncName = TrapFuncName;
510 Options.PositionIndependentExecutable = EnablePIE;
511 Options.EnableSegmentedStacks = SegmentedStacks;
512 Options.UseInitArray = UseInitArray;
Chandler Carruth86e60a32013-07-13 01:00:17 +0000513 Options.SSPBufferSize = SSPBufferSize;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000514 return Options;
515}
516
517CodeGenOpt::Level GetCodeGenOptLevel() {
518 if (OptLevelO1)
519 return CodeGenOpt::Less;
520 if (OptLevelO2)
521 return CodeGenOpt::Default;
522 if (OptLevelO3)
523 return CodeGenOpt::Aggressive;
524 return CodeGenOpt::None;
525}
526
527// Returns the TargetMachine instance or zero if no triple is provided.
Nadav Rotemb1615b12013-01-01 08:00:32 +0000528static TargetMachine* GetTargetMachine(Triple TheTriple) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000529 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000530 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
531 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000532 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000533 if (!TheTarget) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000534 return 0;
Eric Christopher13637e92013-04-15 07:31:37 +0000535 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000536
537 // Package up features to be passed to target/subtarget
538 std::string FeaturesStr;
539 if (MAttrs.size()) {
540 SubtargetFeatures Features;
541 for (unsigned i = 0; i != MAttrs.size(); ++i)
542 Features.AddFeature(MAttrs[i]);
543 FeaturesStr = Features.getString();
544 }
545
546 return TheTarget->createTargetMachine(TheTriple.getTriple(),
547 MCPU, FeaturesStr, GetTargetOptions(),
548 RelocModel, CMModel,
549 GetCodeGenOptLevel());
550}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000551
Chris Lattner5a48a242002-07-23 18:12:22 +0000552//===----------------------------------------------------------------------===//
553// main for opt
554//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000555int main(int argc, char **argv) {
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000556 sys::PrintStackTraceOnErrorSignal();
557 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000558
David Greeneed8a1de2010-01-05 01:30:32 +0000559 // Enable debug stream buffering.
560 EnableDebugBuffering = true;
561
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000562 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson19251ec2009-07-15 22:16:10 +0000563 LLVMContext &Context = getGlobalContext();
Andrew Trickb826ae82011-04-05 18:41:31 +0000564
Nadav Rotemac9a3442012-10-24 17:23:50 +0000565 InitializeAllTargets();
566 InitializeAllTargetMCs();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000567
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000568 // Initialize passes
569 PassRegistry &Registry = *PassRegistry::getPassRegistry();
570 initializeCore(Registry);
Daniel Malea3c5bed12013-05-08 20:44:14 +0000571 initializeDebugIRPass(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000572 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000573 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000574 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000575 initializeIPO(Registry);
576 initializeAnalysis(Registry);
577 initializeIPA(Registry);
578 initializeTransformUtils(Registry);
579 initializeInstCombine(Registry);
580 initializeInstrumentation(Registry);
581 initializeTarget(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000582
Chris Lattnercc815262009-10-22 00:46:41 +0000583 cl::ParseCommandLineOptions(argc, argv,
584 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000585
Tobias Grosser083379f2010-12-02 20:35:16 +0000586 if (AnalyzeOnly && NoOutput) {
587 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
588 return 1;
589 }
590
Chris Lattnercc815262009-10-22 00:46:41 +0000591 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000592
Chris Lattnercc815262009-10-22 00:46:41 +0000593 // Load the input module...
Andy Gibbs95777552013-04-12 10:56:28 +0000594 OwningPtr<Module> M;
Chris Lattnercc815262009-10-22 00:46:41 +0000595 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophere64061f2009-08-21 23:29:40 +0000596
Chris Lattnercc815262009-10-22 00:46:41 +0000597 if (M.get() == 0) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000598 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000599 return 1;
600 }
601
Joe Groff1b738692012-04-17 23:05:48 +0000602 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000603 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000604 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000605
Chris Lattnercc815262009-10-22 00:46:41 +0000606 // Figure out what stream we are supposed to write to...
Dan Gohman268b0f42010-08-20 01:07:01 +0000607 OwningPtr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000608 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000609 if (!OutputFilename.empty())
610 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000611 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000612 } else {
613 // Default to standard output.
614 if (OutputFilename.empty())
615 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000616
Dan Gohmanb01aed12010-08-18 17:40:10 +0000617 std::string ErrorInfo;
Dan Gohman268b0f42010-08-20 01:07:01 +0000618 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola6d354812013-07-16 19:44:17 +0000619 sys::fs::F_Binary));
Dan Gohmanb01aed12010-08-18 17:40:10 +0000620 if (!ErrorInfo.empty()) {
621 errs() << ErrorInfo << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000622 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000623 }
Chris Lattnercc815262009-10-22 00:46:41 +0000624 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000625
Chris Lattnercc815262009-10-22 00:46:41 +0000626 // If the output is set to be emitted to standard out, and standard out is a
627 // console, print out a warning message and refuse to do it. We don't
628 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000629 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000630 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000631 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000632
Chris Lattnercc815262009-10-22 00:46:41 +0000633 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000634 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000635 //
636 PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000637
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000638 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner1341df92011-02-18 22:34:03 +0000639 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Trickb826ae82011-04-05 18:41:31 +0000640
Chris Lattner1341df92011-02-18 22:34:03 +0000641 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
642 if (DisableSimplifyLibCalls)
643 TLI->disableAllFunctions();
644 Passes.add(TLI);
Andrew Trickb826ae82011-04-05 18:41:31 +0000645
Eric Christopher13637e92013-04-15 07:31:37 +0000646 // Add an appropriate DataLayout instance for this module.
647 DataLayout *TD = 0;
648 const std::string &ModuleDataLayout = M.get()->getDataLayout();
649 if (!ModuleDataLayout.empty())
650 TD = new DataLayout(ModuleDataLayout);
651 else if (!DefaultDataLayout.empty())
652 TD = new DataLayout(DefaultDataLayout);
653
Chris Lattnercc815262009-10-22 00:46:41 +0000654 if (TD)
Eric Christopher13637e92013-04-15 07:31:37 +0000655 Passes.add(TD);
Reid Spencer996ec722004-12-30 05:36:08 +0000656
Nadav Rotemb1615b12013-01-01 08:00:32 +0000657 Triple ModuleTriple(M->getTargetTriple());
658 TargetMachine *Machine = 0;
659 if (ModuleTriple.getArch())
660 Machine = GetTargetMachine(Triple(ModuleTriple));
Andy Gibbs95777552013-04-12 10:56:28 +0000661 OwningPtr<TargetMachine> TM(Machine);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000662
Chandler Carruth664e3542013-01-07 01:37:14 +0000663 // Add internal analysis passes from the target machine.
664 if (TM.get())
665 TM->addAnalysisPasses(Passes);
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000666
Chris Lattner3d70add2011-05-22 00:21:33 +0000667 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000668 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000669 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnercfa19112009-10-22 00:44:10 +0000670 if (TD)
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000671 FPasses->add(new DataLayout(*TD));
Chris Lattnercc815262009-10-22 00:46:41 +0000672 }
Reid Spencer996ec722004-12-30 05:36:08 +0000673
Devang Patel786a05e2010-12-07 00:33:43 +0000674 if (PrintBreakpoints) {
675 // Default to standard output.
676 if (!Out) {
677 if (OutputFilename.empty())
678 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000679
Devang Patel786a05e2010-12-07 00:33:43 +0000680 std::string ErrorInfo;
681 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola6d354812013-07-16 19:44:17 +0000682 sys::fs::F_Binary));
Devang Patel786a05e2010-12-07 00:33:43 +0000683 if (!ErrorInfo.empty()) {
684 errs() << ErrorInfo << '\n';
685 return 1;
686 }
687 }
688 Passes.add(new BreakpointPrinter(Out->os()));
689 NoOutput = true;
690 }
691
Chris Lattnercc815262009-10-22 00:46:41 +0000692 // If the -strip-debug command line option was specified, add it. If
693 // -std-compile-opts was also specified, it will handle StripDebug.
694 if (StripDebug && !StandardCompileOpts)
695 addPass(Passes, createStripSymbolsPass(true));
Eric Christophere64061f2009-08-21 23:29:40 +0000696
Chris Lattnercc815262009-10-22 00:46:41 +0000697 // Create a new optimization pass for each one specified on the command line
698 for (unsigned i = 0; i < PassList.size(); ++i) {
699 // Check to see if -std-compile-opts was specified before this option. If
700 // so, handle it.
701 if (StandardCompileOpts &&
702 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner501c3b62008-07-13 19:35:21 +0000703 AddStandardCompilePasses(Passes);
704 StandardCompileOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000705 }
Reid Spencer996ec722004-12-30 05:36:08 +0000706
Chris Lattnercc815262009-10-22 00:46:41 +0000707 if (StandardLinkOpts &&
708 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000709 AddStandardLinkPasses(Passes);
710 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000711 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000712
Chris Lattnercc815262009-10-22 00:46:41 +0000713 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000714 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000715 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000716 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000717
Chris Lattnercc815262009-10-22 00:46:41 +0000718 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000719 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000720 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000721 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000722
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000723 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
724 AddOptimizationPasses(Passes, *FPasses, 2, 1);
725 OptLevelOs = false;
726 }
727
728 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
729 AddOptimizationPasses(Passes, *FPasses, 2, 2);
730 OptLevelOz = false;
731 }
732
Chris Lattnercc815262009-10-22 00:46:41 +0000733 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000734 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000735 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000736 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000737
Chris Lattnercc815262009-10-22 00:46:41 +0000738 const PassInfo *PassInf = PassList[i];
739 Pass *P = 0;
740 if (PassInf->getNormalCtor())
741 P = PassInf->getNormalCtor()();
742 else
743 errs() << argv[0] << ": cannot create pass: "
744 << PassInf->getPassName() << "\n";
745 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000746 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000747 addPass(Passes, P);
748
749 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000750 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000751 case PT_BasicBlock:
Dan Gohmana2233f22010-09-01 14:20:41 +0000752 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000753 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000754 case PT_Region:
755 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
756 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000757 case PT_Loop:
Dan Gohmana2233f22010-09-01 14:20:41 +0000758 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000759 break;
760 case PT_Function:
Dan Gohmana2233f22010-09-01 14:20:41 +0000761 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000762 break;
763 case PT_CallGraphSCC:
Dan Gohmana2233f22010-09-01 14:20:41 +0000764 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000765 break;
766 default:
Dan Gohmana2233f22010-09-01 14:20:41 +0000767 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000768 break;
769 }
Chris Lattnercc815262009-10-22 00:46:41 +0000770 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000771 }
772
Chris Lattnercc815262009-10-22 00:46:41 +0000773 if (PrintEachXForm)
774 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000775 }
Chris Lattnercc815262009-10-22 00:46:41 +0000776
777 // If -std-compile-opts was specified at the end of the pass list, add them.
778 if (StandardCompileOpts) {
779 AddStandardCompilePasses(Passes);
780 StandardCompileOpts = false;
781 }
782
783 if (StandardLinkOpts) {
784 AddStandardLinkPasses(Passes);
785 StandardLinkOpts = false;
786 }
787
788 if (OptLevelO1)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000789 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000790
791 if (OptLevelO2)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000792 AddOptimizationPasses(Passes, *FPasses, 2, 0);
793
794 if (OptLevelOs)
795 AddOptimizationPasses(Passes, *FPasses, 2, 1);
796
797 if (OptLevelOz)
798 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000799
800 if (OptLevelO3)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000801 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000802
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000803 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000804 FPasses->doInitialization();
Chris Lattner3d70add2011-05-22 00:21:33 +0000805 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
806 FPasses->run(*F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000807 FPasses->doFinalization();
808 }
Chris Lattnercc815262009-10-22 00:46:41 +0000809
810 // Check that the module is well formed on completion of optimization
811 if (!NoVerify && !VerifyEach)
812 Passes.add(createVerifierPass());
813
Dan Gohman083330a2010-08-18 17:42:59 +0000814 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000815 if (!NoOutput && !AnalyzeOnly) {
816 if (OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000817 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000818 else
Dan Gohmana2233f22010-09-01 14:20:41 +0000819 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000820 }
821
Andrew Trick12004012011-04-05 18:54:36 +0000822 // Before executing passes, print the final values of the LLVM options.
823 cl::PrintOptionValues();
824
Chris Lattnercc815262009-10-22 00:46:41 +0000825 // Now that we have all of the passes ready, run them.
826 Passes.run(*M.get());
827
Dan Gohman268b0f42010-08-20 01:07:01 +0000828 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000829 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000830 Out->keep();
831
Chris Lattnercc815262009-10-22 00:46:41 +0000832 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000833}