blob: dba16f72dab194f13b52f9a2770f71c870e64bfe [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>
Hal Finkel6d099042013-08-28 18:33:10 +0000139DisableLoopUnrolling("disable-loop-unrolling",
140 cl::desc("Disable loop unrolling in all relevant passes"),
141 cl::init(false));
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000142static cl::opt<bool>
143DisableLoopVectorization("disable-loop-vectorization",
144 cl::desc("Disable the loop vectorization pass"),
145 cl::init(false));
146
147static cl::opt<bool>
148DisableSLPVectorization("disable-slp-vectorization",
149 cl::desc("Disable the slp vectorization pass"),
150 cl::init(false));
151
Hal Finkel6d099042013-08-28 18:33:10 +0000152
153static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000154DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000155 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000156
157static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000158Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000159
Reid Spencerc8878ed2004-05-27 16:28:54 +0000160static cl::alias
161QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
162
Reid Spencer378f7d52006-08-18 06:34:30 +0000163static cl::opt<bool>
164AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
165
Devang Patel786a05e2010-12-07 00:33:43 +0000166static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000167PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000168 cl::desc("Print select breakpoints location for testing"));
169
Chris Lattnercfa19112009-10-22 00:44:10 +0000170static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000171DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-10-22 00:44:10 +0000172 cl::desc("data layout string to use if not specified by module"),
173 cl::value_desc("layout-string"), cl::init(""));
174
Reid Spencer378f7d52006-08-18 06:34:30 +0000175// ---------- Define Printers for module and function passes ------------
176namespace {
177
Devang Patel9a259a22007-06-28 23:09:25 +0000178struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
179 static char ID;
180 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000181 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000182 std::string PassName;
183
Dan Gohman083330a2010-08-18 17:42:59 +0000184 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grossere5fcde72010-09-08 15:02:47 +0000185 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
186 std::string PassToPrintName = PassToPrint->getPassName();
187 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
188 }
Devang Patel9a259a22007-06-28 23:09:25 +0000189
Chris Lattner4422d312010-04-16 22:42:17 +0000190 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000191 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000192 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel9a259a22007-06-28 23:09:25 +0000193
Devang Patel9a259a22007-06-28 23:09:25 +0000194 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000195 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
196 Function *F = (*I)->getFunction();
197 if (F)
198 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
199 F->getParent());
200 }
Devang Patel9a259a22007-06-28 23:09:25 +0000201 return false;
202 }
Eric Christophere64061f2009-08-21 23:29:40 +0000203
Tobias Grossere5fcde72010-09-08 15:02:47 +0000204 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel9a259a22007-06-28 23:09:25 +0000205
206 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000207 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel9a259a22007-06-28 23:09:25 +0000208 AU.setPreservesAll();
209 }
210};
211
212char CallGraphSCCPassPrinter::ID = 0;
213
Reid Spencer378f7d52006-08-18 06:34:30 +0000214struct ModulePassPrinter : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +0000215 static char ID;
Reid Spencer378f7d52006-08-18 06:34:30 +0000216 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000217 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000218 std::string PassName;
219
Dan Gohman083330a2010-08-18 17:42:59 +0000220 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-09-08 15:02:47 +0000221 : ModulePass(ID), PassToPrint(PI), Out(out) {
222 std::string PassToPrintName = PassToPrint->getPassName();
223 PassName = "ModulePass Printer: " + PassToPrintName;
224 }
Reid Spencer378f7d52006-08-18 06:34:30 +0000225
226 virtual bool runOnModule(Module &M) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000227 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000228 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencer378f7d52006-08-18 06:34:30 +0000229
230 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000231 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencer378f7d52006-08-18 06:34:30 +0000232 return false;
233 }
234
Tobias Grossere5fcde72010-09-08 15:02:47 +0000235 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000236
237 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000238 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000239 AU.setPreservesAll();
240 }
241};
242
Devang Patel8c78a0b2007-05-03 01:11:54 +0000243char ModulePassPrinter::ID = 0;
Reid Spencer378f7d52006-08-18 06:34:30 +0000244struct FunctionPassPrinter : public FunctionPass {
245 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000246 raw_ostream &Out;
Devang Patel8c78a0b2007-05-03 01:11:54 +0000247 static char ID;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000248 std::string PassName;
249
Dan Gohman083330a2010-08-18 17:42:59 +0000250 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-09-08 15:02:47 +0000251 : FunctionPass(ID), PassToPrint(PI), Out(out) {
252 std::string PassToPrintName = PassToPrint->getPassName();
253 PassName = "FunctionPass Printer: " + PassToPrintName;
254 }
Reid Spencer378f7d52006-08-18 06:34:30 +0000255
256 virtual bool runOnFunction(Function &F) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000257 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000258 Out << "Printing analysis '" << PassToPrint->getPassName()
259 << "' for function '" << F.getName() << "':\n";
Tobias Grosser011d79a2010-09-08 15:02:51 +0000260
Reid Spencer378f7d52006-08-18 06:34:30 +0000261 // Get and print pass...
Dan Gohman083330a2010-08-18 17:42:59 +0000262 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Andersona7aed182010-08-06 18:33:48 +0000263 F.getParent());
Reid Spencer378f7d52006-08-18 06:34:30 +0000264 return false;
265 }
266
Tobias Grossere5fcde72010-09-08 15:02:47 +0000267 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000268
269 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000270 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000271 AU.setPreservesAll();
272 }
273};
274
Devang Patel8c78a0b2007-05-03 01:11:54 +0000275char FunctionPassPrinter::ID = 0;
Devang Patelf5a15dd2007-07-05 15:32:03 +0000276
277struct LoopPassPrinter : public LoopPass {
278 static char ID;
279 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000280 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000281 std::string PassName;
282
Dan Gohman083330a2010-08-18 17:42:59 +0000283 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grossere5fcde72010-09-08 15:02:47 +0000284 LoopPass(ID), PassToPrint(PI), Out(out) {
285 std::string PassToPrintName = PassToPrint->getPassName();
286 PassName = "LoopPass Printer: " + PassToPrintName;
287 }
288
Devang Patelf5a15dd2007-07-05 15:32:03 +0000289
290 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000291 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000292 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grosser011d79a2010-09-08 15:02:51 +0000293
Devang Patelf5a15dd2007-07-05 15:32:03 +0000294 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000295 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
296 L->getHeader()->getParent()->getParent());
Devang Patelf5a15dd2007-07-05 15:32:03 +0000297 return false;
298 }
Eric Christophere64061f2009-08-21 23:29:40 +0000299
Tobias Grossere5fcde72010-09-08 15:02:47 +0000300 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patelf5a15dd2007-07-05 15:32:03 +0000301
302 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000303 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patelf5a15dd2007-07-05 15:32:03 +0000304 AU.setPreservesAll();
305 }
306};
307
308char LoopPassPrinter::ID = 0;
309
Tobias Grosser23c83412010-10-20 01:54:44 +0000310struct RegionPassPrinter : public RegionPass {
311 static char ID;
312 const PassInfo *PassToPrint;
313 raw_ostream &Out;
314 std::string PassName;
315
316 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
317 PassToPrint(PI), Out(out) {
318 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser7dbb4822011-01-20 21:03:20 +0000319 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser23c83412010-10-20 01:54:44 +0000320 }
321
322 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
323 if (!Quiet) {
324 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000325 << "region: '" << R->getNameStr() << "' in function '"
326 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser23c83412010-10-20 01:54:44 +0000327 }
328 // Get and print pass...
329 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
330 R->getEntry()->getParent()->getParent());
331 return false;
332 }
333
Tobias Grosser7dbb4822011-01-20 21:03:20 +0000334 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser23c83412010-10-20 01:54:44 +0000335
336 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
337 AU.addRequiredID(PassToPrint->getTypeInfo());
338 AU.setPreservesAll();
339 }
340};
341
342char RegionPassPrinter::ID = 0;
343
Reid Spencer378f7d52006-08-18 06:34:30 +0000344struct BasicBlockPassPrinter : public BasicBlockPass {
345 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000346 raw_ostream &Out;
Devang Patel8c78a0b2007-05-03 01:11:54 +0000347 static char ID;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000348 std::string PassName;
349
Dan Gohman083330a2010-08-18 17:42:59 +0000350 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-09-08 15:02:47 +0000351 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
352 std::string PassToPrintName = PassToPrint->getPassName();
353 PassName = "BasicBlockPass Printer: " + PassToPrintName;
354 }
Reid Spencer378f7d52006-08-18 06:34:30 +0000355
356 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000357 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000358 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
359 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencer378f7d52006-08-18 06:34:30 +0000360
361 // Get and print pass...
Andrew Trickb826ae82011-04-05 18:41:31 +0000362 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Andersona7aed182010-08-06 18:33:48 +0000363 BB.getParent()->getParent());
Reid Spencer378f7d52006-08-18 06:34:30 +0000364 return false;
365 }
366
Tobias Grossere5fcde72010-09-08 15:02:47 +0000367 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000368
369 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000370 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000371 AU.setPreservesAll();
372 }
373};
374
Devang Patel8c78a0b2007-05-03 01:11:54 +0000375char BasicBlockPassPrinter::ID = 0;
Devang Patel786a05e2010-12-07 00:33:43 +0000376
Devang Patel0abc4632011-04-04 19:51:17 +0000377struct BreakpointPrinter : public ModulePass {
Devang Patel786a05e2010-12-07 00:33:43 +0000378 raw_ostream &Out;
379 static char ID;
Manman Ren116868e2013-09-09 19:47:11 +0000380 DITypeIdentifierMap TypeIdentifierMap;
Devang Patel786a05e2010-12-07 00:33:43 +0000381
382 BreakpointPrinter(raw_ostream &out)
Devang Patel0abc4632011-04-04 19:51:17 +0000383 : ModulePass(ID), Out(out) {
Devang Patel786a05e2010-12-07 00:33:43 +0000384 }
385
Devang Patel0abc4632011-04-04 19:51:17 +0000386 void getContextName(DIDescriptor Context, std::string &N) {
387 if (Context.isNameSpace()) {
388 DINameSpace NS(Context);
389 if (!NS.getName().empty()) {
390 getContextName(NS.getContext(), N);
391 N = N + NS.getName().str() + "::";
Devang Patelb07ec832011-01-31 21:36:24 +0000392 }
Devang Patel0abc4632011-04-04 19:51:17 +0000393 } else if (Context.isType()) {
394 DIType TY(Context);
395 if (!TY.getName().empty()) {
Manman Ren116868e2013-09-09 19:47:11 +0000396 getContextName(TY.getContext().resolve(TypeIdentifierMap), N);
Devang Patel0abc4632011-04-04 19:51:17 +0000397 N = N + TY.getName().str() + "::";
398 }
399 }
400 }
401
402 virtual bool runOnModule(Module &M) {
Manman Ren116868e2013-09-09 19:47:11 +0000403 TypeIdentifierMap.clear();
404 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
405 if (CU_Nodes)
406 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
407
Devang Patel0abc4632011-04-04 19:51:17 +0000408 StringSet<> Processed;
409 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
410 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
411 std::string Name;
412 DISubprogram SP(NMD->getOperand(i));
Manman Ren983a16c2013-06-28 05:43:10 +0000413 assert((!SP || SP.isSubprogram()) &&
414 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
415 if (!SP)
416 continue;
Manman Renc50fa112013-10-10 18:40:01 +0000417 getContextName(SP.getContext().resolve(TypeIdentifierMap), Name);
Devang Patel0abc4632011-04-04 19:51:17 +0000418 Name = Name + SP.getDisplayName().str();
419 if (!Name.empty() && Processed.insert(Name)) {
420 Out << Name << "\n";
421 }
422 }
Devang Patel786a05e2010-12-07 00:33:43 +0000423 return false;
424 }
425
426 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
427 AU.setPreservesAll();
428 }
429};
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000430
Chris Lattner3d70add2011-05-22 00:21:33 +0000431} // anonymous namespace
Devang Patel786a05e2010-12-07 00:33:43 +0000432
433char BreakpointPrinter::ID = 0;
434
Chris Lattner3d70add2011-05-22 00:21:33 +0000435static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000436 // Add the pass to the pass manager...
437 PM.add(P);
438
439 // If we are verifying all of the intermediate steps, add the verifier...
440 if (VerifyEach) PM.add(createVerifierPass());
441}
442
Eric Christophere64061f2009-08-21 23:29:40 +0000443/// AddOptimizationPasses - This routine adds optimization passes
444/// based on selected optimization level, OptLevel. This routine
Devang Patel9966ccf2008-09-16 22:25:14 +0000445/// duplicates llvm-gcc behaviour.
446///
447/// OptLevel - Optimization Level
Chris Lattner3d70add2011-05-22 00:21:33 +0000448static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000449 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sands0f19e912011-12-07 17:14:20 +0000450 FPM.add(createVerifierPass()); // Verify that input is correct
451
Chris Lattner3d70add2011-05-22 00:21:33 +0000452 PassManagerBuilder Builder;
453 Builder.OptLevel = OptLevel;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000454 Builder.SizeLevel = SizeLevel;
Devang Patel9966ccf2008-09-16 22:25:14 +0000455
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000456 if (DisableInline) {
457 // No inlining pass
Eli Friedmanb68fe252011-06-06 22:13:27 +0000458 } else if (OptLevel > 1) {
Jakob Stoklund Olesen1d73c0e2010-11-02 23:40:28 +0000459 unsigned Threshold = 225;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000460 if (SizeLevel == 1) // -Os
461 Threshold = 75;
462 else if (SizeLevel == 2) // -Oz
463 Threshold = 25;
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000464 if (OptLevel > 2)
Jakob Stoklund Olesen1d73c0e2010-11-02 23:40:28 +0000465 Threshold = 275;
Chris Lattner3d70add2011-05-22 00:21:33 +0000466 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000467 } else {
Chris Lattner3d70add2011-05-22 00:21:33 +0000468 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000469 }
Chris Lattner3d70add2011-05-22 00:21:33 +0000470 Builder.DisableUnitAtATime = !UnitAtATime;
Hal Finkel6d099042013-08-28 18:33:10 +0000471 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
472 DisableLoopUnrolling : OptLevel == 0;
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000473
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000474 Builder.LoopVectorize =
475 DisableLoopVectorization ? false : OptLevel > 1 && SizeLevel < 2;
476 Builder.SLPVectorize =
477 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
Hal Finkel6d099042013-08-28 18:33:10 +0000478
Chris Lattner3d70add2011-05-22 00:21:33 +0000479 Builder.populateFunctionPassManager(FPM);
480 Builder.populateModulePassManager(MPM);
Devang Patel9966ccf2008-09-16 22:25:14 +0000481}
482
Chris Lattner3d70add2011-05-22 00:21:33 +0000483static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000484 PM.add(createVerifierPass()); // Verify that input is correct
485
Reid Spencer22dbfb62007-02-02 14:46:29 +0000486 // If the -strip-debug command line option was specified, do it.
487 if (StripDebug)
488 addPass(PM, createStripSymbolsPass(true));
489
490 if (DisableOptimizations) return;
491
Daniel Dunbar94c7b792009-06-03 18:22:15 +0000492 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner3d70add2011-05-22 00:21:33 +0000493 PassManagerBuilder Builder;
494 if (!DisableInline)
495 Builder.Inliner = createFunctionInliningPass();
496 Builder.OptLevel = 3;
Chris Lattner3d70add2011-05-22 00:21:33 +0000497 Builder.populateModulePassManager(PM);
Reid Spencer22dbfb62007-02-02 14:46:29 +0000498}
499
Chris Lattner3d70add2011-05-22 00:21:33 +0000500static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000501 PM.add(createVerifierPass()); // Verify that input is correct
502
503 // If the -strip-debug command line option was specified, do it.
504 if (StripDebug)
505 addPass(PM, createStripSymbolsPass(true));
506
507 if (DisableOptimizations) return;
508
Chris Lattner3d70add2011-05-22 00:21:33 +0000509 PassManagerBuilder Builder;
510 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
511 /*RunInliner=*/ !DisableInline);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000512}
513
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000514//===----------------------------------------------------------------------===//
515// CodeGen-related helper functions.
516//
517static TargetOptions GetTargetOptions() {
518 TargetOptions Options;
519 Options.LessPreciseFPMADOption = EnableFPMAD;
520 Options.NoFramePointerElim = DisableFPElim;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000521 Options.AllowFPOpFusion = FuseFPOps;
522 Options.UnsafeFPMath = EnableUnsafeFPMath;
523 Options.NoInfsFPMath = EnableNoInfsFPMath;
524 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
525 Options.HonorSignDependentRoundingFPMathOption =
526 EnableHonorSignDependentRoundingFPMath;
527 Options.UseSoftFloat = GenerateSoftFloatCalls;
528 if (FloatABIForCalls != FloatABI::Default)
529 Options.FloatABIType = FloatABIForCalls;
530 Options.NoZerosInBSS = DontPlaceZerosInBSS;
531 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
532 Options.DisableTailCalls = DisableTailCalls;
533 Options.StackAlignmentOverride = OverrideStackAlignment;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000534 Options.TrapFuncName = TrapFuncName;
535 Options.PositionIndependentExecutable = EnablePIE;
536 Options.EnableSegmentedStacks = SegmentedStacks;
537 Options.UseInitArray = UseInitArray;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000538 return Options;
539}
540
541CodeGenOpt::Level GetCodeGenOptLevel() {
542 if (OptLevelO1)
543 return CodeGenOpt::Less;
544 if (OptLevelO2)
545 return CodeGenOpt::Default;
546 if (OptLevelO3)
547 return CodeGenOpt::Aggressive;
548 return CodeGenOpt::None;
549}
550
551// Returns the TargetMachine instance or zero if no triple is provided.
Nadav Rotemb1615b12013-01-01 08:00:32 +0000552static TargetMachine* GetTargetMachine(Triple TheTriple) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000553 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000554 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
555 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000556 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000557 if (!TheTarget) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000558 return 0;
Eric Christopher13637e92013-04-15 07:31:37 +0000559 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000560
561 // Package up features to be passed to target/subtarget
562 std::string FeaturesStr;
563 if (MAttrs.size()) {
564 SubtargetFeatures Features;
565 for (unsigned i = 0; i != MAttrs.size(); ++i)
566 Features.AddFeature(MAttrs[i]);
567 FeaturesStr = Features.getString();
568 }
569
570 return TheTarget->createTargetMachine(TheTriple.getTriple(),
571 MCPU, FeaturesStr, GetTargetOptions(),
572 RelocModel, CMModel,
573 GetCodeGenOptLevel());
574}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000575
Chris Lattner5a48a242002-07-23 18:12:22 +0000576//===----------------------------------------------------------------------===//
577// main for opt
578//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579int main(int argc, char **argv) {
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000580 sys::PrintStackTraceOnErrorSignal();
581 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000582
David Greeneed8a1de2010-01-05 01:30:32 +0000583 // Enable debug stream buffering.
584 EnableDebugBuffering = true;
585
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000586 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson19251ec2009-07-15 22:16:10 +0000587 LLVMContext &Context = getGlobalContext();
Andrew Trickb826ae82011-04-05 18:41:31 +0000588
Nadav Rotemac9a3442012-10-24 17:23:50 +0000589 InitializeAllTargets();
590 InitializeAllTargetMCs();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000591
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000592 // Initialize passes
593 PassRegistry &Registry = *PassRegistry::getPassRegistry();
594 initializeCore(Registry);
Daniel Malea3c5bed12013-05-08 20:44:14 +0000595 initializeDebugIRPass(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000596 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000597 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000598 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000599 initializeIPO(Registry);
600 initializeAnalysis(Registry);
601 initializeIPA(Registry);
602 initializeTransformUtils(Registry);
603 initializeInstCombine(Registry);
604 initializeInstrumentation(Registry);
605 initializeTarget(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000606
Chris Lattnercc815262009-10-22 00:46:41 +0000607 cl::ParseCommandLineOptions(argc, argv,
608 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000609
Tobias Grosser083379f2010-12-02 20:35:16 +0000610 if (AnalyzeOnly && NoOutput) {
611 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
612 return 1;
613 }
614
Chris Lattnercc815262009-10-22 00:46:41 +0000615 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000616
Chris Lattnercc815262009-10-22 00:46:41 +0000617 // Load the input module...
Andy Gibbs95777552013-04-12 10:56:28 +0000618 OwningPtr<Module> M;
Chris Lattnercc815262009-10-22 00:46:41 +0000619 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophere64061f2009-08-21 23:29:40 +0000620
Chris Lattnercc815262009-10-22 00:46:41 +0000621 if (M.get() == 0) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000622 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000623 return 1;
624 }
625
Joe Groff1b738692012-04-17 23:05:48 +0000626 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000627 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000628 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000629
Chris Lattnercc815262009-10-22 00:46:41 +0000630 // Figure out what stream we are supposed to write to...
Dan Gohman268b0f42010-08-20 01:07:01 +0000631 OwningPtr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000632 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000633 if (!OutputFilename.empty())
634 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000635 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000636 } else {
637 // Default to standard output.
638 if (OutputFilename.empty())
639 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000640
Dan Gohmanb01aed12010-08-18 17:40:10 +0000641 std::string ErrorInfo;
Dan Gohman268b0f42010-08-20 01:07:01 +0000642 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola6d354812013-07-16 19:44:17 +0000643 sys::fs::F_Binary));
Dan Gohmanb01aed12010-08-18 17:40:10 +0000644 if (!ErrorInfo.empty()) {
645 errs() << ErrorInfo << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000646 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000647 }
Chris Lattnercc815262009-10-22 00:46:41 +0000648 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000649
Chris Lattnercc815262009-10-22 00:46:41 +0000650 // If the output is set to be emitted to standard out, and standard out is a
651 // console, print out a warning message and refuse to do it. We don't
652 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000653 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000654 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000655 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000656
Chris Lattnercc815262009-10-22 00:46:41 +0000657 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000658 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000659 //
660 PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000661
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000662 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner1341df92011-02-18 22:34:03 +0000663 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Trickb826ae82011-04-05 18:41:31 +0000664
Chris Lattner1341df92011-02-18 22:34:03 +0000665 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
666 if (DisableSimplifyLibCalls)
667 TLI->disableAllFunctions();
668 Passes.add(TLI);
Andrew Trickb826ae82011-04-05 18:41:31 +0000669
Eric Christopher13637e92013-04-15 07:31:37 +0000670 // Add an appropriate DataLayout instance for this module.
671 DataLayout *TD = 0;
672 const std::string &ModuleDataLayout = M.get()->getDataLayout();
673 if (!ModuleDataLayout.empty())
674 TD = new DataLayout(ModuleDataLayout);
675 else if (!DefaultDataLayout.empty())
676 TD = new DataLayout(DefaultDataLayout);
677
Chris Lattnercc815262009-10-22 00:46:41 +0000678 if (TD)
Eric Christopher13637e92013-04-15 07:31:37 +0000679 Passes.add(TD);
Reid Spencer996ec722004-12-30 05:36:08 +0000680
Nadav Rotemb1615b12013-01-01 08:00:32 +0000681 Triple ModuleTriple(M->getTargetTriple());
682 TargetMachine *Machine = 0;
683 if (ModuleTriple.getArch())
684 Machine = GetTargetMachine(Triple(ModuleTriple));
Andy Gibbs95777552013-04-12 10:56:28 +0000685 OwningPtr<TargetMachine> TM(Machine);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000686
Chandler Carruth664e3542013-01-07 01:37:14 +0000687 // Add internal analysis passes from the target machine.
688 if (TM.get())
689 TM->addAnalysisPasses(Passes);
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000690
Chris Lattner3d70add2011-05-22 00:21:33 +0000691 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000692 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000693 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnercfa19112009-10-22 00:44:10 +0000694 if (TD)
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000695 FPasses->add(new DataLayout(*TD));
Tom Stellard8b1e0212013-07-27 00:01:07 +0000696 if (TM.get())
697 TM->addAnalysisPasses(*FPasses);
698
Chris Lattnercc815262009-10-22 00:46:41 +0000699 }
Reid Spencer996ec722004-12-30 05:36:08 +0000700
Devang Patel786a05e2010-12-07 00:33:43 +0000701 if (PrintBreakpoints) {
702 // Default to standard output.
703 if (!Out) {
704 if (OutputFilename.empty())
705 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000706
Devang Patel786a05e2010-12-07 00:33:43 +0000707 std::string ErrorInfo;
708 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola6d354812013-07-16 19:44:17 +0000709 sys::fs::F_Binary));
Devang Patel786a05e2010-12-07 00:33:43 +0000710 if (!ErrorInfo.empty()) {
711 errs() << ErrorInfo << '\n';
712 return 1;
713 }
714 }
715 Passes.add(new BreakpointPrinter(Out->os()));
716 NoOutput = true;
717 }
718
Chris Lattnercc815262009-10-22 00:46:41 +0000719 // If the -strip-debug command line option was specified, add it. If
720 // -std-compile-opts was also specified, it will handle StripDebug.
721 if (StripDebug && !StandardCompileOpts)
722 addPass(Passes, createStripSymbolsPass(true));
Eric Christophere64061f2009-08-21 23:29:40 +0000723
Chris Lattnercc815262009-10-22 00:46:41 +0000724 // Create a new optimization pass for each one specified on the command line
725 for (unsigned i = 0; i < PassList.size(); ++i) {
726 // Check to see if -std-compile-opts was specified before this option. If
727 // so, handle it.
728 if (StandardCompileOpts &&
729 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner501c3b62008-07-13 19:35:21 +0000730 AddStandardCompilePasses(Passes);
731 StandardCompileOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000732 }
Reid Spencer996ec722004-12-30 05:36:08 +0000733
Chris Lattnercc815262009-10-22 00:46:41 +0000734 if (StandardLinkOpts &&
735 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000736 AddStandardLinkPasses(Passes);
737 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000738 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000739
Chris Lattnercc815262009-10-22 00:46:41 +0000740 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000741 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000742 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000743 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000744
Chris Lattnercc815262009-10-22 00:46:41 +0000745 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000746 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000747 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000748 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000749
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000750 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
751 AddOptimizationPasses(Passes, *FPasses, 2, 1);
752 OptLevelOs = false;
753 }
754
755 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
756 AddOptimizationPasses(Passes, *FPasses, 2, 2);
757 OptLevelOz = false;
758 }
759
Chris Lattnercc815262009-10-22 00:46:41 +0000760 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000761 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000762 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000763 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000764
Chris Lattnercc815262009-10-22 00:46:41 +0000765 const PassInfo *PassInf = PassList[i];
766 Pass *P = 0;
767 if (PassInf->getNormalCtor())
768 P = PassInf->getNormalCtor()();
769 else
770 errs() << argv[0] << ": cannot create pass: "
771 << PassInf->getPassName() << "\n";
772 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000773 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000774 addPass(Passes, P);
775
776 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000777 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000778 case PT_BasicBlock:
Dan Gohmana2233f22010-09-01 14:20:41 +0000779 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000780 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000781 case PT_Region:
782 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
783 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000784 case PT_Loop:
Dan Gohmana2233f22010-09-01 14:20:41 +0000785 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000786 break;
787 case PT_Function:
Dan Gohmana2233f22010-09-01 14:20:41 +0000788 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000789 break;
790 case PT_CallGraphSCC:
Dan Gohmana2233f22010-09-01 14:20:41 +0000791 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000792 break;
793 default:
Dan Gohmana2233f22010-09-01 14:20:41 +0000794 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000795 break;
796 }
Chris Lattnercc815262009-10-22 00:46:41 +0000797 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000798 }
799
Chris Lattnercc815262009-10-22 00:46:41 +0000800 if (PrintEachXForm)
801 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000802 }
Chris Lattnercc815262009-10-22 00:46:41 +0000803
804 // If -std-compile-opts was specified at the end of the pass list, add them.
805 if (StandardCompileOpts) {
806 AddStandardCompilePasses(Passes);
807 StandardCompileOpts = false;
808 }
809
810 if (StandardLinkOpts) {
811 AddStandardLinkPasses(Passes);
812 StandardLinkOpts = false;
813 }
814
815 if (OptLevelO1)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000816 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000817
818 if (OptLevelO2)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000819 AddOptimizationPasses(Passes, *FPasses, 2, 0);
820
821 if (OptLevelOs)
822 AddOptimizationPasses(Passes, *FPasses, 2, 1);
823
824 if (OptLevelOz)
825 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000826
827 if (OptLevelO3)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000828 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000829
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000830 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000831 FPasses->doInitialization();
Chris Lattner3d70add2011-05-22 00:21:33 +0000832 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
833 FPasses->run(*F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000834 FPasses->doFinalization();
835 }
Chris Lattnercc815262009-10-22 00:46:41 +0000836
837 // Check that the module is well formed on completion of optimization
838 if (!NoVerify && !VerifyEach)
839 Passes.add(createVerifierPass());
840
Dan Gohman083330a2010-08-18 17:42:59 +0000841 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000842 if (!NoOutput && !AnalyzeOnly) {
843 if (OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000844 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000845 else
Dan Gohmana2233f22010-09-01 14:20:41 +0000846 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000847 }
848
Andrew Trick12004012011-04-05 18:54:36 +0000849 // Before executing passes, print the final values of the LLVM options.
850 cl::PrintOptionValues();
851
Chris Lattnercc815262009-10-22 00:46:41 +0000852 // Now that we have all of the passes ready, run them.
853 Passes.run(*M.get());
854
Dan Gohman268b0f42010-08-20 01:07:01 +0000855 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000856 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000857 Out->keep();
858
Chris Lattnercc815262009-10-22 00:46:41 +0000859 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000860}