blob: 6f3d8ca4156358ccbf81a1c5e27aca4fbf2d2cd0 [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"
Duncan P. N. Exon Smith49e6a702015-03-27 22:04:28 +000028#include "llvm/IR/DebugInfo.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000029#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000030#include "llvm/IR/LLVMContext.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000031#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000033#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000034#include "llvm/IRReader/IRReader.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000035#include "llvm/InitializePasses.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000036#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000037#include "llvm/LinkAllPasses.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000039#include "llvm/IR/LegacyPassManager.h"
David Greeneed8a1de2010-01-05 01:30:32 +000040#include "llvm/Support/Debug.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000041#include "llvm/Support/FileSystem.h"
Craig Topperd675e112015-04-01 05:32:04 +000042#include "llvm/Support/Host.h"
Chris Lattner76d46322006-12-06 01:18:01 +000043#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000044#include "llvm/Support/PluginLoader.h"
Chris Lattner4b2a6e22009-12-09 00:41:28 +000045#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000046#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000047#include "llvm/Support/SourceMgr.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000048#include "llvm/Support/SystemUtils.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000049#include "llvm/Support/TargetRegistry.h"
Nadav Rotemac9a3442012-10-24 17:23:50 +000050#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000051#include "llvm/Support/ToolOutputFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000052#include "llvm/Target/TargetMachine.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000053#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner5a48a242002-07-23 18:12:22 +000054#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000055#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000056using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000057using namespace opt_tool;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000058
Chris Lattner5a48a242002-07-23 18:12:22 +000059// The OptimizationList is automatically populated with registered Passes by the
60// PassNameParser.
61//
Chris Lattner8e5e13b2006-08-27 22:07:01 +000062static cl::list<const PassInfo*, bool, PassNameParser>
63PassList(cl::desc("Optimizations available:"));
Chris Lattner5a48a242002-07-23 18:12:22 +000064
Chandler Carruth66445382014-01-11 08:16:35 +000065// This flag specifies a textual description of the optimization pass pipeline
66// to run over the module. This flag switches opt to use the new pass manager
67// infrastructure, completely disabling all of the flags specific to the old
68// pass management.
69static cl::opt<std::string> PassPipeline(
70 "passes",
71 cl::desc("A textual description of the pass pipeline for optimizing"),
72 cl::Hidden);
73
Chris Lattner5a48a242002-07-23 18:12:22 +000074// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000075//
Chris Lattner02a16832003-05-22 20:13:16 +000076static cl::opt<std::string>
Eric Christophere64061f2009-08-21 23:29:40 +000077InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencer378f7d52006-08-18 06:34:30 +000078 cl::init("-"), cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000079
Chris Lattner02a16832003-05-22 20:13:16 +000080static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000081OutputFilename("o", cl::desc("Override output filename"),
Dan Gohmanb01aed12010-08-18 17:40:10 +000082 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000083
84static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000085Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000086
87static cl::opt<bool>
88PrintEachXForm("p", cl::desc("Print module after each transformation"));
89
90static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000091NoOutput("disable-output",
Gabor Greife16561c2007-07-05 17:07:56 +000092 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000093
94static cl::opt<bool>
Duncan Sands6f2ffce2009-10-14 20:01:39 +000095OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar6b3153b2009-09-05 11:34:53 +000096
97static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000098NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +000099
100static cl::opt<bool>
Reid Spencer22dbfb62007-02-02 14:46:29 +0000101VerifyEach("verify-each", cl::desc("Verify after each transform"));
102
103static cl::opt<bool>
104StripDebug("strip-debug",
105 cl::desc("Strip debugger symbol info from translation unit"));
106
107static cl::opt<bool>
108DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
109
Eric Christophere64061f2009-08-21 23:29:40 +0000110static cl::opt<bool>
111DisableOptimizations("disable-opt",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000112 cl::desc("Do not run any optimization passes"));
113
Eric Christophere64061f2009-08-21 23:29:40 +0000114static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000115StandardLinkOpts("std-link-opts",
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000116 cl::desc("Include the standard link time optimizations"));
117
118static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000119OptLevelO1("O1",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000120 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000121
122static cl::opt<bool>
123OptLevelO2("O2",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000124 cl::desc("Optimization level 2. Similar to clang -O2"));
125
126static cl::opt<bool>
127OptLevelOs("Os",
128 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
129
130static cl::opt<bool>
131OptLevelOz("Oz",
132 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000133
134static cl::opt<bool>
135OptLevelO3("O3",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000136 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000137
Joe Groff1b738692012-04-17 23:05:48 +0000138static cl::opt<std::string>
139TargetTriple("mtriple", cl::desc("Override target triple for module"));
140
Devang Patel9966ccf2008-09-16 22:25:14 +0000141static cl::opt<bool>
142UnitAtATime("funit-at-a-time",
Rafael Espindola40bfd6d2014-08-21 19:22:24 +0000143 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000144 cl::init(true));
Devang Patel9966ccf2008-09-16 22:25:14 +0000145
146static cl::opt<bool>
Hal Finkel6d099042013-08-28 18:33:10 +0000147DisableLoopUnrolling("disable-loop-unrolling",
148 cl::desc("Disable loop unrolling in all relevant passes"),
149 cl::init(false));
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000150static cl::opt<bool>
151DisableLoopVectorization("disable-loop-vectorization",
152 cl::desc("Disable the loop vectorization pass"),
153 cl::init(false));
154
155static cl::opt<bool>
156DisableSLPVectorization("disable-slp-vectorization",
157 cl::desc("Disable the slp vectorization pass"),
158 cl::init(false));
159
Hal Finkel6d099042013-08-28 18:33:10 +0000160
161static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000162DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000163 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000164
165static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000166Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000167
Reid Spencerc8878ed2004-05-27 16:28:54 +0000168static cl::alias
169QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
170
Reid Spencer378f7d52006-08-18 06:34:30 +0000171static cl::opt<bool>
172AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
173
Devang Patel786a05e2010-12-07 00:33:43 +0000174static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000175PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000176 cl::desc("Print select breakpoints location for testing"));
177
Chris Lattnercfa19112009-10-22 00:44:10 +0000178static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000179DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-10-22 00:44:10 +0000180 cl::desc("data layout string to use if not specified by module"),
181 cl::value_desc("layout-string"), cl::init(""));
182
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000183static cl::opt<bool> PreserveBitcodeUseListOrder(
184 "preserve-bc-uselistorder",
185 cl::desc("Preserve use-list order when writing LLVM bitcode."),
186 cl::init(true), cl::Hidden);
Reid Spencer378f7d52006-08-18 06:34:30 +0000187
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000188static cl::opt<bool> PreserveAssemblyUseListOrder(
189 "preserve-ll-uselistorder",
190 cl::desc("Preserve use-list order when writing LLVM assembly."),
191 cl::init(false), cl::Hidden);
Devang Patel786a05e2010-12-07 00:33:43 +0000192
Chandler Carruth30d69c22015-02-13 10:01:29 +0000193static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000194 // Add the pass to the pass manager...
195 PM.add(P);
196
197 // If we are verifying all of the intermediate steps, add the verifier...
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000198 if (VerifyEach)
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000199 PM.add(createVerifierPass());
Reid Spencer22dbfb62007-02-02 14:46:29 +0000200}
201
Rafael Espindola40bfd6d2014-08-21 19:22:24 +0000202/// This routine adds optimization passes based on selected optimization level,
203/// OptLevel.
Devang Patel9966ccf2008-09-16 22:25:14 +0000204///
205/// OptLevel - Optimization Level
Chandler Carruth30d69c22015-02-13 10:01:29 +0000206static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
207 legacy::FunctionPassManager &FPM,
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000208 unsigned OptLevel, unsigned SizeLevel) {
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000209 FPM.add(createVerifierPass()); // Verify that input is correct
Duncan Sands0f19e912011-12-07 17:14:20 +0000210
Chris Lattner3d70add2011-05-22 00:21:33 +0000211 PassManagerBuilder Builder;
212 Builder.OptLevel = OptLevel;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000213 Builder.SizeLevel = SizeLevel;
Devang Patel9966ccf2008-09-16 22:25:14 +0000214
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000215 if (DisableInline) {
216 // No inlining pass
Eli Friedmanb68fe252011-06-06 22:13:27 +0000217 } else if (OptLevel > 1) {
Eli Bendersky49f65652014-03-12 16:12:36 +0000218 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000219 } else {
Chris Lattner3d70add2011-05-22 00:21:33 +0000220 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000221 }
Chris Lattner3d70add2011-05-22 00:21:33 +0000222 Builder.DisableUnitAtATime = !UnitAtATime;
Hal Finkel6d099042013-08-28 18:33:10 +0000223 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
224 DisableLoopUnrolling : OptLevel == 0;
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000225
Renato Golin729a3ae2013-12-05 21:20:02 +0000226 // This is final, unless there is a #pragma vectorize enable
227 if (DisableLoopVectorization)
228 Builder.LoopVectorize = false;
229 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
230 else if (!Builder.LoopVectorize)
231 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
232
233 // When #pragma vectorize is on for SLP, do the same as above
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000234 Builder.SLPVectorize =
235 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
Hal Finkel6d099042013-08-28 18:33:10 +0000236
Chris Lattner3d70add2011-05-22 00:21:33 +0000237 Builder.populateFunctionPassManager(FPM);
238 Builder.populateModulePassManager(MPM);
Devang Patel9966ccf2008-09-16 22:25:14 +0000239}
240
Chandler Carruth30d69c22015-02-13 10:01:29 +0000241static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000242 PassManagerBuilder Builder;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000243 Builder.VerifyInput = true;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000244 if (DisableOptimizations)
245 Builder.OptLevel = 0;
246
Rafael Espindolae07caad2014-08-21 13:35:30 +0000247 if (!DisableInline)
248 Builder.Inliner = createFunctionInliningPass();
249 Builder.populateLTOPassManager(PM);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000250}
251
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000252//===----------------------------------------------------------------------===//
253// CodeGen-related helper functions.
254//
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000255
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000256static CodeGenOpt::Level GetCodeGenOptLevel() {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000257 if (OptLevelO1)
258 return CodeGenOpt::Less;
259 if (OptLevelO2)
260 return CodeGenOpt::Default;
261 if (OptLevelO3)
262 return CodeGenOpt::Aggressive;
263 return CodeGenOpt::None;
264}
265
266// Returns the TargetMachine instance or zero if no triple is provided.
Akira Hatanaka32b37602015-05-06 23:49:24 +0000267static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000268 StringRef FeaturesStr,
269 const TargetOptions &Options) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000270 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000271 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
272 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000273 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000274 if (!TheTarget) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000275 return nullptr;
Eric Christopher13637e92013-04-15 07:31:37 +0000276 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000277
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000278 return TheTarget->createTargetMachine(TheTriple.getTriple(),
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000279 CPUStr, FeaturesStr, Options,
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000280 RelocModel, CMModel,
281 GetCodeGenOptLevel());
282}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000283
Sebastian Popa59005b2014-03-14 04:04:14 +0000284#ifdef LINK_POLLY_INTO_TOOLS
285namespace polly {
286void initializePollyPasses(llvm::PassRegistry &Registry);
287}
288#endif
289
Chris Lattner5a48a242002-07-23 18:12:22 +0000290//===----------------------------------------------------------------------===//
291// main for opt
292//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293int main(int argc, char **argv) {
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000294 sys::PrintStackTraceOnErrorSignal();
295 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000296
David Greeneed8a1de2010-01-05 01:30:32 +0000297 // Enable debug stream buffering.
298 EnableDebugBuffering = true;
299
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000300 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson19251ec2009-07-15 22:16:10 +0000301 LLVMContext &Context = getGlobalContext();
Andrew Trickb826ae82011-04-05 18:41:31 +0000302
Nadav Rotemac9a3442012-10-24 17:23:50 +0000303 InitializeAllTargets();
304 InitializeAllTargetMCs();
Tobias Grosser9190e0d2014-06-13 16:12:08 +0000305 InitializeAllAsmPrinters();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000306
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000307 // Initialize passes
308 PassRegistry &Registry = *PassRegistry::getPassRegistry();
309 initializeCore(Registry);
310 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000311 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000312 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000313 initializeIPO(Registry);
314 initializeAnalysis(Registry);
315 initializeIPA(Registry);
316 initializeTransformUtils(Registry);
317 initializeInstCombine(Registry);
318 initializeInstrumentation(Registry);
319 initializeTarget(Registry);
Quentin Colombeta3490842014-02-22 00:07:45 +0000320 // For codegen passes, only passes that do IR to IR transformation are
Tim Northover037f26f22014-04-17 18:22:47 +0000321 // supported.
Quentin Colombeta3490842014-02-22 00:07:45 +0000322 initializeCodeGenPreparePass(Registry);
Robin Morisset59c23cd2014-08-21 21:50:01 +0000323 initializeAtomicExpandPass(Registry);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000324 initializeRewriteSymbolsPass(Registry);
Reid Kleckner1185fce2015-01-29 00:41:44 +0000325 initializeWinEHPreparePass(Registry);
Reid Kleckner7bb07382015-02-18 23:17:41 +0000326 initializeDwarfEHPreparePass(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000327
Sebastian Popa59005b2014-03-14 04:04:14 +0000328#ifdef LINK_POLLY_INTO_TOOLS
329 polly::initializePollyPasses(Registry);
330#endif
331
Chris Lattnercc815262009-10-22 00:46:41 +0000332 cl::ParseCommandLineOptions(argc, argv,
333 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000334
Tobias Grosser083379f2010-12-02 20:35:16 +0000335 if (AnalyzeOnly && NoOutput) {
336 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
337 return 1;
338 }
339
Chris Lattnercc815262009-10-22 00:46:41 +0000340 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000341
Chris Lattnercc815262009-10-22 00:46:41 +0000342 // Load the input module...
Rafael Espindolad233b062014-08-26 17:29:46 +0000343 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
Eric Christophere64061f2009-08-21 23:29:40 +0000344
Craig Toppere2f17f02014-12-12 07:52:09 +0000345 if (!M) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000346 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000347 return 1;
348 }
349
Duncan P. N. Exon Smith49e6a702015-03-27 22:04:28 +0000350 // Strip debug info before running the verifier.
351 if (StripDebug)
352 StripDebugInfo(*M);
353
354 // Immediately run the verifier to catch any problems before starting up the
355 // pass pipelines. Otherwise we can crash on broken code during
356 // doInitialization().
357 if (!NoVerify && verifyModule(*M, &errs())) {
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000358 errs() << argv[0] << ": " << InputFilename
359 << ": error: input module is broken!\n";
Duncan P. N. Exon Smith49e6a702015-03-27 22:04:28 +0000360 return 1;
361 }
362
Joe Groff1b738692012-04-17 23:05:48 +0000363 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000364 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000365 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000366
Chris Lattnercc815262009-10-22 00:46:41 +0000367 // Figure out what stream we are supposed to write to...
Ahmed Charles56440fd2014-03-06 05:51:42 +0000368 std::unique_ptr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000369 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000370 if (!OutputFilename.empty())
371 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000372 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000373 } else {
374 // Default to standard output.
375 if (OutputFilename.empty())
376 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000377
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000378 std::error_code EC;
379 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
380 if (EC) {
381 errs() << EC.message() << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000382 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000383 }
Chris Lattnercc815262009-10-22 00:46:41 +0000384 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000385
Chandler Carruthe0385522015-02-01 10:11:22 +0000386 Triple ModuleTriple(M->getTargetTriple());
Akira Hatanaka32b37602015-05-06 23:49:24 +0000387 std::string CPUStr, FeaturesStr;
Chandler Carruthe0385522015-02-01 10:11:22 +0000388 TargetMachine *Machine = nullptr;
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000389 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Akira Hatanakabd881832015-05-23 01:12:26 +0000390
Akira Hatanaka32b37602015-05-06 23:49:24 +0000391 if (ModuleTriple.getArch()) {
392 CPUStr = getCPUStr();
393 FeaturesStr = getFeaturesStr();
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000394 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
Akira Hatanaka32b37602015-05-06 23:49:24 +0000395 }
396
Chandler Carruthe0385522015-02-01 10:11:22 +0000397 std::unique_ptr<TargetMachine> TM(Machine);
398
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000399 // Override function attributes based on CPUStr, FeaturesStr, and Options.
400 // Pass AlwaysRecordAttrs=false as we want to override an attribute only when
401 // the corresponding cl::opt has been provided on opt's command line.
402 setFunctionAttributes(CPUStr, FeaturesStr, Options, *M,
403 /* AlwaysRecordAttrs */ false);
Akira Hatanaka3058d0f2015-05-06 23:54:14 +0000404
Chris Lattnercc815262009-10-22 00:46:41 +0000405 // If the output is set to be emitted to standard out, and standard out is a
406 // console, print out a warning message and refuse to do it. We don't
407 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000408 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000409 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000410 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000411
Chandler Carruth949282e2014-01-13 03:08:40 +0000412 if (PassPipeline.getNumOccurrences() > 0) {
413 OutputKind OK = OK_NoOutput;
414 if (!NoOutput)
415 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
416
Chandler Carruth4d356312014-01-20 11:34:08 +0000417 VerifierKind VK = VK_VerifyInAndOut;
418 if (NoVerify)
419 VK = VK_NoVerifier;
420 else if (VerifyEach)
421 VK = VK_VerifyEachPass;
422
Chandler Carruth66445382014-01-11 08:16:35 +0000423 // The user has asked to use the new pass manager and provided a pipeline
424 // string. Hand off the rest of the functionality to the new code for that
425 // layer.
Chandler Carruthe0385522015-02-01 10:11:22 +0000426 return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(),
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000427 PassPipeline, OK, VK, PreserveAssemblyUseListOrder,
428 PreserveBitcodeUseListOrder)
Chandler Carruth66445382014-01-11 08:16:35 +0000429 ? 0
430 : 1;
Chandler Carruth949282e2014-01-13 03:08:40 +0000431 }
Chandler Carruth66445382014-01-11 08:16:35 +0000432
Chris Lattnercc815262009-10-22 00:46:41 +0000433 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000434 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000435 //
Chandler Carruth30d69c22015-02-13 10:01:29 +0000436 legacy::PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000437
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000438 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chandler Carruthe0385522015-02-01 10:11:22 +0000439 TargetLibraryInfoImpl TLII(ModuleTriple);
Andrew Trickb826ae82011-04-05 18:41:31 +0000440
Chris Lattner1341df92011-02-18 22:34:03 +0000441 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
442 if (DisableSimplifyLibCalls)
Chandler Carruthc0291862015-01-24 02:06:09 +0000443 TLII.disableAllFunctions();
444 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
Andrew Trickb826ae82011-04-05 18:41:31 +0000445
Eric Christopher13637e92013-04-15 07:31:37 +0000446 // Add an appropriate DataLayout instance for this module.
Mehdi Amini46a43552015-03-04 18:43:29 +0000447 const DataLayout &DL = M->getDataLayout();
448 if (DL.isDefault() && !DefaultDataLayout.empty()) {
Rafael Espindola339430f2014-02-25 23:25:17 +0000449 M->setDataLayout(DefaultDataLayout);
Rafael Espindola339430f2014-02-25 23:25:17 +0000450 }
Eric Christopher13637e92013-04-15 07:31:37 +0000451
Chandler Carruth664e3542013-01-07 01:37:14 +0000452 // Add internal analysis passes from the target machine.
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000453 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
454 : TargetIRAnalysis()));
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000455
Chandler Carruth30d69c22015-02-13 10:01:29 +0000456 std::unique_ptr<legacy::FunctionPassManager> FPasses;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000457 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chandler Carruth30d69c22015-02-13 10:01:29 +0000458 FPasses.reset(new legacy::FunctionPassManager(M.get()));
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000459 FPasses->add(createTargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000460 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
Chris Lattnercc815262009-10-22 00:46:41 +0000461 }
Reid Spencer996ec722004-12-30 05:36:08 +0000462
Devang Patel786a05e2010-12-07 00:33:43 +0000463 if (PrintBreakpoints) {
464 // Default to standard output.
465 if (!Out) {
466 if (OutputFilename.empty())
467 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000468
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000469 std::error_code EC;
Craig Topperc5c52f42014-12-12 07:52:14 +0000470 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
471 sys::fs::F_None);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000472 if (EC) {
473 errs() << EC.message() << '\n';
Devang Patel786a05e2010-12-07 00:33:43 +0000474 return 1;
475 }
476 }
Eli Bendersky54dc2832014-02-12 16:48:02 +0000477 Passes.add(createBreakpointPrinter(Out->os()));
Devang Patel786a05e2010-12-07 00:33:43 +0000478 NoOutput = true;
479 }
480
Chris Lattnercc815262009-10-22 00:46:41 +0000481 // Create a new optimization pass for each one specified on the command line
482 for (unsigned i = 0; i < PassList.size(); ++i) {
Chris Lattnercc815262009-10-22 00:46:41 +0000483 if (StandardLinkOpts &&
484 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000485 AddStandardLinkPasses(Passes);
486 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000487 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000488
Chris Lattnercc815262009-10-22 00:46:41 +0000489 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000490 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000491 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000492 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000493
Chris Lattnercc815262009-10-22 00:46:41 +0000494 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000495 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000496 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000497 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000498
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000499 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
500 AddOptimizationPasses(Passes, *FPasses, 2, 1);
501 OptLevelOs = false;
502 }
503
504 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
505 AddOptimizationPasses(Passes, *FPasses, 2, 2);
506 OptLevelOz = false;
507 }
508
Chris Lattnercc815262009-10-22 00:46:41 +0000509 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000510 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000511 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000512 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000513
Chris Lattnercc815262009-10-22 00:46:41 +0000514 const PassInfo *PassInf = PassList[i];
Craig Toppere6cb63e2014-04-25 04:24:47 +0000515 Pass *P = nullptr;
Quentin Colombetdc0b2ea2014-01-16 21:44:34 +0000516 if (PassInf->getTargetMachineCtor())
517 P = PassInf->getTargetMachineCtor()(TM.get());
518 else if (PassInf->getNormalCtor())
Chris Lattnercc815262009-10-22 00:46:41 +0000519 P = PassInf->getNormalCtor()();
520 else
521 errs() << argv[0] << ": cannot create pass: "
522 << PassInf->getPassName() << "\n";
523 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000524 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000525 addPass(Passes, P);
526
527 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000528 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000529 case PT_BasicBlock:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000530 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000531 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000532 case PT_Region:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000533 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
Tobias Grosser23c83412010-10-20 01:54:44 +0000534 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000535 case PT_Loop:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000536 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000537 break;
538 case PT_Function:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000539 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000540 break;
541 case PT_CallGraphSCC:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000542 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000543 break;
544 default:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000545 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000546 break;
547 }
Chris Lattnercc815262009-10-22 00:46:41 +0000548 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000549 }
550
Chris Lattnercc815262009-10-22 00:46:41 +0000551 if (PrintEachXForm)
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000552 Passes.add(
553 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000554 }
Chris Lattnercc815262009-10-22 00:46:41 +0000555
Chris Lattnercc815262009-10-22 00:46:41 +0000556 if (StandardLinkOpts) {
557 AddStandardLinkPasses(Passes);
558 StandardLinkOpts = false;
559 }
560
561 if (OptLevelO1)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000562 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000563
564 if (OptLevelO2)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000565 AddOptimizationPasses(Passes, *FPasses, 2, 0);
566
567 if (OptLevelOs)
568 AddOptimizationPasses(Passes, *FPasses, 2, 1);
569
570 if (OptLevelOz)
571 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000572
573 if (OptLevelO3)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000574 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000575
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000576 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000577 FPasses->doInitialization();
Craig Topperdbe2d2e2014-12-12 07:52:11 +0000578 for (Function &F : *M)
579 FPasses->run(F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000580 FPasses->doFinalization();
581 }
Chris Lattnercc815262009-10-22 00:46:41 +0000582
583 // Check that the module is well formed on completion of optimization
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000584 if (!NoVerify && !VerifyEach)
Chris Lattnercc815262009-10-22 00:46:41 +0000585 Passes.add(createVerifierPass());
586
Dan Gohman083330a2010-08-18 17:42:59 +0000587 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000588 if (!NoOutput && !AnalyzeOnly) {
589 if (OutputAssembly)
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000590 Passes.add(
591 createPrintModulePass(Out->os(), "", PreserveAssemblyUseListOrder));
Chris Lattnercc815262009-10-22 00:46:41 +0000592 else
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000593 Passes.add(
594 createBitcodeWriterPass(Out->os(), PreserveBitcodeUseListOrder));
Chris Lattnercc815262009-10-22 00:46:41 +0000595 }
596
Andrew Trick12004012011-04-05 18:54:36 +0000597 // Before executing passes, print the final values of the LLVM options.
598 cl::PrintOptionValues();
599
Chris Lattnercc815262009-10-22 00:46:41 +0000600 // Now that we have all of the passes ready, run them.
Craig Toppere2f17f02014-12-12 07:52:09 +0000601 Passes.run(*M);
Chris Lattnercc815262009-10-22 00:46:41 +0000602
Dan Gohman268b0f42010-08-20 01:07:01 +0000603 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000604 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000605 Out->keep();
606
Chris Lattnercc815262009-10-22 00:46:41 +0000607 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000608}