blob: c4ab3426509fc34e3b586d4ee3a719766c66b84c [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 Carruth66445382014-01-11 08:16:35 +000015#include "NewPMDriver.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"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000023#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/CodeGen/CommandFlags.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000027#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000028#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000030#include "llvm/IRReader/IRReader.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000031#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000032#include "llvm/LinkAllPasses.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000033#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/PassManager.h"
David Greeneed8a1de2010-01-05 01:30:32 +000035#include "llvm/Support/Debug.h"
Chris Lattner76d46322006-12-06 01:18:01 +000036#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Support/PassNameParser.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000038#include "llvm/Support/PluginLoader.h"
Chris Lattner4b2a6e22009-12-09 00:41:28 +000039#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000041#include "llvm/Support/SourceMgr.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000042#include "llvm/Support/SystemUtils.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000043#include "llvm/Support/TargetRegistry.h"
Nadav Rotemac9a3442012-10-24 17:23:50 +000044#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000045#include "llvm/Support/ToolOutputFile.h"
46#include "llvm/Target/TargetLibraryInfo.h"
47#include "llvm/Target/TargetMachine.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000048#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner5a48a242002-07-23 18:12:22 +000049#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000050#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000051using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000052using namespace opt_tool;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000053
Chris Lattner5a48a242002-07-23 18:12:22 +000054// The OptimizationList is automatically populated with registered Passes by the
55// PassNameParser.
56//
Chris Lattner8e5e13b2006-08-27 22:07:01 +000057static cl::list<const PassInfo*, bool, PassNameParser>
58PassList(cl::desc("Optimizations available:"));
Chris Lattner5a48a242002-07-23 18:12:22 +000059
Chandler Carruth66445382014-01-11 08:16:35 +000060// This flag specifies a textual description of the optimization pass pipeline
61// to run over the module. This flag switches opt to use the new pass manager
62// infrastructure, completely disabling all of the flags specific to the old
63// pass management.
64static cl::opt<std::string> PassPipeline(
65 "passes",
66 cl::desc("A textual description of the pass pipeline for optimizing"),
67 cl::Hidden);
68
Chris Lattner5a48a242002-07-23 18:12:22 +000069// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000070//
Chris Lattner02a16832003-05-22 20:13:16 +000071static cl::opt<std::string>
Eric Christophere64061f2009-08-21 23:29:40 +000072InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencer378f7d52006-08-18 06:34:30 +000073 cl::init("-"), cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000074
Chris Lattner02a16832003-05-22 20:13:16 +000075static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000076OutputFilename("o", cl::desc("Override output filename"),
Dan Gohmanb01aed12010-08-18 17:40:10 +000077 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000078
79static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000080Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000081
82static cl::opt<bool>
83PrintEachXForm("p", cl::desc("Print module after each transformation"));
84
85static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000086NoOutput("disable-output",
Gabor Greife16561c2007-07-05 17:07:56 +000087 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000088
89static cl::opt<bool>
Duncan Sands6f2ffce2009-10-14 20:01:39 +000090OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar6b3153b2009-09-05 11:34:53 +000091
92static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000093NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +000094
95static cl::opt<bool>
Reid Spencer22dbfb62007-02-02 14:46:29 +000096VerifyEach("verify-each", cl::desc("Verify after each transform"));
97
98static cl::opt<bool>
99StripDebug("strip-debug",
100 cl::desc("Strip debugger symbol info from translation unit"));
101
102static cl::opt<bool>
103DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
104
Eric Christophere64061f2009-08-21 23:29:40 +0000105static cl::opt<bool>
106DisableOptimizations("disable-opt",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000107 cl::desc("Do not run any optimization passes"));
108
Eric Christophere64061f2009-08-21 23:29:40 +0000109static cl::opt<bool>
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000110DisableInternalize("disable-internalize",
111 cl::desc("Do not mark all symbols as internal"));
112
Reid Spencer22dbfb62007-02-02 14:46:29 +0000113static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000114StandardCompileOpts("std-compile-opts",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000115 cl::desc("Include the standard compile time optimizations"));
116
117static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000118StandardLinkOpts("std-link-opts",
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000119 cl::desc("Include the standard link time optimizations"));
120
121static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000122OptLevelO1("O1",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000123 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000124
125static cl::opt<bool>
126OptLevelO2("O2",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000127 cl::desc("Optimization level 2. Similar to clang -O2"));
128
129static cl::opt<bool>
130OptLevelOs("Os",
131 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
132
133static cl::opt<bool>
134OptLevelOz("Oz",
135 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000136
137static cl::opt<bool>
138OptLevelO3("O3",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000139 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000140
Joe Groff1b738692012-04-17 23:05:48 +0000141static cl::opt<std::string>
142TargetTriple("mtriple", cl::desc("Override target triple for module"));
143
Devang Patel9966ccf2008-09-16 22:25:14 +0000144static cl::opt<bool>
145UnitAtATime("funit-at-a-time",
Eric Christopherc8f625b2009-08-21 23:30:30 +0000146 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000147 cl::init(true));
Devang Patel9966ccf2008-09-16 22:25:14 +0000148
149static cl::opt<bool>
Hal Finkel6d099042013-08-28 18:33:10 +0000150DisableLoopUnrolling("disable-loop-unrolling",
151 cl::desc("Disable loop unrolling in all relevant passes"),
152 cl::init(false));
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000153static cl::opt<bool>
154DisableLoopVectorization("disable-loop-vectorization",
155 cl::desc("Disable the loop vectorization pass"),
156 cl::init(false));
157
158static cl::opt<bool>
159DisableSLPVectorization("disable-slp-vectorization",
160 cl::desc("Disable the slp vectorization pass"),
161 cl::init(false));
162
Hal Finkel6d099042013-08-28 18:33:10 +0000163
164static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000165DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000166 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000167
168static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000169Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000170
Reid Spencerc8878ed2004-05-27 16:28:54 +0000171static cl::alias
172QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
173
Reid Spencer378f7d52006-08-18 06:34:30 +0000174static cl::opt<bool>
175AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
176
Devang Patel786a05e2010-12-07 00:33:43 +0000177static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000178PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000179 cl::desc("Print select breakpoints location for testing"));
180
Chris Lattnercfa19112009-10-22 00:44:10 +0000181static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000182DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-10-22 00:44:10 +0000183 cl::desc("data layout string to use if not specified by module"),
184 cl::value_desc("layout-string"), cl::init(""));
185
Reid Spencer378f7d52006-08-18 06:34:30 +0000186// ---------- Define Printers for module and function passes ------------
187namespace {
188
Devang Patel9a259a22007-06-28 23:09:25 +0000189struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
190 static char ID;
191 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000192 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000193 std::string PassName;
194
Dan Gohman083330a2010-08-18 17:42:59 +0000195 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grossere5fcde72010-09-08 15:02:47 +0000196 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
197 std::string PassToPrintName = PassToPrint->getPassName();
198 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
199 }
Devang Patel9a259a22007-06-28 23:09:25 +0000200
Chris Lattner4422d312010-04-16 22:42:17 +0000201 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000202 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000203 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel9a259a22007-06-28 23:09:25 +0000204
Devang Patel9a259a22007-06-28 23:09:25 +0000205 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000206 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
207 Function *F = (*I)->getFunction();
208 if (F)
209 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
210 F->getParent());
211 }
Devang Patel9a259a22007-06-28 23:09:25 +0000212 return false;
213 }
Eric Christophere64061f2009-08-21 23:29:40 +0000214
Tobias Grossere5fcde72010-09-08 15:02:47 +0000215 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel9a259a22007-06-28 23:09:25 +0000216
217 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000218 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel9a259a22007-06-28 23:09:25 +0000219 AU.setPreservesAll();
220 }
221};
222
223char CallGraphSCCPassPrinter::ID = 0;
224
Reid Spencer378f7d52006-08-18 06:34:30 +0000225struct ModulePassPrinter : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +0000226 static char ID;
Reid Spencer378f7d52006-08-18 06:34:30 +0000227 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000228 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000229 std::string PassName;
230
Dan Gohman083330a2010-08-18 17:42:59 +0000231 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-09-08 15:02:47 +0000232 : ModulePass(ID), PassToPrint(PI), Out(out) {
233 std::string PassToPrintName = PassToPrint->getPassName();
234 PassName = "ModulePass Printer: " + PassToPrintName;
235 }
Reid Spencer378f7d52006-08-18 06:34:30 +0000236
237 virtual bool runOnModule(Module &M) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000238 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000239 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencer378f7d52006-08-18 06:34:30 +0000240
241 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000242 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencer378f7d52006-08-18 06:34:30 +0000243 return false;
244 }
245
Tobias Grossere5fcde72010-09-08 15:02:47 +0000246 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000247
248 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000249 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000250 AU.setPreservesAll();
251 }
252};
253
Devang Patel8c78a0b2007-05-03 01:11:54 +0000254char ModulePassPrinter::ID = 0;
Reid Spencer378f7d52006-08-18 06:34:30 +0000255struct FunctionPassPrinter : public FunctionPass {
256 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000257 raw_ostream &Out;
Devang Patel8c78a0b2007-05-03 01:11:54 +0000258 static char ID;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000259 std::string PassName;
260
Dan Gohman083330a2010-08-18 17:42:59 +0000261 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-09-08 15:02:47 +0000262 : FunctionPass(ID), PassToPrint(PI), Out(out) {
263 std::string PassToPrintName = PassToPrint->getPassName();
264 PassName = "FunctionPass Printer: " + PassToPrintName;
265 }
Reid Spencer378f7d52006-08-18 06:34:30 +0000266
267 virtual bool runOnFunction(Function &F) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000268 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000269 Out << "Printing analysis '" << PassToPrint->getPassName()
270 << "' for function '" << F.getName() << "':\n";
Tobias Grosser011d79a2010-09-08 15:02:51 +0000271
Reid Spencer378f7d52006-08-18 06:34:30 +0000272 // Get and print pass...
Dan Gohman083330a2010-08-18 17:42:59 +0000273 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Andersona7aed182010-08-06 18:33:48 +0000274 F.getParent());
Reid Spencer378f7d52006-08-18 06:34:30 +0000275 return false;
276 }
277
Tobias Grossere5fcde72010-09-08 15:02:47 +0000278 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000279
280 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000281 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000282 AU.setPreservesAll();
283 }
284};
285
Devang Patel8c78a0b2007-05-03 01:11:54 +0000286char FunctionPassPrinter::ID = 0;
Devang Patelf5a15dd2007-07-05 15:32:03 +0000287
288struct LoopPassPrinter : public LoopPass {
289 static char ID;
290 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000291 raw_ostream &Out;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000292 std::string PassName;
293
Dan Gohman083330a2010-08-18 17:42:59 +0000294 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grossere5fcde72010-09-08 15:02:47 +0000295 LoopPass(ID), PassToPrint(PI), Out(out) {
296 std::string PassToPrintName = PassToPrint->getPassName();
297 PassName = "LoopPass Printer: " + PassToPrintName;
298 }
299
Devang Patelf5a15dd2007-07-05 15:32:03 +0000300
301 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000302 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000303 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grosser011d79a2010-09-08 15:02:51 +0000304
Devang Patelf5a15dd2007-07-05 15:32:03 +0000305 // Get and print pass...
Tobias Grosser011d79a2010-09-08 15:02:51 +0000306 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
307 L->getHeader()->getParent()->getParent());
Devang Patelf5a15dd2007-07-05 15:32:03 +0000308 return false;
309 }
Eric Christophere64061f2009-08-21 23:29:40 +0000310
Tobias Grossere5fcde72010-09-08 15:02:47 +0000311 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patelf5a15dd2007-07-05 15:32:03 +0000312
313 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000314 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patelf5a15dd2007-07-05 15:32:03 +0000315 AU.setPreservesAll();
316 }
317};
318
319char LoopPassPrinter::ID = 0;
320
Tobias Grosser23c83412010-10-20 01:54:44 +0000321struct RegionPassPrinter : public RegionPass {
322 static char ID;
323 const PassInfo *PassToPrint;
324 raw_ostream &Out;
325 std::string PassName;
326
327 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
328 PassToPrint(PI), Out(out) {
329 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser7dbb4822011-01-20 21:03:20 +0000330 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser23c83412010-10-20 01:54:44 +0000331 }
332
333 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
334 if (!Quiet) {
335 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000336 << "region: '" << R->getNameStr() << "' in function '"
337 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser23c83412010-10-20 01:54:44 +0000338 }
339 // Get and print pass...
340 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
341 R->getEntry()->getParent()->getParent());
342 return false;
343 }
344
Tobias Grosser7dbb4822011-01-20 21:03:20 +0000345 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser23c83412010-10-20 01:54:44 +0000346
347 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
348 AU.addRequiredID(PassToPrint->getTypeInfo());
349 AU.setPreservesAll();
350 }
351};
352
353char RegionPassPrinter::ID = 0;
354
Reid Spencer378f7d52006-08-18 06:34:30 +0000355struct BasicBlockPassPrinter : public BasicBlockPass {
356 const PassInfo *PassToPrint;
Dan Gohman083330a2010-08-18 17:42:59 +0000357 raw_ostream &Out;
Devang Patel8c78a0b2007-05-03 01:11:54 +0000358 static char ID;
Tobias Grossere5fcde72010-09-08 15:02:47 +0000359 std::string PassName;
360
Dan Gohman083330a2010-08-18 17:42:59 +0000361 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grossere5fcde72010-09-08 15:02:47 +0000362 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
363 std::string PassToPrintName = PassToPrint->getPassName();
364 PassName = "BasicBlockPass Printer: " + PassToPrintName;
365 }
Reid Spencer378f7d52006-08-18 06:34:30 +0000366
367 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grosser011d79a2010-09-08 15:02:51 +0000368 if (!Quiet)
Dan Gohman083330a2010-08-18 17:42:59 +0000369 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
370 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencer378f7d52006-08-18 06:34:30 +0000371
372 // Get and print pass...
Andrew Trickb826ae82011-04-05 18:41:31 +0000373 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Andersona7aed182010-08-06 18:33:48 +0000374 BB.getParent()->getParent());
Reid Spencer378f7d52006-08-18 06:34:30 +0000375 return false;
376 }
377
Tobias Grossere5fcde72010-09-08 15:02:47 +0000378 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencer378f7d52006-08-18 06:34:30 +0000379
380 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersona7aed182010-08-06 18:33:48 +0000381 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencer378f7d52006-08-18 06:34:30 +0000382 AU.setPreservesAll();
383 }
384};
385
Devang Patel8c78a0b2007-05-03 01:11:54 +0000386char BasicBlockPassPrinter::ID = 0;
Devang Patel786a05e2010-12-07 00:33:43 +0000387
Devang Patel0abc4632011-04-04 19:51:17 +0000388struct BreakpointPrinter : public ModulePass {
Devang Patel786a05e2010-12-07 00:33:43 +0000389 raw_ostream &Out;
390 static char ID;
Manman Ren116868e2013-09-09 19:47:11 +0000391 DITypeIdentifierMap TypeIdentifierMap;
Devang Patel786a05e2010-12-07 00:33:43 +0000392
393 BreakpointPrinter(raw_ostream &out)
Devang Patel0abc4632011-04-04 19:51:17 +0000394 : ModulePass(ID), Out(out) {
Devang Patel786a05e2010-12-07 00:33:43 +0000395 }
396
Devang Patel0abc4632011-04-04 19:51:17 +0000397 void getContextName(DIDescriptor Context, std::string &N) {
398 if (Context.isNameSpace()) {
399 DINameSpace NS(Context);
400 if (!NS.getName().empty()) {
401 getContextName(NS.getContext(), N);
402 N = N + NS.getName().str() + "::";
Devang Patelb07ec832011-01-31 21:36:24 +0000403 }
Devang Patel0abc4632011-04-04 19:51:17 +0000404 } else if (Context.isType()) {
405 DIType TY(Context);
406 if (!TY.getName().empty()) {
Manman Ren116868e2013-09-09 19:47:11 +0000407 getContextName(TY.getContext().resolve(TypeIdentifierMap), N);
Devang Patel0abc4632011-04-04 19:51:17 +0000408 N = N + TY.getName().str() + "::";
409 }
410 }
411 }
412
413 virtual bool runOnModule(Module &M) {
Manman Ren116868e2013-09-09 19:47:11 +0000414 TypeIdentifierMap.clear();
415 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
416 if (CU_Nodes)
417 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
418
Devang Patel0abc4632011-04-04 19:51:17 +0000419 StringSet<> Processed;
420 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
421 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
422 std::string Name;
423 DISubprogram SP(NMD->getOperand(i));
Manman Ren983a16c2013-06-28 05:43:10 +0000424 assert((!SP || SP.isSubprogram()) &&
425 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
426 if (!SP)
427 continue;
Manman Renc50fa112013-10-10 18:40:01 +0000428 getContextName(SP.getContext().resolve(TypeIdentifierMap), Name);
Devang Patel0abc4632011-04-04 19:51:17 +0000429 Name = Name + SP.getDisplayName().str();
430 if (!Name.empty() && Processed.insert(Name)) {
431 Out << Name << "\n";
432 }
433 }
Devang Patel786a05e2010-12-07 00:33:43 +0000434 return false;
435 }
436
437 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
438 AU.setPreservesAll();
439 }
440};
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000441
Chris Lattner3d70add2011-05-22 00:21:33 +0000442} // anonymous namespace
Devang Patel786a05e2010-12-07 00:33:43 +0000443
444char BreakpointPrinter::ID = 0;
445
Chris Lattner3d70add2011-05-22 00:21:33 +0000446static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000447 // Add the pass to the pass manager...
448 PM.add(P);
449
450 // If we are verifying all of the intermediate steps, add the verifier...
451 if (VerifyEach) PM.add(createVerifierPass());
452}
453
Eric Christophere64061f2009-08-21 23:29:40 +0000454/// AddOptimizationPasses - This routine adds optimization passes
455/// based on selected optimization level, OptLevel. This routine
Devang Patel9966ccf2008-09-16 22:25:14 +0000456/// duplicates llvm-gcc behaviour.
457///
458/// OptLevel - Optimization Level
Chris Lattner3d70add2011-05-22 00:21:33 +0000459static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000460 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sands0f19e912011-12-07 17:14:20 +0000461 FPM.add(createVerifierPass()); // Verify that input is correct
462
Chris Lattner3d70add2011-05-22 00:21:33 +0000463 PassManagerBuilder Builder;
464 Builder.OptLevel = OptLevel;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000465 Builder.SizeLevel = SizeLevel;
Devang Patel9966ccf2008-09-16 22:25:14 +0000466
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000467 if (DisableInline) {
468 // No inlining pass
Eli Friedmanb68fe252011-06-06 22:13:27 +0000469 } else if (OptLevel > 1) {
Jakob Stoklund Olesen1d73c0e2010-11-02 23:40:28 +0000470 unsigned Threshold = 225;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000471 if (SizeLevel == 1) // -Os
472 Threshold = 75;
473 else if (SizeLevel == 2) // -Oz
474 Threshold = 25;
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000475 if (OptLevel > 2)
Jakob Stoklund Olesen1d73c0e2010-11-02 23:40:28 +0000476 Threshold = 275;
Chris Lattner3d70add2011-05-22 00:21:33 +0000477 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000478 } else {
Chris Lattner3d70add2011-05-22 00:21:33 +0000479 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000480 }
Chris Lattner3d70add2011-05-22 00:21:33 +0000481 Builder.DisableUnitAtATime = !UnitAtATime;
Hal Finkel6d099042013-08-28 18:33:10 +0000482 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
483 DisableLoopUnrolling : OptLevel == 0;
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000484
Renato Golin729a3ae2013-12-05 21:20:02 +0000485 // This is final, unless there is a #pragma vectorize enable
486 if (DisableLoopVectorization)
487 Builder.LoopVectorize = false;
488 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
489 else if (!Builder.LoopVectorize)
490 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
491
492 // When #pragma vectorize is on for SLP, do the same as above
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000493 Builder.SLPVectorize =
494 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
Hal Finkel6d099042013-08-28 18:33:10 +0000495
Chris Lattner3d70add2011-05-22 00:21:33 +0000496 Builder.populateFunctionPassManager(FPM);
497 Builder.populateModulePassManager(MPM);
Devang Patel9966ccf2008-09-16 22:25:14 +0000498}
499
Chris Lattner3d70add2011-05-22 00:21:33 +0000500static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000501 PM.add(createVerifierPass()); // Verify that input is correct
502
Reid Spencer22dbfb62007-02-02 14:46:29 +0000503 // 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
Daniel Dunbar94c7b792009-06-03 18:22:15 +0000509 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner3d70add2011-05-22 00:21:33 +0000510 PassManagerBuilder Builder;
511 if (!DisableInline)
512 Builder.Inliner = createFunctionInliningPass();
513 Builder.OptLevel = 3;
Chris Lattner3d70add2011-05-22 00:21:33 +0000514 Builder.populateModulePassManager(PM);
Reid Spencer22dbfb62007-02-02 14:46:29 +0000515}
516
Chris Lattner3d70add2011-05-22 00:21:33 +0000517static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000518 PM.add(createVerifierPass()); // Verify that input is correct
519
520 // If the -strip-debug command line option was specified, do it.
521 if (StripDebug)
522 addPass(PM, createStripSymbolsPass(true));
523
524 if (DisableOptimizations) return;
525
Chris Lattner3d70add2011-05-22 00:21:33 +0000526 PassManagerBuilder Builder;
527 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
528 /*RunInliner=*/ !DisableInline);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000529}
530
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000531//===----------------------------------------------------------------------===//
532// CodeGen-related helper functions.
533//
534static TargetOptions GetTargetOptions() {
535 TargetOptions Options;
536 Options.LessPreciseFPMADOption = EnableFPMAD;
537 Options.NoFramePointerElim = DisableFPElim;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000538 Options.AllowFPOpFusion = FuseFPOps;
539 Options.UnsafeFPMath = EnableUnsafeFPMath;
540 Options.NoInfsFPMath = EnableNoInfsFPMath;
541 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
542 Options.HonorSignDependentRoundingFPMathOption =
543 EnableHonorSignDependentRoundingFPMath;
544 Options.UseSoftFloat = GenerateSoftFloatCalls;
545 if (FloatABIForCalls != FloatABI::Default)
546 Options.FloatABIType = FloatABIForCalls;
547 Options.NoZerosInBSS = DontPlaceZerosInBSS;
548 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
549 Options.DisableTailCalls = DisableTailCalls;
550 Options.StackAlignmentOverride = OverrideStackAlignment;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000551 Options.TrapFuncName = TrapFuncName;
552 Options.PositionIndependentExecutable = EnablePIE;
553 Options.EnableSegmentedStacks = SegmentedStacks;
554 Options.UseInitArray = UseInitArray;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000555 return Options;
556}
557
558CodeGenOpt::Level GetCodeGenOptLevel() {
559 if (OptLevelO1)
560 return CodeGenOpt::Less;
561 if (OptLevelO2)
562 return CodeGenOpt::Default;
563 if (OptLevelO3)
564 return CodeGenOpt::Aggressive;
565 return CodeGenOpt::None;
566}
567
568// Returns the TargetMachine instance or zero if no triple is provided.
Nadav Rotemb1615b12013-01-01 08:00:32 +0000569static TargetMachine* GetTargetMachine(Triple TheTriple) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000570 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000571 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
572 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000573 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000574 if (!TheTarget) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000575 return 0;
Eric Christopher13637e92013-04-15 07:31:37 +0000576 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000577
578 // Package up features to be passed to target/subtarget
579 std::string FeaturesStr;
580 if (MAttrs.size()) {
581 SubtargetFeatures Features;
582 for (unsigned i = 0; i != MAttrs.size(); ++i)
583 Features.AddFeature(MAttrs[i]);
584 FeaturesStr = Features.getString();
585 }
586
587 return TheTarget->createTargetMachine(TheTriple.getTriple(),
588 MCPU, FeaturesStr, GetTargetOptions(),
589 RelocModel, CMModel,
590 GetCodeGenOptLevel());
591}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000592
Chris Lattner5a48a242002-07-23 18:12:22 +0000593//===----------------------------------------------------------------------===//
594// main for opt
595//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596int main(int argc, char **argv) {
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000597 sys::PrintStackTraceOnErrorSignal();
598 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000599
David Greeneed8a1de2010-01-05 01:30:32 +0000600 // Enable debug stream buffering.
601 EnableDebugBuffering = true;
602
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000603 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson19251ec2009-07-15 22:16:10 +0000604 LLVMContext &Context = getGlobalContext();
Andrew Trickb826ae82011-04-05 18:41:31 +0000605
Nadav Rotemac9a3442012-10-24 17:23:50 +0000606 InitializeAllTargets();
607 InitializeAllTargetMCs();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000608
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000609 // Initialize passes
610 PassRegistry &Registry = *PassRegistry::getPassRegistry();
611 initializeCore(Registry);
Daniel Malea3c5bed12013-05-08 20:44:14 +0000612 initializeDebugIRPass(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000613 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000614 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000615 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000616 initializeIPO(Registry);
617 initializeAnalysis(Registry);
618 initializeIPA(Registry);
619 initializeTransformUtils(Registry);
620 initializeInstCombine(Registry);
621 initializeInstrumentation(Registry);
622 initializeTarget(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000623
Chris Lattnercc815262009-10-22 00:46:41 +0000624 cl::ParseCommandLineOptions(argc, argv,
625 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000626
Tobias Grosser083379f2010-12-02 20:35:16 +0000627 if (AnalyzeOnly && NoOutput) {
628 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
629 return 1;
630 }
631
Chris Lattnercc815262009-10-22 00:46:41 +0000632 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000633
Chris Lattnercc815262009-10-22 00:46:41 +0000634 // Load the input module...
Andy Gibbs95777552013-04-12 10:56:28 +0000635 OwningPtr<Module> M;
Chris Lattnercc815262009-10-22 00:46:41 +0000636 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophere64061f2009-08-21 23:29:40 +0000637
Chris Lattnercc815262009-10-22 00:46:41 +0000638 if (M.get() == 0) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000639 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000640 return 1;
641 }
642
Joe Groff1b738692012-04-17 23:05:48 +0000643 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000644 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000645 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000646
Chris Lattnercc815262009-10-22 00:46:41 +0000647 // Figure out what stream we are supposed to write to...
Dan Gohman268b0f42010-08-20 01:07:01 +0000648 OwningPtr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000649 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000650 if (!OutputFilename.empty())
651 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000652 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000653 } else {
654 // Default to standard output.
655 if (OutputFilename.empty())
656 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000657
Dan Gohmanb01aed12010-08-18 17:40:10 +0000658 std::string ErrorInfo;
Dan Gohman268b0f42010-08-20 01:07:01 +0000659 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola6d354812013-07-16 19:44:17 +0000660 sys::fs::F_Binary));
Dan Gohmanb01aed12010-08-18 17:40:10 +0000661 if (!ErrorInfo.empty()) {
662 errs() << ErrorInfo << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000663 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000664 }
Chris Lattnercc815262009-10-22 00:46:41 +0000665 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000666
Chris Lattnercc815262009-10-22 00:46:41 +0000667 // If the output is set to be emitted to standard out, and standard out is a
668 // console, print out a warning message and refuse to do it. We don't
669 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000670 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000671 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000672 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000673
Chandler Carruth949282e2014-01-13 03:08:40 +0000674 if (PassPipeline.getNumOccurrences() > 0) {
675 OutputKind OK = OK_NoOutput;
676 if (!NoOutput)
677 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
678
Chandler Carruth66445382014-01-11 08:16:35 +0000679 // The user has asked to use the new pass manager and provided a pipeline
680 // string. Hand off the rest of the functionality to the new code for that
681 // layer.
682 return runPassPipeline(argv[0], Context, *M.get(), Out.get(), PassPipeline,
Chandler Carruth949282e2014-01-13 03:08:40 +0000683 OK)
Chandler Carruth66445382014-01-11 08:16:35 +0000684 ? 0
685 : 1;
Chandler Carruth949282e2014-01-13 03:08:40 +0000686 }
Chandler Carruth66445382014-01-11 08:16:35 +0000687
Chris Lattnercc815262009-10-22 00:46:41 +0000688 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000689 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000690 //
691 PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000692
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000693 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner1341df92011-02-18 22:34:03 +0000694 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Trickb826ae82011-04-05 18:41:31 +0000695
Chris Lattner1341df92011-02-18 22:34:03 +0000696 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
697 if (DisableSimplifyLibCalls)
698 TLI->disableAllFunctions();
699 Passes.add(TLI);
Andrew Trickb826ae82011-04-05 18:41:31 +0000700
Eric Christopher13637e92013-04-15 07:31:37 +0000701 // Add an appropriate DataLayout instance for this module.
702 DataLayout *TD = 0;
703 const std::string &ModuleDataLayout = M.get()->getDataLayout();
704 if (!ModuleDataLayout.empty())
705 TD = new DataLayout(ModuleDataLayout);
706 else if (!DefaultDataLayout.empty())
707 TD = new DataLayout(DefaultDataLayout);
708
Chris Lattnercc815262009-10-22 00:46:41 +0000709 if (TD)
Eric Christopher13637e92013-04-15 07:31:37 +0000710 Passes.add(TD);
Reid Spencer996ec722004-12-30 05:36:08 +0000711
Nadav Rotemb1615b12013-01-01 08:00:32 +0000712 Triple ModuleTriple(M->getTargetTriple());
713 TargetMachine *Machine = 0;
714 if (ModuleTriple.getArch())
715 Machine = GetTargetMachine(Triple(ModuleTriple));
Andy Gibbs95777552013-04-12 10:56:28 +0000716 OwningPtr<TargetMachine> TM(Machine);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000717
Chandler Carruth664e3542013-01-07 01:37:14 +0000718 // Add internal analysis passes from the target machine.
719 if (TM.get())
720 TM->addAnalysisPasses(Passes);
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000721
Chris Lattner3d70add2011-05-22 00:21:33 +0000722 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000723 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000724 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnercfa19112009-10-22 00:44:10 +0000725 if (TD)
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000726 FPasses->add(new DataLayout(*TD));
Tom Stellard8b1e0212013-07-27 00:01:07 +0000727 if (TM.get())
728 TM->addAnalysisPasses(*FPasses);
729
Chris Lattnercc815262009-10-22 00:46:41 +0000730 }
Reid Spencer996ec722004-12-30 05:36:08 +0000731
Devang Patel786a05e2010-12-07 00:33:43 +0000732 if (PrintBreakpoints) {
733 // Default to standard output.
734 if (!Out) {
735 if (OutputFilename.empty())
736 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000737
Devang Patel786a05e2010-12-07 00:33:43 +0000738 std::string ErrorInfo;
739 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola6d354812013-07-16 19:44:17 +0000740 sys::fs::F_Binary));
Devang Patel786a05e2010-12-07 00:33:43 +0000741 if (!ErrorInfo.empty()) {
742 errs() << ErrorInfo << '\n';
743 return 1;
744 }
745 }
746 Passes.add(new BreakpointPrinter(Out->os()));
747 NoOutput = true;
748 }
749
Chris Lattnercc815262009-10-22 00:46:41 +0000750 // If the -strip-debug command line option was specified, add it. If
751 // -std-compile-opts was also specified, it will handle StripDebug.
752 if (StripDebug && !StandardCompileOpts)
753 addPass(Passes, createStripSymbolsPass(true));
Eric Christophere64061f2009-08-21 23:29:40 +0000754
Chris Lattnercc815262009-10-22 00:46:41 +0000755 // Create a new optimization pass for each one specified on the command line
756 for (unsigned i = 0; i < PassList.size(); ++i) {
757 // Check to see if -std-compile-opts was specified before this option. If
758 // so, handle it.
759 if (StandardCompileOpts &&
760 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner501c3b62008-07-13 19:35:21 +0000761 AddStandardCompilePasses(Passes);
762 StandardCompileOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000763 }
Reid Spencer996ec722004-12-30 05:36:08 +0000764
Chris Lattnercc815262009-10-22 00:46:41 +0000765 if (StandardLinkOpts &&
766 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000767 AddStandardLinkPasses(Passes);
768 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000769 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000770
Chris Lattnercc815262009-10-22 00:46:41 +0000771 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000772 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000773 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000774 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000775
Chris Lattnercc815262009-10-22 00:46:41 +0000776 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000777 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000778 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000779 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000780
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000781 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
782 AddOptimizationPasses(Passes, *FPasses, 2, 1);
783 OptLevelOs = false;
784 }
785
786 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
787 AddOptimizationPasses(Passes, *FPasses, 2, 2);
788 OptLevelOz = false;
789 }
790
Chris Lattnercc815262009-10-22 00:46:41 +0000791 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000792 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000793 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000794 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000795
Chris Lattnercc815262009-10-22 00:46:41 +0000796 const PassInfo *PassInf = PassList[i];
797 Pass *P = 0;
798 if (PassInf->getNormalCtor())
799 P = PassInf->getNormalCtor()();
800 else
801 errs() << argv[0] << ": cannot create pass: "
802 << PassInf->getPassName() << "\n";
803 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000804 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000805 addPass(Passes, P);
806
807 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000808 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000809 case PT_BasicBlock:
Dan Gohmana2233f22010-09-01 14:20:41 +0000810 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000811 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000812 case PT_Region:
813 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
814 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000815 case PT_Loop:
Dan Gohmana2233f22010-09-01 14:20:41 +0000816 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000817 break;
818 case PT_Function:
Dan Gohmana2233f22010-09-01 14:20:41 +0000819 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000820 break;
821 case PT_CallGraphSCC:
Dan Gohmana2233f22010-09-01 14:20:41 +0000822 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000823 break;
824 default:
Dan Gohmana2233f22010-09-01 14:20:41 +0000825 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000826 break;
827 }
Chris Lattnercc815262009-10-22 00:46:41 +0000828 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000829 }
830
Chris Lattnercc815262009-10-22 00:46:41 +0000831 if (PrintEachXForm)
Chandler Carruth9d805132014-01-12 11:30:46 +0000832 Passes.add(createPrintModulePass(errs()));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000833 }
Chris Lattnercc815262009-10-22 00:46:41 +0000834
835 // If -std-compile-opts was specified at the end of the pass list, add them.
836 if (StandardCompileOpts) {
837 AddStandardCompilePasses(Passes);
838 StandardCompileOpts = false;
839 }
840
841 if (StandardLinkOpts) {
842 AddStandardLinkPasses(Passes);
843 StandardLinkOpts = false;
844 }
845
846 if (OptLevelO1)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000847 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000848
849 if (OptLevelO2)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000850 AddOptimizationPasses(Passes, *FPasses, 2, 0);
851
852 if (OptLevelOs)
853 AddOptimizationPasses(Passes, *FPasses, 2, 1);
854
855 if (OptLevelOz)
856 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000857
858 if (OptLevelO3)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000859 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000860
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000861 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000862 FPasses->doInitialization();
Chris Lattner3d70add2011-05-22 00:21:33 +0000863 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
864 FPasses->run(*F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000865 FPasses->doFinalization();
866 }
Chris Lattnercc815262009-10-22 00:46:41 +0000867
868 // Check that the module is well formed on completion of optimization
869 if (!NoVerify && !VerifyEach)
870 Passes.add(createVerifierPass());
871
Dan Gohman083330a2010-08-18 17:42:59 +0000872 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000873 if (!NoOutput && !AnalyzeOnly) {
874 if (OutputAssembly)
Chandler Carruth9d805132014-01-12 11:30:46 +0000875 Passes.add(createPrintModulePass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000876 else
Dan Gohmana2233f22010-09-01 14:20:41 +0000877 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000878 }
879
Andrew Trick12004012011-04-05 18:54:36 +0000880 // Before executing passes, print the final values of the LLVM options.
881 cl::PrintOptionValues();
882
Chris Lattnercc815262009-10-22 00:46:41 +0000883 // Now that we have all of the passes ready, run them.
884 Passes.run(*M.get());
885
Dan Gohman268b0f42010-08-20 01:07:01 +0000886 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000887 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000888 Out->keep();
889
Chris Lattnercc815262009-10-22 00:46:41 +0000890 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000891}