blob: 6ba6340040b35d109e9c4c937a7231bf1d708bf2 [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 Carruthb7bdfd62014-01-13 07:38:24 +000023#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/CodeGen/CommandFlags.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DataLayout.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000026#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000027#include "llvm/IR/LLVMContext.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000028#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000030#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000031#include "llvm/IRReader/IRReader.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000032#include "llvm/InitializePasses.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000033#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000034#include "llvm/LinkAllPasses.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000035#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000036#include "llvm/PassManager.h"
David Greeneed8a1de2010-01-05 01:30:32 +000037#include "llvm/Support/Debug.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000038#include "llvm/Support/FileSystem.h"
Chris Lattner76d46322006-12-06 01:18:01 +000039#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000040#include "llvm/Support/PluginLoader.h"
Chris Lattner4b2a6e22009-12-09 00:41:28 +000041#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000042#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000043#include "llvm/Support/SourceMgr.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000044#include "llvm/Support/SystemUtils.h"
Nadav Rotem5dc203e2012-10-18 23:22:48 +000045#include "llvm/Support/TargetRegistry.h"
Nadav Rotemac9a3442012-10-24 17:23:50 +000046#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000047#include "llvm/Support/ToolOutputFile.h"
48#include "llvm/Target/TargetLibraryInfo.h"
49#include "llvm/Target/TargetMachine.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000050#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner5a48a242002-07-23 18:12:22 +000051#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000052#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000053using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000054using namespace opt_tool;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000055
Chris Lattner5a48a242002-07-23 18:12:22 +000056// The OptimizationList is automatically populated with registered Passes by the
57// PassNameParser.
58//
Chris Lattner8e5e13b2006-08-27 22:07:01 +000059static cl::list<const PassInfo*, bool, PassNameParser>
60PassList(cl::desc("Optimizations available:"));
Chris Lattner5a48a242002-07-23 18:12:22 +000061
Chandler Carruth66445382014-01-11 08:16:35 +000062// This flag specifies a textual description of the optimization pass pipeline
63// to run over the module. This flag switches opt to use the new pass manager
64// infrastructure, completely disabling all of the flags specific to the old
65// pass management.
66static cl::opt<std::string> PassPipeline(
67 "passes",
68 cl::desc("A textual description of the pass pipeline for optimizing"),
69 cl::Hidden);
70
Chris Lattner5a48a242002-07-23 18:12:22 +000071// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000072//
Chris Lattner02a16832003-05-22 20:13:16 +000073static cl::opt<std::string>
Eric Christophere64061f2009-08-21 23:29:40 +000074InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencer378f7d52006-08-18 06:34:30 +000075 cl::init("-"), cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000076
Chris Lattner02a16832003-05-22 20:13:16 +000077static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000078OutputFilename("o", cl::desc("Override output filename"),
Dan Gohmanb01aed12010-08-18 17:40:10 +000079 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000080
81static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000082Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000083
84static cl::opt<bool>
85PrintEachXForm("p", cl::desc("Print module after each transformation"));
86
87static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000088NoOutput("disable-output",
Gabor Greife16561c2007-07-05 17:07:56 +000089 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000090
91static cl::opt<bool>
Duncan Sands6f2ffce2009-10-14 20:01:39 +000092OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar6b3153b2009-09-05 11:34:53 +000093
94static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000095NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +000096
97static cl::opt<bool>
Reid Spencer22dbfb62007-02-02 14:46:29 +000098VerifyEach("verify-each", cl::desc("Verify after each transform"));
99
100static cl::opt<bool>
101StripDebug("strip-debug",
102 cl::desc("Strip debugger symbol info from translation unit"));
103
104static cl::opt<bool>
105DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
106
Eric Christophere64061f2009-08-21 23:29:40 +0000107static cl::opt<bool>
108DisableOptimizations("disable-opt",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000109 cl::desc("Do not run any optimization passes"));
110
Eric Christophere64061f2009-08-21 23:29:40 +0000111static cl::opt<bool>
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000112DisableInternalize("disable-internalize",
113 cl::desc("Do not mark all symbols as internal"));
114
Reid Spencer22dbfb62007-02-02 14:46:29 +0000115static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000116StandardCompileOpts("std-compile-opts",
Reid Spencer22dbfb62007-02-02 14:46:29 +0000117 cl::desc("Include the standard compile time optimizations"));
118
119static cl::opt<bool>
Eric Christophere64061f2009-08-21 23:29:40 +0000120StandardLinkOpts("std-link-opts",
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000121 cl::desc("Include the standard link time optimizations"));
122
123static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000124OptLevelO1("O1",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000125 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000126
127static cl::opt<bool>
128OptLevelO2("O2",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000129 cl::desc("Optimization level 2. Similar to clang -O2"));
130
131static cl::opt<bool>
132OptLevelOs("Os",
133 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
134
135static cl::opt<bool>
136OptLevelOz("Oz",
137 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000138
139static cl::opt<bool>
140OptLevelO3("O3",
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000141 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000142
Joe Groff1b738692012-04-17 23:05:48 +0000143static cl::opt<std::string>
144TargetTriple("mtriple", cl::desc("Override target triple for module"));
145
Devang Patel9966ccf2008-09-16 22:25:14 +0000146static cl::opt<bool>
147UnitAtATime("funit-at-a-time",
Eric Christopherc8f625b2009-08-21 23:30:30 +0000148 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000149 cl::init(true));
Devang Patel9966ccf2008-09-16 22:25:14 +0000150
151static cl::opt<bool>
Hal Finkel6d099042013-08-28 18:33:10 +0000152DisableLoopUnrolling("disable-loop-unrolling",
153 cl::desc("Disable loop unrolling in all relevant passes"),
154 cl::init(false));
Arnold Schwaighofer46db7252013-12-03 16:33:06 +0000155static cl::opt<bool>
156DisableLoopVectorization("disable-loop-vectorization",
157 cl::desc("Disable the loop vectorization pass"),
158 cl::init(false));
159
160static cl::opt<bool>
161DisableSLPVectorization("disable-slp-vectorization",
162 cl::desc("Disable the slp vectorization pass"),
163 cl::init(false));
164
Hal Finkel6d099042013-08-28 18:33:10 +0000165
166static cl::opt<bool>
Devang Patel9966ccf2008-09-16 22:25:14 +0000167DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel7293f0f2008-09-17 16:01:39 +0000168 cl::desc("Disable simplify-libcalls"));
Devang Patel9966ccf2008-09-16 22:25:14 +0000169
170static cl::opt<bool>
Chris Lattner1553edc2004-05-27 20:32:10 +0000171Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattnerf5cad152002-07-22 02:10:13 +0000172
Reid Spencerc8878ed2004-05-27 16:28:54 +0000173static cl::alias
174QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
175
Reid Spencer378f7d52006-08-18 06:34:30 +0000176static cl::opt<bool>
177AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
178
Devang Patel786a05e2010-12-07 00:33:43 +0000179static cl::opt<bool>
Andrew Trickb826ae82011-04-05 18:41:31 +0000180PrintBreakpoints("print-breakpoints-for-testing",
Devang Patel786a05e2010-12-07 00:33:43 +0000181 cl::desc("Print select breakpoints location for testing"));
182
Chris Lattnercfa19112009-10-22 00:44:10 +0000183static cl::opt<std::string>
Andrew Trickb826ae82011-04-05 18:41:31 +0000184DefaultDataLayout("default-data-layout",
Chris Lattnercfa19112009-10-22 00:44:10 +0000185 cl::desc("data layout string to use if not specified by module"),
186 cl::value_desc("layout-string"), cl::init(""));
187
Reid Spencer378f7d52006-08-18 06:34:30 +0000188
Devang Patel786a05e2010-12-07 00:33:43 +0000189
Chris Lattner3d70add2011-05-22 00:21:33 +0000190static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000191 // Add the pass to the pass manager...
192 PM.add(P);
193
194 // If we are verifying all of the intermediate steps, add the verifier...
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000195 if (VerifyEach) {
196 PM.add(createVerifierPass());
197 PM.add(createDebugInfoVerifierPass());
198 }
Reid Spencer22dbfb62007-02-02 14:46:29 +0000199}
200
Eric Christophere64061f2009-08-21 23:29:40 +0000201/// AddOptimizationPasses - This routine adds optimization passes
202/// based on selected optimization level, OptLevel. This routine
Devang Patel9966ccf2008-09-16 22:25:14 +0000203/// duplicates llvm-gcc behaviour.
204///
205/// OptLevel - Optimization Level
Chris Lattner3d70add2011-05-22 00:21:33 +0000206static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000207 unsigned OptLevel, unsigned SizeLevel) {
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000208 FPM.add(createVerifierPass()); // Verify that input is correct
209 MPM.add(createDebugInfoVerifierPass()); // Verify that debug info 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
Chris Lattner3d70add2011-05-22 00:21:33 +0000241static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer22dbfb62007-02-02 14:46:29 +0000242 PM.add(createVerifierPass()); // Verify that input is correct
243
Reid Spencer22dbfb62007-02-02 14:46:29 +0000244 // If the -strip-debug command line option was specified, do it.
245 if (StripDebug)
246 addPass(PM, createStripSymbolsPass(true));
247
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000248 // Verify debug info only after it's (possibly) stripped.
249 PM.add(createDebugInfoVerifierPass());
250
Reid Spencer22dbfb62007-02-02 14:46:29 +0000251 if (DisableOptimizations) return;
252
Daniel Dunbar94c7b792009-06-03 18:22:15 +0000253 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner3d70add2011-05-22 00:21:33 +0000254 PassManagerBuilder Builder;
255 if (!DisableInline)
256 Builder.Inliner = createFunctionInliningPass();
257 Builder.OptLevel = 3;
Chris Lattner3d70add2011-05-22 00:21:33 +0000258 Builder.populateModulePassManager(PM);
Reid Spencer22dbfb62007-02-02 14:46:29 +0000259}
260
Chris Lattner3d70add2011-05-22 00:21:33 +0000261static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000262 PM.add(createVerifierPass()); // Verify that input is correct
263
264 // If the -strip-debug command line option was specified, do it.
265 if (StripDebug)
266 addPass(PM, createStripSymbolsPass(true));
267
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000268 // Verify debug info only after it's (possibly) stripped.
269 PM.add(createDebugInfoVerifierPass());
270
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000271 if (DisableOptimizations) return;
272
Chris Lattner3d70add2011-05-22 00:21:33 +0000273 PassManagerBuilder Builder;
274 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
275 /*RunInliner=*/ !DisableInline);
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000276}
277
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000278//===----------------------------------------------------------------------===//
279// CodeGen-related helper functions.
280//
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000281
282CodeGenOpt::Level GetCodeGenOptLevel() {
283 if (OptLevelO1)
284 return CodeGenOpt::Less;
285 if (OptLevelO2)
286 return CodeGenOpt::Default;
287 if (OptLevelO3)
288 return CodeGenOpt::Aggressive;
289 return CodeGenOpt::None;
290}
291
292// Returns the TargetMachine instance or zero if no triple is provided.
Nadav Rotemb1615b12013-01-01 08:00:32 +0000293static TargetMachine* GetTargetMachine(Triple TheTriple) {
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000294 std::string Error;
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000295 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
296 Error);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000297 // Some modules don't specify a triple, and this is okay.
Eric Christopher13637e92013-04-15 07:31:37 +0000298 if (!TheTarget) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000299 return nullptr;
Eric Christopher13637e92013-04-15 07:31:37 +0000300 }
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000301
302 // Package up features to be passed to target/subtarget
303 std::string FeaturesStr;
304 if (MAttrs.size()) {
305 SubtargetFeatures Features;
306 for (unsigned i = 0; i != MAttrs.size(); ++i)
307 Features.AddFeature(MAttrs[i]);
308 FeaturesStr = Features.getString();
309 }
310
311 return TheTarget->createTargetMachine(TheTriple.getTriple(),
Eli Benderskyf0f21002014-02-19 17:09:35 +0000312 MCPU, FeaturesStr,
313 InitTargetOptionsFromCodeGenFlags(),
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000314 RelocModel, CMModel,
315 GetCodeGenOptLevel());
316}
Chris Lattner4db2f2c2002-02-01 04:54:11 +0000317
Sebastian Popa59005b2014-03-14 04:04:14 +0000318#ifdef LINK_POLLY_INTO_TOOLS
319namespace polly {
320void initializePollyPasses(llvm::PassRegistry &Registry);
321}
322#endif
323
Chris Lattner5a48a242002-07-23 18:12:22 +0000324//===----------------------------------------------------------------------===//
325// main for opt
326//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000327int main(int argc, char **argv) {
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000328 sys::PrintStackTraceOnErrorSignal();
329 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmana2233f22010-09-01 14:20:41 +0000330
David Greeneed8a1de2010-01-05 01:30:32 +0000331 // Enable debug stream buffering.
332 EnableDebugBuffering = true;
333
Chris Lattner4b2a6e22009-12-09 00:41:28 +0000334 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson19251ec2009-07-15 22:16:10 +0000335 LLVMContext &Context = getGlobalContext();
Andrew Trickb826ae82011-04-05 18:41:31 +0000336
Nadav Rotemac9a3442012-10-24 17:23:50 +0000337 InitializeAllTargets();
338 InitializeAllTargetMCs();
Tobias Grosser9190e0d2014-06-13 16:12:08 +0000339 InitializeAllAsmPrinters();
Nadav Rotemac9a3442012-10-24 17:23:50 +0000340
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000341 // Initialize passes
342 PassRegistry &Registry = *PassRegistry::getPassRegistry();
343 initializeCore(Registry);
Daniel Malea3c5bed12013-05-08 20:44:14 +0000344 initializeDebugIRPass(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000345 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000346 initializeObjCARCOpts(Registry);
Hal Finkelc34e5112012-02-01 03:51:43 +0000347 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000348 initializeIPO(Registry);
349 initializeAnalysis(Registry);
350 initializeIPA(Registry);
351 initializeTransformUtils(Registry);
352 initializeInstCombine(Registry);
353 initializeInstrumentation(Registry);
354 initializeTarget(Registry);
Quentin Colombeta3490842014-02-22 00:07:45 +0000355 // For codegen passes, only passes that do IR to IR transformation are
Tim Northover037f26f22014-04-17 18:22:47 +0000356 // supported.
Quentin Colombeta3490842014-02-22 00:07:45 +0000357 initializeCodeGenPreparePass(Registry);
Tim Northover037f26f22014-04-17 18:22:47 +0000358 initializeAtomicExpandLoadLinkedPass(Registry);
Andrew Trickb826ae82011-04-05 18:41:31 +0000359
Sebastian Popa59005b2014-03-14 04:04:14 +0000360#ifdef LINK_POLLY_INTO_TOOLS
361 polly::initializePollyPasses(Registry);
362#endif
363
Chris Lattnercc815262009-10-22 00:46:41 +0000364 cl::ParseCommandLineOptions(argc, argv,
365 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000366
Tobias Grosser083379f2010-12-02 20:35:16 +0000367 if (AnalyzeOnly && NoOutput) {
368 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
369 return 1;
370 }
371
Chris Lattnercc815262009-10-22 00:46:41 +0000372 SMDiagnostic Err;
Vikram S. Adve82491b72002-09-16 16:09:43 +0000373
Chris Lattnercc815262009-10-22 00:46:41 +0000374 // Load the input module...
Ahmed Charles56440fd2014-03-06 05:51:42 +0000375 std::unique_ptr<Module> M;
Chris Lattnercc815262009-10-22 00:46:41 +0000376 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophere64061f2009-08-21 23:29:40 +0000377
Craig Toppere6cb63e2014-04-25 04:24:47 +0000378 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000379 Err.print(argv[0], errs());
Chris Lattnercc815262009-10-22 00:46:41 +0000380 return 1;
381 }
382
Joe Groff1b738692012-04-17 23:05:48 +0000383 // If we are supposed to override the target triple, do so now.
Eric Christopher13637e92013-04-15 07:31:37 +0000384 if (!TargetTriple.empty())
Joe Groff1b738692012-04-17 23:05:48 +0000385 M->setTargetTriple(Triple::normalize(TargetTriple));
Eric Christopher1f140312013-04-14 23:35:36 +0000386
Chris Lattnercc815262009-10-22 00:46:41 +0000387 // Figure out what stream we are supposed to write to...
Ahmed Charles56440fd2014-03-06 05:51:42 +0000388 std::unique_ptr<tool_output_file> Out;
Dan Gohman083330a2010-08-18 17:42:59 +0000389 if (NoOutput) {
Dan Gohmanb01aed12010-08-18 17:40:10 +0000390 if (!OutputFilename.empty())
391 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman083330a2010-08-18 17:42:59 +0000392 "the --disable-output option is used.\n";
Dan Gohmanb01aed12010-08-18 17:40:10 +0000393 } else {
394 // Default to standard output.
395 if (OutputFilename.empty())
396 OutputFilename = "-";
Chris Lattnercc815262009-10-22 00:46:41 +0000397
Dan Gohmanb01aed12010-08-18 17:40:10 +0000398 std::string ErrorInfo;
Dan Gohman268b0f42010-08-20 01:07:01 +0000399 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000400 sys::fs::F_None));
Dan Gohmanb01aed12010-08-18 17:40:10 +0000401 if (!ErrorInfo.empty()) {
402 errs() << ErrorInfo << '\n';
Dan Gohmanb01aed12010-08-18 17:40:10 +0000403 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000404 }
Chris Lattnercc815262009-10-22 00:46:41 +0000405 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000406
Chris Lattnercc815262009-10-22 00:46:41 +0000407 // If the output is set to be emitted to standard out, and standard out is a
408 // console, print out a warning message and refuse to do it. We don't
409 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman915ad962010-01-17 17:47:24 +0000410 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000411 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattnercc815262009-10-22 00:46:41 +0000412 NoOutput = true;
Dan Gohmane5929232009-09-11 20:46:33 +0000413
Chandler Carruth949282e2014-01-13 03:08:40 +0000414 if (PassPipeline.getNumOccurrences() > 0) {
415 OutputKind OK = OK_NoOutput;
416 if (!NoOutput)
417 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
418
Chandler Carruth4d356312014-01-20 11:34:08 +0000419 VerifierKind VK = VK_VerifyInAndOut;
420 if (NoVerify)
421 VK = VK_NoVerifier;
422 else if (VerifyEach)
423 VK = VK_VerifyEachPass;
424
Chandler Carruth66445382014-01-11 08:16:35 +0000425 // The user has asked to use the new pass manager and provided a pipeline
426 // string. Hand off the rest of the functionality to the new code for that
427 // layer.
428 return runPassPipeline(argv[0], Context, *M.get(), Out.get(), PassPipeline,
Chandler Carruth4d356312014-01-20 11:34:08 +0000429 OK, VK)
Chandler Carruth66445382014-01-11 08:16:35 +0000430 ? 0
431 : 1;
Chandler Carruth949282e2014-01-13 03:08:40 +0000432 }
Chandler Carruth66445382014-01-11 08:16:35 +0000433
Chris Lattnercc815262009-10-22 00:46:41 +0000434 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000435 // about to build.
Chris Lattnercc815262009-10-22 00:46:41 +0000436 //
437 PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438
Chris Lattner15c8b5e2011-02-18 22:13:01 +0000439 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner1341df92011-02-18 22:34:03 +0000440 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Trickb826ae82011-04-05 18:41:31 +0000441
Chris Lattner1341df92011-02-18 22:34:03 +0000442 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
443 if (DisableSimplifyLibCalls)
444 TLI->disableAllFunctions();
445 Passes.add(TLI);
Andrew Trickb826ae82011-04-05 18:41:31 +0000446
Eric Christopher13637e92013-04-15 07:31:37 +0000447 // Add an appropriate DataLayout instance for this module.
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000448 const DataLayout *DL = M.get()->getDataLayout();
Rafael Espindola339430f2014-02-25 23:25:17 +0000449 if (!DL && !DefaultDataLayout.empty()) {
450 M->setDataLayout(DefaultDataLayout);
451 DL = M.get()->getDataLayout();
452 }
Eric Christopher13637e92013-04-15 07:31:37 +0000453
Rafael Espindolaf1939022014-02-21 02:01:42 +0000454 if (DL)
Rafael Espindola339430f2014-02-25 23:25:17 +0000455 Passes.add(new DataLayoutPass(M.get()));
Reid Spencer996ec722004-12-30 05:36:08 +0000456
Nadav Rotemb1615b12013-01-01 08:00:32 +0000457 Triple ModuleTriple(M->getTargetTriple());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000458 TargetMachine *Machine = nullptr;
Nadav Rotemb1615b12013-01-01 08:00:32 +0000459 if (ModuleTriple.getArch())
460 Machine = GetTargetMachine(Triple(ModuleTriple));
Ahmed Charles56440fd2014-03-06 05:51:42 +0000461 std::unique_ptr<TargetMachine> TM(Machine);
Nadav Rotemb1615b12013-01-01 08:00:32 +0000462
Chandler Carruth664e3542013-01-07 01:37:14 +0000463 // Add internal analysis passes from the target machine.
464 if (TM.get())
465 TM->addAnalysisPasses(Passes);
Nadav Rotem5dc203e2012-10-18 23:22:48 +0000466
Ahmed Charles56440fd2014-03-06 05:51:42 +0000467 std::unique_ptr<FunctionPassManager> FPasses;
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000468 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3d70add2011-05-22 00:21:33 +0000469 FPasses.reset(new FunctionPassManager(M.get()));
Rafael Espindolaf1939022014-02-21 02:01:42 +0000470 if (DL)
Rafael Espindola339430f2014-02-25 23:25:17 +0000471 FPasses->add(new DataLayoutPass(M.get()));
Tom Stellard8b1e0212013-07-27 00:01:07 +0000472 if (TM.get())
473 TM->addAnalysisPasses(*FPasses);
474
Chris Lattnercc815262009-10-22 00:46:41 +0000475 }
Reid Spencer996ec722004-12-30 05:36:08 +0000476
Devang Patel786a05e2010-12-07 00:33:43 +0000477 if (PrintBreakpoints) {
478 // Default to standard output.
479 if (!Out) {
480 if (OutputFilename.empty())
481 OutputFilename = "-";
Andrew Trickb826ae82011-04-05 18:41:31 +0000482
Devang Patel786a05e2010-12-07 00:33:43 +0000483 std::string ErrorInfo;
484 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000485 sys::fs::F_None));
Devang Patel786a05e2010-12-07 00:33:43 +0000486 if (!ErrorInfo.empty()) {
487 errs() << ErrorInfo << '\n';
488 return 1;
489 }
490 }
Eli Bendersky54dc2832014-02-12 16:48:02 +0000491 Passes.add(createBreakpointPrinter(Out->os()));
Devang Patel786a05e2010-12-07 00:33:43 +0000492 NoOutput = true;
493 }
494
Chris Lattnercc815262009-10-22 00:46:41 +0000495 // If the -strip-debug command line option was specified, add it. If
496 // -std-compile-opts was also specified, it will handle StripDebug.
497 if (StripDebug && !StandardCompileOpts)
498 addPass(Passes, createStripSymbolsPass(true));
Eric Christophere64061f2009-08-21 23:29:40 +0000499
Chris Lattnercc815262009-10-22 00:46:41 +0000500 // Create a new optimization pass for each one specified on the command line
501 for (unsigned i = 0; i < PassList.size(); ++i) {
502 // Check to see if -std-compile-opts was specified before this option. If
503 // so, handle it.
504 if (StandardCompileOpts &&
505 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner501c3b62008-07-13 19:35:21 +0000506 AddStandardCompilePasses(Passes);
507 StandardCompileOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000508 }
Reid Spencer996ec722004-12-30 05:36:08 +0000509
Chris Lattnercc815262009-10-22 00:46:41 +0000510 if (StandardLinkOpts &&
511 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000512 AddStandardLinkPasses(Passes);
513 StandardLinkOpts = false;
Eric Christophere64061f2009-08-21 23:29:40 +0000514 }
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000515
Chris Lattnercc815262009-10-22 00:46:41 +0000516 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000517 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000518 OptLevelO1 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000519 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000520
Chris Lattnercc815262009-10-22 00:46:41 +0000521 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000522 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000523 OptLevelO2 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000524 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000525
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000526 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
527 AddOptimizationPasses(Passes, *FPasses, 2, 1);
528 OptLevelOs = false;
529 }
530
531 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
532 AddOptimizationPasses(Passes, *FPasses, 2, 2);
533 OptLevelOz = false;
534 }
535
Chris Lattnercc815262009-10-22 00:46:41 +0000536 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000537 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000538 OptLevelO3 = false;
Daniel Dunbar41f9a3a2009-07-17 18:09:39 +0000539 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000540
Chris Lattnercc815262009-10-22 00:46:41 +0000541 const PassInfo *PassInf = PassList[i];
Craig Toppere6cb63e2014-04-25 04:24:47 +0000542 Pass *P = nullptr;
Quentin Colombetdc0b2ea2014-01-16 21:44:34 +0000543 if (PassInf->getTargetMachineCtor())
544 P = PassInf->getTargetMachineCtor()(TM.get());
545 else if (PassInf->getNormalCtor())
Chris Lattnercc815262009-10-22 00:46:41 +0000546 P = PassInf->getNormalCtor()();
547 else
548 errs() << argv[0] << ": cannot create pass: "
549 << PassInf->getPassName() << "\n";
550 if (P) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000551 PassKind Kind = P->getPassKind();
Chris Lattnercc815262009-10-22 00:46:41 +0000552 addPass(Passes, P);
553
554 if (AnalyzeOnly) {
Benjamin Kramer32c3d302010-02-18 12:57:05 +0000555 switch (Kind) {
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000556 case PT_BasicBlock:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000557 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000558 break;
Tobias Grosser23c83412010-10-20 01:54:44 +0000559 case PT_Region:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000560 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
Tobias Grosser23c83412010-10-20 01:54:44 +0000561 break;
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000562 case PT_Loop:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000563 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000564 break;
565 case PT_Function:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000566 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000567 break;
568 case PT_CallGraphSCC:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000569 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000570 break;
571 default:
Eli Benderskyb60f8382014-02-10 23:34:23 +0000572 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000573 break;
574 }
Chris Lattnercc815262009-10-22 00:46:41 +0000575 }
Devang Patel9966ccf2008-09-16 22:25:14 +0000576 }
577
Chris Lattnercc815262009-10-22 00:46:41 +0000578 if (PrintEachXForm)
Chandler Carruth9d805132014-01-12 11:30:46 +0000579 Passes.add(createPrintModulePass(errs()));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000580 }
Chris Lattnercc815262009-10-22 00:46:41 +0000581
582 // If -std-compile-opts was specified at the end of the pass list, add them.
583 if (StandardCompileOpts) {
584 AddStandardCompilePasses(Passes);
585 StandardCompileOpts = false;
586 }
587
588 if (StandardLinkOpts) {
589 AddStandardLinkPasses(Passes);
590 StandardLinkOpts = false;
591 }
592
593 if (OptLevelO1)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000594 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000595
596 if (OptLevelO2)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000597 AddOptimizationPasses(Passes, *FPasses, 2, 0);
598
599 if (OptLevelOs)
600 AddOptimizationPasses(Passes, *FPasses, 2, 1);
601
602 if (OptLevelOz)
603 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattnercc815262009-10-22 00:46:41 +0000604
605 if (OptLevelO3)
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000606 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattnercc815262009-10-22 00:46:41 +0000607
Chandler Carruthd8c08c22012-05-16 08:32:49 +0000608 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000609 FPasses->doInitialization();
Chris Lattner3d70add2011-05-22 00:21:33 +0000610 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
611 FPasses->run(*F);
Chris Lattneref8cf6d2011-05-22 06:44:19 +0000612 FPasses->doFinalization();
613 }
Chris Lattnercc815262009-10-22 00:46:41 +0000614
615 // Check that the module is well formed on completion of optimization
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000616 if (!NoVerify && !VerifyEach) {
Chris Lattnercc815262009-10-22 00:46:41 +0000617 Passes.add(createVerifierPass());
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000618 Passes.add(createDebugInfoVerifierPass());
619 }
Chris Lattnercc815262009-10-22 00:46:41 +0000620
Dan Gohman083330a2010-08-18 17:42:59 +0000621 // Write bitcode or assembly to the output as the last step...
Chris Lattnercc815262009-10-22 00:46:41 +0000622 if (!NoOutput && !AnalyzeOnly) {
623 if (OutputAssembly)
Chandler Carruth9d805132014-01-12 11:30:46 +0000624 Passes.add(createPrintModulePass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000625 else
Dan Gohmana2233f22010-09-01 14:20:41 +0000626 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattnercc815262009-10-22 00:46:41 +0000627 }
628
Andrew Trick12004012011-04-05 18:54:36 +0000629 // Before executing passes, print the final values of the LLVM options.
630 cl::PrintOptionValues();
631
Chris Lattnercc815262009-10-22 00:46:41 +0000632 // Now that we have all of the passes ready, run them.
633 Passes.run(*M.get());
634
Dan Gohman268b0f42010-08-20 01:07:01 +0000635 // Declare success.
Devang Patel786a05e2010-12-07 00:33:43 +0000636 if (!NoOutput || PrintBreakpoints)
Dan Gohman268b0f42010-08-20 01:07:01 +0000637 Out->keep();
638
Chris Lattnercc815262009-10-22 00:46:41 +0000639 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000640}