blob: 58376ee7ee9c35f5dc21b748df22c4e7b9d1ecab [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
Eli Bendersky54dc2832014-02-12 16:48:02 +000015#include "BreakpointPrinter.h"
Chandler Carruth66445382014-01-11 08:16:35 +000016#include "NewPMDriver.h"
Eli Benderskyb60f8382014-02-10 23:34:23 +000017#include "PassPrinters.h"
Chris Lattner15c8b5e2011-02-18 22:13:01 +000018#include "llvm/ADT/Triple.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000019#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000020#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000021#include "llvm/Analysis/LoopPass.h"
22#include "llvm/Analysis/RegionPass.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000023#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000024#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000025#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/CodeGen/CommandFlags.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DataLayout.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000028#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000029#include "llvm/IR/LLVMContext.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000030#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000032#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000033#include "llvm/IRReader/IRReader.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000034#include "llvm/InitializePasses.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000035#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000036#include "llvm/LinkAllPasses.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000038#include "llvm/IR/LegacyPassManager.h"
David Greeneed8a1de2010-01-05 01:30:32 +000039#include "llvm/Support/Debug.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000040#include "llvm/Support/FileSystem.h"
Chris Lattner76d46322006-12-06 01:18:01 +000041#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000042#include "llvm/Support/PluginLoader.h"
Chris Lattner4b2a6e22009-12-09 00:41:28 +000043#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000044#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000045#include "llvm/Support/SourceMgr.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000046#include "llvm/Support/SystemUtils.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000047#include "llvm/Support/TargetRegistry.h"
Nadav Rotemac9a3442012-10-24 17:23:50 +000048#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000049#include "llvm/Support/ToolOutputFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000050#include "llvm/Target/TargetMachine.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000051#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner5a48a242002-07-23 18:12:22 +000052#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000053#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000054using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000055using namespace opt_tool;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000056
Chris Lattner5a48a242002-07-23 18:12:22 +000057// The OptimizationList is automatically populated with registered Passes by the
58// PassNameParser.
59//
Chris Lattner8e5e13b2006-08-27 22:07:01 +000060static cl::list<const PassInfo*, bool, PassNameParser>
61PassList(cl::desc("Optimizations available:"));
Chris Lattner5a48a242002-07-23 18:12:22 +000062
Chandler Carruth66445382014-01-11 08:16:35 +000063// This flag specifies a textual description of the optimization pass pipeline
64// to run over the module. This flag switches opt to use the new pass manager
65// infrastructure, completely disabling all of the flags specific to the old
66// pass management.
67static cl::opt<std::string> PassPipeline(
68 "passes",
69 cl::desc("A textual description of the pass pipeline for optimizing"),
70 cl::Hidden);
71
Chris Lattner5a48a242002-07-23 18:12:22 +000072// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000073//
Chris Lattner02a16832003-05-22 20:13:16 +000074static cl::opt<std::string>
Eric Christophere64061f2009-08-21 23:29:40 +000075InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencer378f7d52006-08-18 06:34:30 +000076 cl::init("-"), cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000077
Chris Lattner02a16832003-05-22 20:13:16 +000078static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000079OutputFilename("o", cl::desc("Override output filename"),
Dan Gohmanb01aed12010-08-18 17:40:10 +000080 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000081
82static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000083Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000084
85static cl::opt<bool>
86PrintEachXForm("p", cl::desc("Print module after each transformation"));
87
88static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000089NoOutput("disable-output",
Gabor Greife16561c2007-07-05 17:07:56 +000090 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000091
92static cl::opt<bool>
Duncan Sands6f2ffce2009-10-14 20:01:39 +000093OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar6b3153b2009-09-05 11:34:53 +000094
95static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000096NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +000097
98static cl::opt<bool>
Reid Spencer22dbfb62007-02-02 14:46:29 +000099VerifyEach("verify-each", cl::desc("Verify after each transform"));
100
101static cl::opt<bool>
102StripDebug("strip-debug",
103 cl::desc("Strip debugger symbol info from translation unit"));
104
105static cl::opt<bool>
106DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
107
Eric Christophere64061f2009-08-21 23:29:40 +0000108static cl::opt<bool>
109DisableOptimizations("disable-opt",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000110 cl::desc("Do not run any optimization passes"));
111
Eric Christophere64061f2009-08-21 23:29:40 +0000112static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000113StandardLinkOpts("std-link-opts",
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000114 cl::desc("Include the standard link time optimizations"));
115
116static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000117OptLevelO1("O1",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000118 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000119
120static cl::opt<bool>
121OptLevelO2("O2",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000122 cl::desc("Optimization level 2. Similar to clang -O2"));
123
124static cl::opt<bool>
125OptLevelOs("Os",
126 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
127
128static cl::opt<bool>
129OptLevelOz("Oz",
130 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000131
132static cl::opt<bool>
133OptLevelO3("O3",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000134 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000135
Joe Groff1b738692012-04-17 23:05:48 +0000136static cl::opt<std::string>
137TargetTriple("mtriple", cl::desc("Override target triple for module"));
138
Devang Patel9966ccf2008-09-16 22:25:14 +0000139static cl::opt<bool>
140UnitAtATime("funit-at-a-time",
Rafael Espindola40bfd6d2014-08-21 19:22:24 +0000141 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000142 cl::init(true));
Devang Patel9966ccf2008-09-16 22:25:14 +0000143
144static cl::opt<bool>
Hal Finkel6d099042013-08-28 18:33:10 +0000145DisableLoopUnrolling("disable-loop-unrolling",
146 cl::desc("Disable loop unrolling in all relevant passes"),
147 cl::init(false));
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000148static cl::opt<bool>
149DisableLoopVectorization("disable-loop-vectorization",
150 cl::desc("Disable the loop vectorization pass"),
151 cl::init(false));
152
153static cl::opt<bool>
154DisableSLPVectorization("disable-slp-vectorization",
155 cl::desc("Disable the slp vectorization pass"),
156 cl::init(false));
157
Hal Finkel6d099042013-08-28 18:33:10 +0000158
159static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000160DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000161 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000162
163static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000164Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000165
Reid Spencerc8878ed2004-05-27 16:28:54 +0000166static cl::alias
167QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
168
Reid Spencer378f7d52006-08-18 06:34:30 +0000169static cl::opt<bool>
170AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
171
Devang Patel786a05e2010-12-07 00:33:43 +0000172static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000173PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000174 cl::desc("Print select breakpoints location for testing"));
175
Chris Lattnercfa19112009-10-22 00:44:10 +0000176static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000177DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-10-22 00:44:10 +0000178 cl::desc("data layout string to use if not specified by module"),
179 cl::value_desc("layout-string"), cl::init(""));
180
Reid Spencer378f7d52006-08-18 06:34:30 +0000181
Devang Patel786a05e2010-12-07 00:33:43 +0000182
Chandler Carruth30d69c22015-02-13 10:01:29 +0000183static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000184 // Add the pass to the pass manager...
185 PM.add(P);
186
187 // If we are verifying all of the intermediate steps, add the verifier...
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000188 if (VerifyEach) {
189 PM.add(createVerifierPass());
190 PM.add(createDebugInfoVerifierPass());
191 }
Reid Spencer22dbfb62007-02-02 14:46:29 +0000192}
193
Rafael Espindola40bfd6d2014-08-21 19:22:24 +0000194/// This routine adds optimization passes based on selected optimization level,
195/// OptLevel.
Devang Patel9966ccf2008-09-16 22:25:14 +0000196///
197/// OptLevel - Optimization Level
Chandler Carruth30d69c22015-02-13 10:01:29 +0000198static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
199 legacy::FunctionPassManager &FPM,
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000200 unsigned OptLevel, unsigned SizeLevel) {
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000201 FPM.add(createVerifierPass()); // Verify that input is correct
202 MPM.add(createDebugInfoVerifierPass()); // Verify that debug info is correct
Duncan Sands0f19e912011-12-07 17:14:20 +0000203
Chris Lattner3d70add2011-05-22 00:21:33 +0000204 PassManagerBuilder Builder;
205 Builder.OptLevel = OptLevel;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000206 Builder.SizeLevel = SizeLevel;
Devang Patel9966ccf2008-09-16 22:25:14 +0000207
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000208 if (DisableInline) {
209 // No inlining pass
Eli Friedmanb68fe252011-06-06 22:13:27 +0000210 } else if (OptLevel > 1) {
Eli Bendersky49f65652014-03-12 16:12:36 +0000211 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000212 } else {
Chris Lattner3d70add2011-05-22 00:21:33 +0000213 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000214 }
Chris Lattner3d70add2011-05-22 00:21:33 +0000215 Builder.DisableUnitAtATime = !UnitAtATime;
Hal Finkel6d099042013-08-28 18:33:10 +0000216 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
217 DisableLoopUnrolling : OptLevel == 0;
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000218
Renato Golin729a3ae2013-12-05 21:20:02 +0000219 // This is final, unless there is a #pragma vectorize enable
220 if (DisableLoopVectorization)
221 Builder.LoopVectorize = false;
222 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
223 else if (!Builder.LoopVectorize)
224 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
225
226 // When #pragma vectorize is on for SLP, do the same as above
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000227 Builder.SLPVectorize =
228 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
Hal Finkel6d099042013-08-28 18:33:10 +0000229
Chris Lattner3d70add2011-05-22 00:21:33 +0000230 Builder.populateFunctionPassManager(FPM);
231 Builder.populateModulePassManager(MPM);
Devang Patel9966ccf2008-09-16 22:25:14 +0000232}
233
Chandler Carruth30d69c22015-02-13 10:01:29 +0000234static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000235 PassManagerBuilder Builder;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000236 Builder.VerifyInput = true;
237 Builder.StripDebug = StripDebug;
238 if (DisableOptimizations)
239 Builder.OptLevel = 0;
240
Rafael Espindolae07caad2014-08-21 13:35:30 +0000241 if (!DisableInline)
242 Builder.Inliner = createFunctionInliningPass();
243 Builder.populateLTOPassManager(PM);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000244}
245
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000246//===----------------------------------------------------------------------===//
247// CodeGen-related helper functions.
248//
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000249
250CodeGenOpt::Level GetCodeGenOptLevel() {
251 if (OptLevelO1)
252 return CodeGenOpt::Less;
253 if (OptLevelO2)
254 return CodeGenOpt::Default;
255 if (OptLevelO3)
256 return CodeGenOpt::Aggressive;
257 return CodeGenOpt::None;
258}
259
260// Returns the TargetMachine instance or zero if no triple is provided.
Nadav Rotemb1615b12013-01-01 08:00:32 +0000261static TargetMachine* GetTargetMachine(Triple TheTriple) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000262 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000263 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
264 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000265 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000266 if (!TheTarget) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000267 return nullptr;
Eric Christopher13637e92013-04-15 07:31:37 +0000268 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000269
270 // Package up features to be passed to target/subtarget
271 std::string FeaturesStr;
272 if (MAttrs.size()) {
273 SubtargetFeatures Features;
274 for (unsigned i = 0; i != MAttrs.size(); ++i)
275 Features.AddFeature(MAttrs[i]);
276 FeaturesStr = Features.getString();
277 }
278
279 return TheTarget->createTargetMachine(TheTriple.getTriple(),
Eli Benderskyf0f21002014-02-19 17:09:35 +0000280 MCPU, FeaturesStr,
281 InitTargetOptionsFromCodeGenFlags(),
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000282 RelocModel, CMModel,
283 GetCodeGenOptLevel());
284}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000285
Sebastian Popa59005b2014-03-14 04:04:14 +0000286#ifdef LINK_POLLY_INTO_TOOLS
287namespace polly {
288void initializePollyPasses(llvm::PassRegistry &Registry);
289}
290#endif
291
Chris Lattner5a48a242002-07-23 18:12:22 +0000292//===----------------------------------------------------------------------===//
293// main for opt
294//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295int main(int argc, char **argv) {
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000296 sys::PrintStackTraceOnErrorSignal();
297 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000298
David Greeneed8a1de2010-01-05 01:30:32 +0000299 // Enable debug stream buffering.
300 EnableDebugBuffering = true;
301
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000302 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson19251ec2009-07-15 22:16:10 +0000303 LLVMContext &Context = getGlobalContext();
Andrew Trickb826ae82011-04-05 18:41:31 +0000304
Nadav Rotemac9a3442012-10-24 17:23:50 +0000305 InitializeAllTargets();
306 InitializeAllTargetMCs();
Tobias Grosser9190e0d2014-06-13 16:12:08 +0000307 InitializeAllAsmPrinters();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000308
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000309 // Initialize passes
310 PassRegistry &Registry = *PassRegistry::getPassRegistry();
311 initializeCore(Registry);
312 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000313 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000314 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000315 initializeIPO(Registry);
316 initializeAnalysis(Registry);
317 initializeIPA(Registry);
318 initializeTransformUtils(Registry);
319 initializeInstCombine(Registry);
320 initializeInstrumentation(Registry);
321 initializeTarget(Registry);
Quentin Colombeta3490842014-02-22 00:07:45 +0000322 // For codegen passes, only passes that do IR to IR transformation are
Tim Northover037f26f22014-04-17 18:22:47 +0000323 // supported.
Quentin Colombeta3490842014-02-22 00:07:45 +0000324 initializeCodeGenPreparePass(Registry);
Robin Morisset59c23cd2014-08-21 21:50:01 +0000325 initializeAtomicExpandPass(Registry);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000326 initializeRewriteSymbolsPass(Registry);
Reid Kleckner1185fce2015-01-29 00:41:44 +0000327 initializeWinEHPreparePass(Registry);
Reid Kleckner7bb07382015-02-18 23:17:41 +0000328 initializeDwarfEHPreparePass(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000329
Sebastian Popa59005b2014-03-14 04:04:14 +0000330#ifdef LINK_POLLY_INTO_TOOLS
331 polly::initializePollyPasses(Registry);
332#endif
333
Chris Lattnercc815262009-10-22 00:46:41 +0000334 cl::ParseCommandLineOptions(argc, argv,
335 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000336
Tobias Grosser083379f2010-12-02 20:35:16 +0000337 if (AnalyzeOnly && NoOutput) {
338 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
339 return 1;
340 }
341
Chris Lattnercc815262009-10-22 00:46:41 +0000342 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000343
Chris Lattnercc815262009-10-22 00:46:41 +0000344 // Load the input module...
Rafael Espindolad233b062014-08-26 17:29:46 +0000345 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
Eric Christophere64061f2009-08-21 23:29:40 +0000346
Craig Toppere2f17f02014-12-12 07:52:09 +0000347 if (!M) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000348 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000349 return 1;
350 }
351
Joe Groff1b738692012-04-17 23:05:48 +0000352 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000353 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000354 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000355
Chris Lattnercc815262009-10-22 00:46:41 +0000356 // Figure out what stream we are supposed to write to...
Ahmed Charles56440fd2014-03-06 05:51:42 +0000357 std::unique_ptr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000358 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000359 if (!OutputFilename.empty())
360 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000361 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000362 } else {
363 // Default to standard output.
364 if (OutputFilename.empty())
365 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000366
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000367 std::error_code EC;
368 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
369 if (EC) {
370 errs() << EC.message() << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000371 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000372 }
Chris Lattnercc815262009-10-22 00:46:41 +0000373 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000374
Chandler Carruthe0385522015-02-01 10:11:22 +0000375 Triple ModuleTriple(M->getTargetTriple());
376 TargetMachine *Machine = nullptr;
377 if (ModuleTriple.getArch())
378 Machine = GetTargetMachine(ModuleTriple);
379 std::unique_ptr<TargetMachine> TM(Machine);
380
Chris Lattnercc815262009-10-22 00:46:41 +0000381 // If the output is set to be emitted to standard out, and standard out is a
382 // console, print out a warning message and refuse to do it. We don't
383 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000384 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000385 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000386 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000387
Chandler Carruth949282e2014-01-13 03:08:40 +0000388 if (PassPipeline.getNumOccurrences() > 0) {
389 OutputKind OK = OK_NoOutput;
390 if (!NoOutput)
391 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
392
Chandler Carruth4d356312014-01-20 11:34:08 +0000393 VerifierKind VK = VK_VerifyInAndOut;
394 if (NoVerify)
395 VK = VK_NoVerifier;
396 else if (VerifyEach)
397 VK = VK_VerifyEachPass;
398
Chandler Carruth66445382014-01-11 08:16:35 +0000399 // The user has asked to use the new pass manager and provided a pipeline
400 // string. Hand off the rest of the functionality to the new code for that
401 // layer.
Chandler Carruthe0385522015-02-01 10:11:22 +0000402 return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(),
403 PassPipeline, OK, VK)
Chandler Carruth66445382014-01-11 08:16:35 +0000404 ? 0
405 : 1;
Chandler Carruth949282e2014-01-13 03:08:40 +0000406 }
Chandler Carruth66445382014-01-11 08:16:35 +0000407
Chris Lattnercc815262009-10-22 00:46:41 +0000408 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000409 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000410 //
Chandler Carruth30d69c22015-02-13 10:01:29 +0000411 legacy::PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000412
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000413 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chandler Carruthe0385522015-02-01 10:11:22 +0000414 TargetLibraryInfoImpl TLII(ModuleTriple);
Andrew Trickb826ae82011-04-05 18:41:31 +0000415
Chris Lattner1341df92011-02-18 22:34:03 +0000416 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
417 if (DisableSimplifyLibCalls)
Chandler Carruthc0291862015-01-24 02:06:09 +0000418 TLII.disableAllFunctions();
419 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
Andrew Trickb826ae82011-04-05 18:41:31 +0000420
Eric Christopher13637e92013-04-15 07:31:37 +0000421 // Add an appropriate DataLayout instance for this module.
Mehdi Amini46a43552015-03-04 18:43:29 +0000422 const DataLayout &DL = M->getDataLayout();
423 if (DL.isDefault() && !DefaultDataLayout.empty()) {
Rafael Espindola339430f2014-02-25 23:25:17 +0000424 M->setDataLayout(DefaultDataLayout);
Rafael Espindola339430f2014-02-25 23:25:17 +0000425 }
Eric Christopher13637e92013-04-15 07:31:37 +0000426
Chandler Carruth664e3542013-01-07 01:37:14 +0000427 // Add internal analysis passes from the target machine.
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000428 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
429 : TargetIRAnalysis()));
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000430
Chandler Carruth30d69c22015-02-13 10:01:29 +0000431 std::unique_ptr<legacy::FunctionPassManager> FPasses;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000432 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chandler Carruth30d69c22015-02-13 10:01:29 +0000433 FPasses.reset(new legacy::FunctionPassManager(M.get()));
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000434 FPasses->add(createTargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000435 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
Chris Lattnercc815262009-10-22 00:46:41 +0000436 }
Reid Spencer996ec722004-12-30 05:36:08 +0000437
Devang Patel786a05e2010-12-07 00:33:43 +0000438 if (PrintBreakpoints) {
439 // Default to standard output.
440 if (!Out) {
441 if (OutputFilename.empty())
442 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000443
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000444 std::error_code EC;
Craig Topperc5c52f42014-12-12 07:52:14 +0000445 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
446 sys::fs::F_None);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000447 if (EC) {
448 errs() << EC.message() << '\n';
Devang Patel786a05e2010-12-07 00:33:43 +0000449 return 1;
450 }
451 }
Eli Bendersky54dc2832014-02-12 16:48:02 +0000452 Passes.add(createBreakpointPrinter(Out->os()));
Devang Patel786a05e2010-12-07 00:33:43 +0000453 NoOutput = true;
454 }
455
Rafael Espindola11aaaee2014-10-16 20:00:02 +0000456 // If the -strip-debug command line option was specified, add it.
457 if (StripDebug)
Chris Lattnercc815262009-10-22 00:46:41 +0000458 addPass(Passes, createStripSymbolsPass(true));
Eric Christophere64061f2009-08-21 23:29:40 +0000459
Chris Lattnercc815262009-10-22 00:46:41 +0000460 // Create a new optimization pass for each one specified on the command line
461 for (unsigned i = 0; i < PassList.size(); ++i) {
Chris Lattnercc815262009-10-22 00:46:41 +0000462 if (StandardLinkOpts &&
463 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000464 AddStandardLinkPasses(Passes);
465 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000466 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000467
Chris Lattnercc815262009-10-22 00:46:41 +0000468 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000469 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000470 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000471 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000472
Chris Lattnercc815262009-10-22 00:46:41 +0000473 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000474 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000475 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000476 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000477
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000478 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
479 AddOptimizationPasses(Passes, *FPasses, 2, 1);
480 OptLevelOs = false;
481 }
482
483 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
484 AddOptimizationPasses(Passes, *FPasses, 2, 2);
485 OptLevelOz = false;
486 }
487
Chris Lattnercc815262009-10-22 00:46:41 +0000488 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000489 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000490 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000491 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000492
Chris Lattnercc815262009-10-22 00:46:41 +0000493 const PassInfo *PassInf = PassList[i];
Craig Toppere6cb63e2014-04-25 04:24:47 +0000494 Pass *P = nullptr;
Quentin Colombetdc0b2ea2014-01-16 21:44:34 +0000495 if (PassInf->getTargetMachineCtor())
496 P = PassInf->getTargetMachineCtor()(TM.get());
497 else if (PassInf->getNormalCtor())
Chris Lattnercc815262009-10-22 00:46:41 +0000498 P = PassInf->getNormalCtor()();
499 else
500 errs() << argv[0] << ": cannot create pass: "
501 << PassInf->getPassName() << "\n";
502 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000503 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000504 addPass(Passes, P);
505
506 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000507 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000508 case PT_BasicBlock:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000509 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000510 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000511 case PT_Region:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000512 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
Tobias Grosser23c83412010-10-20 01:54:44 +0000513 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000514 case PT_Loop:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000515 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000516 break;
517 case PT_Function:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000518 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000519 break;
520 case PT_CallGraphSCC:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000521 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000522 break;
523 default:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000524 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000525 break;
526 }
Chris Lattnercc815262009-10-22 00:46:41 +0000527 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000528 }
529
Chris Lattnercc815262009-10-22 00:46:41 +0000530 if (PrintEachXForm)
Chandler Carruth9d805132014-01-12 11:30:46 +0000531 Passes.add(createPrintModulePass(errs()));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000532 }
Chris Lattnercc815262009-10-22 00:46:41 +0000533
Chris Lattnercc815262009-10-22 00:46:41 +0000534 if (StandardLinkOpts) {
535 AddStandardLinkPasses(Passes);
536 StandardLinkOpts = false;
537 }
538
539 if (OptLevelO1)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000540 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000541
542 if (OptLevelO2)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000543 AddOptimizationPasses(Passes, *FPasses, 2, 0);
544
545 if (OptLevelOs)
546 AddOptimizationPasses(Passes, *FPasses, 2, 1);
547
548 if (OptLevelOz)
549 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000550
551 if (OptLevelO3)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000552 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000553
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000554 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000555 FPasses->doInitialization();
Craig Topperdbe2d2e2014-12-12 07:52:11 +0000556 for (Function &F : *M)
557 FPasses->run(F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000558 FPasses->doFinalization();
559 }
Chris Lattnercc815262009-10-22 00:46:41 +0000560
561 // Check that the module is well formed on completion of optimization
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000562 if (!NoVerify && !VerifyEach) {
Chris Lattnercc815262009-10-22 00:46:41 +0000563 Passes.add(createVerifierPass());
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000564 Passes.add(createDebugInfoVerifierPass());
565 }
Chris Lattnercc815262009-10-22 00:46:41 +0000566
Dan Gohman083330a2010-08-18 17:42:59 +0000567 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000568 if (!NoOutput && !AnalyzeOnly) {
569 if (OutputAssembly)
Chandler Carruth9d805132014-01-12 11:30:46 +0000570 Passes.add(createPrintModulePass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000571 else
Dan Gohmana2233f22010-09-01 14:20:41 +0000572 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000573 }
574
Andrew Trick12004012011-04-05 18:54:36 +0000575 // Before executing passes, print the final values of the LLVM options.
576 cl::PrintOptionValues();
577
Chris Lattnercc815262009-10-22 00:46:41 +0000578 // Now that we have all of the passes ready, run them.
Craig Toppere2f17f02014-12-12 07:52:09 +0000579 Passes.run(*M);
Chris Lattnercc815262009-10-22 00:46:41 +0000580
Dan Gohman268b0f42010-08-20 01:07:01 +0000581 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000582 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000583 Out->keep();
584
Chris Lattnercc815262009-10-22 00:46:41 +0000585 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586}