blob: 035629a8aa4b6b8d83b778767e76d88403fdf186 [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"
Keno Fischer04464cf2015-12-04 21:56:46 +000031#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000032#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000034#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000035#include "llvm/IRReader/IRReader.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000036#include "llvm/InitializePasses.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000037#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000038#include "llvm/LinkAllPasses.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/MC/SubtargetFeature.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"
Adam Nemeta62b7e12016-09-27 20:55:07 +000052#include "llvm/Support/YAMLTraits.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000053#include "llvm/Target/TargetMachine.h"
David Majnemer3d32b7e2016-07-28 21:04:31 +000054#include "llvm/Transforms/Coroutines.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000055#include "llvm/Transforms/IPO/AlwaysInliner.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000056#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Keno Fischer04464cf2015-12-04 21:56:46 +000057#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner5a48a242002-07-23 18:12:22 +000058#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000059#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000060using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000061using namespace opt_tool;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000062
Chris Lattner5a48a242002-07-23 18:12:22 +000063// The OptimizationList is automatically populated with registered Passes by the
64// PassNameParser.
65//
Chris Lattner8e5e13b2006-08-27 22:07:01 +000066static cl::list<const PassInfo*, bool, PassNameParser>
67PassList(cl::desc("Optimizations available:"));
Chris Lattner5a48a242002-07-23 18:12:22 +000068
Chandler Carruth66445382014-01-11 08:16:35 +000069// This flag specifies a textual description of the optimization pass pipeline
70// to run over the module. This flag switches opt to use the new pass manager
71// infrastructure, completely disabling all of the flags specific to the old
72// pass management.
73static cl::opt<std::string> PassPipeline(
74 "passes",
75 cl::desc("A textual description of the pass pipeline for optimizing"),
76 cl::Hidden);
77
Chris Lattner5a48a242002-07-23 18:12:22 +000078// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000079//
Chris Lattner02a16832003-05-22 20:13:16 +000080static cl::opt<std::string>
Eric Christophere64061f2009-08-21 23:29:40 +000081InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencer378f7d52006-08-18 06:34:30 +000082 cl::init("-"), cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000083
Chris Lattner02a16832003-05-22 20:13:16 +000084static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000085OutputFilename("o", cl::desc("Override output filename"),
Dan Gohmanb01aed12010-08-18 17:40:10 +000086 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000087
88static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000089Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000090
91static cl::opt<bool>
92PrintEachXForm("p", cl::desc("Print module after each transformation"));
93
94static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000095NoOutput("disable-output",
Gabor Greife16561c2007-07-05 17:07:56 +000096 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000097
98static cl::opt<bool>
Duncan Sands6f2ffce2009-10-14 20:01:39 +000099OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar6b3153b2009-09-05 11:34:53 +0000100
101static cl::opt<bool>
Vedant Kumarae22c582016-03-10 06:58:53 +0000102NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +0000103
104static cl::opt<bool>
Reid Spencer22dbfb62007-02-02 14:46:29 +0000105VerifyEach("verify-each", cl::desc("Verify after each transform"));
106
107static cl::opt<bool>
Teresa Johnson07c7e252016-04-19 15:48:30 +0000108 DisableDITypeMap("disable-debug-info-type-map",
109 cl::desc("Don't use a uniquing type map for debug info"));
110
111static cl::opt<bool>
Reid Spencer22dbfb62007-02-02 14:46:29 +0000112StripDebug("strip-debug",
113 cl::desc("Strip debugger symbol info from translation unit"));
114
115static cl::opt<bool>
116DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
117
Eric Christophere64061f2009-08-21 23:29:40 +0000118static cl::opt<bool>
119DisableOptimizations("disable-opt",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000120 cl::desc("Do not run any optimization passes"));
121
Eric Christophere64061f2009-08-21 23:29:40 +0000122static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000123StandardLinkOpts("std-link-opts",
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000124 cl::desc("Include the standard link time optimizations"));
125
126static cl::opt<bool>
Gor Nishanovf3bb3612016-08-05 16:27:33 +0000127OptLevelO0("O0",
128 cl::desc("Optimization level 0. Similar to clang -O0"));
129
130static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000131OptLevelO1("O1",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000132 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000133
134static cl::opt<bool>
135OptLevelO2("O2",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000136 cl::desc("Optimization level 2. Similar to clang -O2"));
137
138static cl::opt<bool>
139OptLevelOs("Os",
140 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
141
142static cl::opt<bool>
143OptLevelOz("Oz",
144 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000145
146static cl::opt<bool>
147OptLevelO3("O3",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000148 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000149
Tim Northoverb629c772016-04-18 21:48:55 +0000150static cl::opt<unsigned>
151CodeGenOptLevel("codegen-opt-level",
152 cl::desc("Override optimization level for codegen hooks"));
153
Joe Groff1b738692012-04-17 23:05:48 +0000154static cl::opt<std::string>
155TargetTriple("mtriple", cl::desc("Override target triple for module"));
156
Devang Patel9966ccf2008-09-16 22:25:14 +0000157static cl::opt<bool>
158UnitAtATime("funit-at-a-time",
Rafael Espindola40bfd6d2014-08-21 19:22:24 +0000159 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000160 cl::init(true));
Devang Patel9966ccf2008-09-16 22:25:14 +0000161
162static cl::opt<bool>
Hal Finkel6d099042013-08-28 18:33:10 +0000163DisableLoopUnrolling("disable-loop-unrolling",
164 cl::desc("Disable loop unrolling in all relevant passes"),
165 cl::init(false));
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000166static cl::opt<bool>
167DisableLoopVectorization("disable-loop-vectorization",
168 cl::desc("Disable the loop vectorization pass"),
169 cl::init(false));
170
171static cl::opt<bool>
172DisableSLPVectorization("disable-slp-vectorization",
173 cl::desc("Disable the slp vectorization pass"),
174 cl::init(false));
175
Mehdi Amini68da4262016-04-12 21:35:18 +0000176static cl::opt<bool> EmitSummaryIndex("module-summary",
177 cl::desc("Emit module summary index"),
178 cl::init(false));
179
180static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
181 cl::init(false));
Hal Finkel6d099042013-08-28 18:33:10 +0000182
183static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000184DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000185 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000186
187static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000188Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000189
Reid Spencerc8878ed2004-05-27 16:28:54 +0000190static cl::alias
191QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
192
Reid Spencer378f7d52006-08-18 06:34:30 +0000193static cl::opt<bool>
194AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
195
Devang Patel786a05e2010-12-07 00:33:43 +0000196static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000197PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000198 cl::desc("Print select breakpoints location for testing"));
199
Chris Lattnercfa19112009-10-22 00:44:10 +0000200static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000201DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-10-22 00:44:10 +0000202 cl::desc("data layout string to use if not specified by module"),
203 cl::value_desc("layout-string"), cl::init(""));
204
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000205static cl::opt<bool> PreserveBitcodeUseListOrder(
206 "preserve-bc-uselistorder",
207 cl::desc("Preserve use-list order when writing LLVM bitcode."),
208 cl::init(true), cl::Hidden);
Reid Spencer378f7d52006-08-18 06:34:30 +0000209
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000210static cl::opt<bool> PreserveAssemblyUseListOrder(
211 "preserve-ll-uselistorder",
212 cl::desc("Preserve use-list order when writing LLVM assembly."),
213 cl::init(false), cl::Hidden);
Devang Patel786a05e2010-12-07 00:33:43 +0000214
Keno Fischer04464cf2015-12-04 21:56:46 +0000215static cl::opt<bool>
216 RunTwice("run-twice",
217 cl::desc("Run all passes twice, re-using the same pass manager."),
218 cl::init(false), cl::Hidden);
219
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000220static cl::opt<bool> DiscardValueNames(
221 "discard-value-names",
222 cl::desc("Discard names from Value (other than GlobalValue)."),
223 cl::init(false), cl::Hidden);
224
David Majnemer3d32b7e2016-07-28 21:04:31 +0000225static cl::opt<bool> Coroutines(
226 "enable-coroutines",
227 cl::desc("Enable coroutine passes."),
228 cl::init(false), cl::Hidden);
229
Adam Nemetaad81602016-07-15 17:23:20 +0000230static cl::opt<bool> PassRemarksWithHotness(
231 "pass-remarks-with-hotness",
232 cl::desc("With PGO, include profile count in optimization remarks"),
233 cl::Hidden);
234
Adam Nemeta62b7e12016-09-27 20:55:07 +0000235static cl::opt<std::string>
236 RemarksFilename("pass-remarks-output",
237 cl::desc("YAML output filename for pass remarks"),
238 cl::value_desc("filename"));
239
Chandler Carruth30d69c22015-02-13 10:01:29 +0000240static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000241 // Add the pass to the pass manager...
242 PM.add(P);
243
244 // If we are verifying all of the intermediate steps, add the verifier...
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000245 if (VerifyEach)
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000246 PM.add(createVerifierPass());
Reid Spencer22dbfb62007-02-02 14:46:29 +0000247}
248
Rafael Espindola40bfd6d2014-08-21 19:22:24 +0000249/// This routine adds optimization passes based on selected optimization level,
250/// OptLevel.
Devang Patel9966ccf2008-09-16 22:25:14 +0000251///
252/// OptLevel - Optimization Level
Chandler Carruth30d69c22015-02-13 10:01:29 +0000253static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
254 legacy::FunctionPassManager &FPM,
Justin Lebar6e1089b2016-04-27 19:08:24 +0000255 TargetMachine *TM, unsigned OptLevel,
256 unsigned SizeLevel) {
Vedant Kumar37a1d622016-03-10 03:40:14 +0000257 if (!NoVerify || VerifyEach)
258 FPM.add(createVerifierPass()); // Verify that input is correct
Duncan Sands0f19e912011-12-07 17:14:20 +0000259
Chris Lattner3d70add2011-05-22 00:21:33 +0000260 PassManagerBuilder Builder;
261 Builder.OptLevel = OptLevel;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000262 Builder.SizeLevel = SizeLevel;
Devang Patel9966ccf2008-09-16 22:25:14 +0000263
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000264 if (DisableInline) {
265 // No inlining pass
Eli Friedmanb68fe252011-06-06 22:13:27 +0000266 } else if (OptLevel > 1) {
Eli Bendersky49f65652014-03-12 16:12:36 +0000267 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000268 } else {
Chandler Carruth67fc52f2016-08-17 02:56:20 +0000269 Builder.Inliner = createAlwaysInlinerLegacyPass();
Eli Friedmaneb0c52f2010-01-18 22:38:31 +0000270 }
Chris Lattner3d70add2011-05-22 00:21:33 +0000271 Builder.DisableUnitAtATime = !UnitAtATime;
Hal Finkel6d099042013-08-28 18:33:10 +0000272 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
273 DisableLoopUnrolling : OptLevel == 0;
Greg Bedwell1411aeb2013-10-09 08:55:27 +0000274
Renato Golin729a3ae2013-12-05 21:20:02 +0000275 // This is final, unless there is a #pragma vectorize enable
276 if (DisableLoopVectorization)
277 Builder.LoopVectorize = false;
278 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
279 else if (!Builder.LoopVectorize)
280 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
281
282 // When #pragma vectorize is on for SLP, do the same as above
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000283 Builder.SLPVectorize =
284 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
Hal Finkel6d099042013-08-28 18:33:10 +0000285
Justin Lebar6e1089b2016-04-27 19:08:24 +0000286 // Add target-specific passes that need to run as early as possible.
287 if (TM)
288 Builder.addExtension(
289 PassManagerBuilder::EP_EarlyAsPossible,
290 [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
291 TM->addEarlyAsPossiblePasses(PM);
292 });
293
David Majnemer3d32b7e2016-07-28 21:04:31 +0000294 if (Coroutines)
295 addCoroutinePassesToExtensionPoints(Builder);
296
Chris Lattner3d70add2011-05-22 00:21:33 +0000297 Builder.populateFunctionPassManager(FPM);
298 Builder.populateModulePassManager(MPM);
Devang Patel9966ccf2008-09-16 22:25:14 +0000299}
300
Chandler Carruth30d69c22015-02-13 10:01:29 +0000301static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000302 PassManagerBuilder Builder;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000303 Builder.VerifyInput = true;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000304 if (DisableOptimizations)
305 Builder.OptLevel = 0;
306
Rafael Espindolae07caad2014-08-21 13:35:30 +0000307 if (!DisableInline)
308 Builder.Inliner = createFunctionInliningPass();
309 Builder.populateLTOPassManager(PM);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000310}
311
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000312//===----------------------------------------------------------------------===//
313// CodeGen-related helper functions.
314//
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000315
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000316static CodeGenOpt::Level GetCodeGenOptLevel() {
Tim Northoverb629c772016-04-18 21:48:55 +0000317 if (CodeGenOptLevel.getNumOccurrences())
318 return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000319 if (OptLevelO1)
320 return CodeGenOpt::Less;
321 if (OptLevelO2)
322 return CodeGenOpt::Default;
323 if (OptLevelO3)
324 return CodeGenOpt::Aggressive;
325 return CodeGenOpt::None;
326}
327
328// Returns the TargetMachine instance or zero if no triple is provided.
Akira Hatanaka32b37602015-05-06 23:49:24 +0000329static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000330 StringRef FeaturesStr,
331 const TargetOptions &Options) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000332 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000333 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
334 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000335 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000336 if (!TheTarget) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000337 return nullptr;
Eric Christopher13637e92013-04-15 07:31:37 +0000338 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000339
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000340 return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
341 FeaturesStr, Options, getRelocModel(),
342 CMModel, GetCodeGenOptLevel());
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000343}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000344
Sebastian Popa59005b2014-03-14 04:04:14 +0000345#ifdef LINK_POLLY_INTO_TOOLS
346namespace polly {
347void initializePollyPasses(llvm::PassRegistry &Registry);
348}
349#endif
350
Chris Lattner5a48a242002-07-23 18:12:22 +0000351//===----------------------------------------------------------------------===//
352// main for opt
353//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354int main(int argc, char **argv) {
Richard Smith2ad6d482016-06-09 00:53:21 +0000355 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000356 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000357
David Greeneed8a1de2010-01-05 01:30:32 +0000358 // Enable debug stream buffering.
359 EnableDebugBuffering = true;
360
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000361 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Mehdi Amini03b42e42016-04-14 21:59:01 +0000362 LLVMContext Context;
Andrew Trickb826ae82011-04-05 18:41:31 +0000363
Nadav Rotemac9a3442012-10-24 17:23:50 +0000364 InitializeAllTargets();
365 InitializeAllTargetMCs();
Tobias Grosser9190e0d2014-06-13 16:12:08 +0000366 InitializeAllAsmPrinters();
Teresa Johnson3624bdf2016-11-14 17:12:32 +0000367 InitializeAllAsmParsers();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000368
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000369 // Initialize passes
370 PassRegistry &Registry = *PassRegistry::getPassRegistry();
371 initializeCore(Registry);
David Majnemer3d32b7e2016-07-28 21:04:31 +0000372 initializeCoroutines(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000373 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000374 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000375 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000376 initializeIPO(Registry);
377 initializeAnalysis(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000378 initializeTransformUtils(Registry);
379 initializeInstCombine(Registry);
380 initializeInstrumentation(Registry);
381 initializeTarget(Registry);
Quentin Colombeta3490842014-02-22 00:07:45 +0000382 // For codegen passes, only passes that do IR to IR transformation are
Tim Northover037f26f22014-04-17 18:22:47 +0000383 // supported.
Quentin Colombeta3490842014-02-22 00:07:45 +0000384 initializeCodeGenPreparePass(Registry);
Robin Morisset59c23cd2014-08-21 21:50:01 +0000385 initializeAtomicExpandPass(Registry);
Michael Kuperstein39feb622016-07-25 20:52:00 +0000386 initializeRewriteSymbolsLegacyPassPass(Registry);
Reid Kleckner1185fce2015-01-29 00:41:44 +0000387 initializeWinEHPreparePass(Registry);
Reid Kleckner7bb07382015-02-18 23:17:41 +0000388 initializeDwarfEHPreparePass(Registry);
Benjamin Kramer390c33c2016-01-27 16:53:42 +0000389 initializeSafeStackPass(Registry);
Reid Klecknerc16b1072015-07-09 21:48:40 +0000390 initializeSjLjEHPreparePass(Registry);
Michael Kuperstein82d5da52016-06-24 20:13:42 +0000391 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
Peter Collingbournefe12d0e2016-05-19 04:38:56 +0000392 initializeGlobalMergePass(Registry);
Matthew Simpson330a1252016-05-19 20:08:32 +0000393 initializeInterleavedAccessPass(Registry);
Hal Finkel40d7f5c2016-09-01 09:42:39 +0000394 initializeCountingFunctionInserterPass(Registry);
Wei Mi90d195a2016-07-08 03:32:49 +0000395 initializeUnreachableBlockElimLegacyPassPass(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000396
Sebastian Popa59005b2014-03-14 04:04:14 +0000397#ifdef LINK_POLLY_INTO_TOOLS
398 polly::initializePollyPasses(Registry);
399#endif
400
Chris Lattnercc815262009-10-22 00:46:41 +0000401 cl::ParseCommandLineOptions(argc, argv,
402 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000403
Tobias Grosser083379f2010-12-02 20:35:16 +0000404 if (AnalyzeOnly && NoOutput) {
405 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
406 return 1;
407 }
408
Chris Lattnercc815262009-10-22 00:46:41 +0000409 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000410
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000411 Context.setDiscardValueNames(DiscardValueNames);
Teresa Johnson07c7e252016-04-19 15:48:30 +0000412 if (!DisableDITypeMap)
413 Context.enableDebugTypeODRUniquing();
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000414
Adam Nemetaad81602016-07-15 17:23:20 +0000415 if (PassRemarksWithHotness)
416 Context.setDiagnosticHotnessRequested(true);
417
Adam Nemeta62b7e12016-09-27 20:55:07 +0000418 std::unique_ptr<tool_output_file> YamlFile;
419 if (RemarksFilename != "") {
420 std::error_code EC;
421 YamlFile = llvm::make_unique<tool_output_file>(RemarksFilename, EC,
422 sys::fs::F_None);
423 if (EC) {
424 errs() << EC.message() << '\n';
425 return 1;
426 }
Mehdi Amini6f408362016-11-19 18:19:41 +0000427 Context.setDiagnosticsOutputFile(
428 llvm::make_unique<yaml::Output>(YamlFile->os()));
Adam Nemeta62b7e12016-09-27 20:55:07 +0000429 }
430
Chris Lattnercc815262009-10-22 00:46:41 +0000431 // Load the input module...
Rafael Espindolad233b062014-08-26 17:29:46 +0000432 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
Eric Christophere64061f2009-08-21 23:29:40 +0000433
Craig Toppere2f17f02014-12-12 07:52:09 +0000434 if (!M) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000435 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000436 return 1;
437 }
438
Duncan P. N. Exon Smith49e6a702015-03-27 22:04:28 +0000439 // Strip debug info before running the verifier.
440 if (StripDebug)
441 StripDebugInfo(*M);
442
443 // Immediately run the verifier to catch any problems before starting up the
444 // pass pipelines. Otherwise we can crash on broken code during
445 // doInitialization().
446 if (!NoVerify && verifyModule(*M, &errs())) {
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000447 errs() << argv[0] << ": " << InputFilename
448 << ": error: input module is broken!\n";
Duncan P. N. Exon Smith49e6a702015-03-27 22:04:28 +0000449 return 1;
450 }
451
Joe Groff1b738692012-04-17 23:05:48 +0000452 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000453 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000454 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000455
Chris Lattnercc815262009-10-22 00:46:41 +0000456 // Figure out what stream we are supposed to write to...
Ahmed Charles56440fd2014-03-06 05:51:42 +0000457 std::unique_ptr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000458 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000459 if (!OutputFilename.empty())
460 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000461 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000462 } else {
463 // Default to standard output.
464 if (OutputFilename.empty())
465 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000466
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000467 std::error_code EC;
468 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
469 if (EC) {
470 errs() << EC.message() << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000471 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472 }
Chris Lattnercc815262009-10-22 00:46:41 +0000473 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000474
Chandler Carruthe0385522015-02-01 10:11:22 +0000475 Triple ModuleTriple(M->getTargetTriple());
Akira Hatanaka32b37602015-05-06 23:49:24 +0000476 std::string CPUStr, FeaturesStr;
Chandler Carruthe0385522015-02-01 10:11:22 +0000477 TargetMachine *Machine = nullptr;
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000478 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Akira Hatanakabd881832015-05-23 01:12:26 +0000479
Akira Hatanaka32b37602015-05-06 23:49:24 +0000480 if (ModuleTriple.getArch()) {
481 CPUStr = getCPUStr();
482 FeaturesStr = getFeaturesStr();
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000483 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
Akira Hatanaka32b37602015-05-06 23:49:24 +0000484 }
485
Chandler Carruthe0385522015-02-01 10:11:22 +0000486 std::unique_ptr<TargetMachine> TM(Machine);
487
Akira Hatanakae36505c2015-05-26 20:17:20 +0000488 // Override function attributes based on CPUStr, FeaturesStr, and command line
489 // flags.
490 setFunctionAttributes(CPUStr, FeaturesStr, *M);
Akira Hatanaka3058d0f2015-05-06 23:54:14 +0000491
Chris Lattnercc815262009-10-22 00:46:41 +0000492 // If the output is set to be emitted to standard out, and standard out is a
493 // console, print out a warning message and refuse to do it. We don't
494 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000495 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000496 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000497 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000498
Chandler Carruth949282e2014-01-13 03:08:40 +0000499 if (PassPipeline.getNumOccurrences() > 0) {
500 OutputKind OK = OK_NoOutput;
501 if (!NoOutput)
502 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
503
Chandler Carruth4d356312014-01-20 11:34:08 +0000504 VerifierKind VK = VK_VerifyInAndOut;
505 if (NoVerify)
506 VK = VK_NoVerifier;
507 else if (VerifyEach)
508 VK = VK_VerifyEachPass;
509
Chandler Carruth66445382014-01-11 08:16:35 +0000510 // The user has asked to use the new pass manager and provided a pipeline
511 // string. Hand off the rest of the functionality to the new code for that
512 // layer.
Davide Italianoc5d0a5c2016-09-07 00:48:47 +0000513 return runPassPipeline(argv[0], *M, TM.get(), Out.get(),
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000514 PassPipeline, OK, VK, PreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000515 PreserveBitcodeUseListOrder, EmitSummaryIndex,
516 EmitModuleHash)
Chandler Carruth66445382014-01-11 08:16:35 +0000517 ? 0
518 : 1;
Chandler Carruth949282e2014-01-13 03:08:40 +0000519 }
Chandler Carruth66445382014-01-11 08:16:35 +0000520
Chris Lattnercc815262009-10-22 00:46:41 +0000521 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000522 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000523 //
Chandler Carruth30d69c22015-02-13 10:01:29 +0000524 legacy::PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000525
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000526 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chandler Carruthe0385522015-02-01 10:11:22 +0000527 TargetLibraryInfoImpl TLII(ModuleTriple);
Andrew Trickb826ae82011-04-05 18:41:31 +0000528
Chris Lattner1341df92011-02-18 22:34:03 +0000529 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
530 if (DisableSimplifyLibCalls)
Chandler Carruthc0291862015-01-24 02:06:09 +0000531 TLII.disableAllFunctions();
532 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
Andrew Trickb826ae82011-04-05 18:41:31 +0000533
Eric Christopher13637e92013-04-15 07:31:37 +0000534 // Add an appropriate DataLayout instance for this module.
Mehdi Amini46a43552015-03-04 18:43:29 +0000535 const DataLayout &DL = M->getDataLayout();
536 if (DL.isDefault() && !DefaultDataLayout.empty()) {
Rafael Espindola339430f2014-02-25 23:25:17 +0000537 M->setDataLayout(DefaultDataLayout);
Rafael Espindola339430f2014-02-25 23:25:17 +0000538 }
Eric Christopher13637e92013-04-15 07:31:37 +0000539
Chandler Carruth664e3542013-01-07 01:37:14 +0000540 // Add internal analysis passes from the target machine.
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000541 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
542 : TargetIRAnalysis()));
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000543
Chandler Carruth30d69c22015-02-13 10:01:29 +0000544 std::unique_ptr<legacy::FunctionPassManager> FPasses;
Gor Nishanovf3bb3612016-08-05 16:27:33 +0000545 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
546 OptLevelO3) {
Chandler Carruth30d69c22015-02-13 10:01:29 +0000547 FPasses.reset(new legacy::FunctionPassManager(M.get()));
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000548 FPasses->add(createTargetTransformInfoWrapperPass(
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000549 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
Chris Lattnercc815262009-10-22 00:46:41 +0000550 }
Reid Spencer996ec722004-12-30 05:36:08 +0000551
Devang Patel786a05e2010-12-07 00:33:43 +0000552 if (PrintBreakpoints) {
553 // Default to standard output.
554 if (!Out) {
555 if (OutputFilename.empty())
556 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000557
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000558 std::error_code EC;
Craig Topperc5c52f42014-12-12 07:52:14 +0000559 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
560 sys::fs::F_None);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000561 if (EC) {
562 errs() << EC.message() << '\n';
Devang Patel786a05e2010-12-07 00:33:43 +0000563 return 1;
564 }
565 }
Eli Bendersky54dc2832014-02-12 16:48:02 +0000566 Passes.add(createBreakpointPrinter(Out->os()));
Devang Patel786a05e2010-12-07 00:33:43 +0000567 NoOutput = true;
568 }
569
Chris Lattnercc815262009-10-22 00:46:41 +0000570 // Create a new optimization pass for each one specified on the command line
571 for (unsigned i = 0; i < PassList.size(); ++i) {
Chris Lattnercc815262009-10-22 00:46:41 +0000572 if (StandardLinkOpts &&
573 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000574 AddStandardLinkPasses(Passes);
575 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000576 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000577
Gor Nishanovf3bb3612016-08-05 16:27:33 +0000578 if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
579 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
580 OptLevelO0 = false;
581 }
582
Chris Lattnercc815262009-10-22 00:46:41 +0000583 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Justin Lebar6e1089b2016-04-27 19:08:24 +0000584 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000585 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000586 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000587
Chris Lattnercc815262009-10-22 00:46:41 +0000588 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Justin Lebar6e1089b2016-04-27 19:08:24 +0000589 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000590 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000591 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000592
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000593 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
Justin Lebar6e1089b2016-04-27 19:08:24 +0000594 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000595 OptLevelOs = false;
596 }
597
598 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
Justin Lebar6e1089b2016-04-27 19:08:24 +0000599 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000600 OptLevelOz = false;
601 }
602
Chris Lattnercc815262009-10-22 00:46:41 +0000603 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Justin Lebar6e1089b2016-04-27 19:08:24 +0000604 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000605 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000606 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000607
Chris Lattnercc815262009-10-22 00:46:41 +0000608 const PassInfo *PassInf = PassList[i];
Craig Toppere6cb63e2014-04-25 04:24:47 +0000609 Pass *P = nullptr;
Quentin Colombetdc0b2ea2014-01-16 21:44:34 +0000610 if (PassInf->getTargetMachineCtor())
611 P = PassInf->getTargetMachineCtor()(TM.get());
612 else if (PassInf->getNormalCtor())
Chris Lattnercc815262009-10-22 00:46:41 +0000613 P = PassInf->getNormalCtor()();
614 else
615 errs() << argv[0] << ": cannot create pass: "
616 << PassInf->getPassName() << "\n";
617 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000618 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000619 addPass(Passes, P);
620
621 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000622 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000623 case PT_BasicBlock:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000624 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000625 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000626 case PT_Region:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000627 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
Tobias Grosser23c83412010-10-20 01:54:44 +0000628 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000629 case PT_Loop:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000630 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000631 break;
632 case PT_Function:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000633 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000634 break;
635 case PT_CallGraphSCC:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000636 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000637 break;
638 default:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000639 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000640 break;
641 }
Chris Lattnercc815262009-10-22 00:46:41 +0000642 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000643 }
644
Chris Lattnercc815262009-10-22 00:46:41 +0000645 if (PrintEachXForm)
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000646 Passes.add(
647 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000648 }
Chris Lattnercc815262009-10-22 00:46:41 +0000649
Chris Lattnercc815262009-10-22 00:46:41 +0000650 if (StandardLinkOpts) {
651 AddStandardLinkPasses(Passes);
652 StandardLinkOpts = false;
653 }
654
Gor Nishanovf3bb3612016-08-05 16:27:33 +0000655 if (OptLevelO0)
656 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
657
Chris Lattnercc815262009-10-22 00:46:41 +0000658 if (OptLevelO1)
Justin Lebar6e1089b2016-04-27 19:08:24 +0000659 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000660
661 if (OptLevelO2)
Justin Lebar6e1089b2016-04-27 19:08:24 +0000662 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000663
664 if (OptLevelOs)
Justin Lebar6e1089b2016-04-27 19:08:24 +0000665 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000666
667 if (OptLevelOz)
Justin Lebar6e1089b2016-04-27 19:08:24 +0000668 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000669
670 if (OptLevelO3)
Justin Lebar6e1089b2016-04-27 19:08:24 +0000671 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000672
Patrik Hagglund26daabe2016-06-15 10:32:00 +0000673 if (FPasses) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000674 FPasses->doInitialization();
Craig Topperdbe2d2e2014-12-12 07:52:11 +0000675 for (Function &F : *M)
676 FPasses->run(F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000677 FPasses->doFinalization();
678 }
Chris Lattnercc815262009-10-22 00:46:41 +0000679
680 // Check that the module is well formed on completion of optimization
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000681 if (!NoVerify && !VerifyEach)
Chris Lattnercc815262009-10-22 00:46:41 +0000682 Passes.add(createVerifierPass());
683
Keno Fischer04464cf2015-12-04 21:56:46 +0000684 // In run twice mode, we want to make sure the output is bit-by-bit
685 // equivalent if we run the pass manager again, so setup two buffers and
686 // a stream to write to them. Note that llc does something similar and it
687 // may be worth to abstract this out in the future.
688 SmallVector<char, 0> Buffer;
689 SmallVector<char, 0> CompileTwiceBuffer;
690 std::unique_ptr<raw_svector_ostream> BOS;
Keno Fischer38707c42015-12-05 00:06:37 +0000691 raw_ostream *OS = nullptr;
Keno Fischer04464cf2015-12-04 21:56:46 +0000692
Dan Gohman083330a2010-08-18 17:42:59 +0000693 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000694 if (!NoOutput && !AnalyzeOnly) {
Keno Fischer38707c42015-12-05 00:06:37 +0000695 assert(Out);
696 OS = &Out->os();
697 if (RunTwice) {
698 BOS = make_unique<raw_svector_ostream>(Buffer);
699 OS = BOS.get();
700 }
Mehdi Aminife6df262016-04-13 17:20:10 +0000701 if (OutputAssembly) {
702 if (EmitSummaryIndex)
703 report_fatal_error("Text output is incompatible with -module-summary");
704 if (EmitModuleHash)
705 report_fatal_error("Text output is incompatible with -module-hash");
Keno Fischer04464cf2015-12-04 21:56:46 +0000706 Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
Mehdi Aminife6df262016-04-13 17:20:10 +0000707 } else
Mehdi Amini68da4262016-04-12 21:35:18 +0000708 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
709 EmitSummaryIndex, EmitModuleHash));
Chris Lattnercc815262009-10-22 00:46:41 +0000710 }
711
Andrew Trick12004012011-04-05 18:54:36 +0000712 // Before executing passes, print the final values of the LLVM options.
713 cl::PrintOptionValues();
714
Keno Fischer04464cf2015-12-04 21:56:46 +0000715 // If requested, run all passes again with the same pass manager to catch
716 // bugs caused by persistent state in the passes
717 if (RunTwice) {
Keno Fischere54f58c2015-12-05 01:38:12 +0000718 std::unique_ptr<Module> M2(CloneModule(M.get()));
719 Passes.run(*M2);
720 CompileTwiceBuffer = Buffer;
721 Buffer.clear();
722 }
723
724 // Now that we have all of the passes ready, run them.
725 Passes.run(*M);
726
727 // Compare the two outputs and make sure they're the same
728 if (RunTwice) {
Keno Fischer38707c42015-12-05 00:06:37 +0000729 assert(Out);
Keno Fischer04464cf2015-12-04 21:56:46 +0000730 if (Buffer.size() != CompileTwiceBuffer.size() ||
731 (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
732 0)) {
733 errs() << "Running the pass manager twice changed the output.\n"
Keno Fischere54f58c2015-12-05 01:38:12 +0000734 "Writing the result of the second run to the specified output.\n"
Keno Fischer04464cf2015-12-04 21:56:46 +0000735 "To generate the one-run comparison binary, just run without\n"
736 "the compile-twice option\n";
737 Out->os() << BOS->str();
738 Out->keep();
Adam Nemeta62b7e12016-09-27 20:55:07 +0000739 if (YamlFile)
740 YamlFile->keep();
Keno Fischer04464cf2015-12-04 21:56:46 +0000741 return 1;
742 }
743 Out->os() << BOS->str();
744 }
745
Dan Gohman268b0f42010-08-20 01:07:01 +0000746 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000747 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000748 Out->keep();
749
Adam Nemeta62b7e12016-09-27 20:55:07 +0000750 if (YamlFile)
751 YamlFile->keep();
752
Chris Lattnercc815262009-10-22 00:46:41 +0000753 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000754}