1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Optimizations may be specified an arbitrary number of times on the command
11 // line, They are run in the order specified.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "BreakpointPrinter.h"
16 #include "NewPMDriver.h"
17 #include "PassPrinters.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/Analysis/CallGraphSCCPass.h"
21 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/Analysis/RegionPass.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/TargetTransformInfo.h"
25 #include "llvm/Bitcode/BitcodeWriterPass.h"
26 #include "llvm/CodeGen/CommandFlags.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/IRPrintingPasses.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LegacyPassNameParser.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/LinkAllIR.h"
37 #include "llvm/LinkAllPasses.h"
38 #include "llvm/MC/SubtargetFeature.h"
39 #include "llvm/IR/LegacyPassManager.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/FileSystem.h"
42 #include "llvm/Support/Host.h"
43 #include "llvm/Support/ManagedStatic.h"
44 #include "llvm/Support/PluginLoader.h"
45 #include "llvm/Support/PrettyStackTrace.h"
46 #include "llvm/Support/Signals.h"
47 #include "llvm/Support/SourceMgr.h"
48 #include "llvm/Support/SystemUtils.h"
49 #include "llvm/Support/TargetRegistry.h"
50 #include "llvm/Support/TargetSelect.h"
51 #include "llvm/Support/ToolOutputFile.h"
52 #include "llvm/Target/TargetMachine.h"
53 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
54 #include <algorithm>
55 #include <memory>
56 using namespace llvm;
57 using namespace opt_tool;
58
59 // The OptimizationList is automatically populated with registered Passes by the
60 // PassNameParser.
61 //
62 static cl::list<const PassInfo*, bool, PassNameParser>
63 PassList(cl::desc("Optimizations available:"));
64
65 // 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.
69 static cl::opt<std::string> PassPipeline(
70 "passes",
71 cl::desc("A textual description of the pass pipeline for optimizing"),
72 cl::Hidden);
73
74 // Other command line options...
75 //
76 static cl::opt<std::string>
77 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
78 cl::init("-"), cl::value_desc("filename"));
79
80 static cl::opt<std::string>
81 OutputFilename("o", cl::desc("Override output filename"),
82 cl::value_desc("filename"));
83
84 static cl::opt<bool>
85 Force("f", cl::desc("Enable binary output on terminals"));
86
87 static cl::opt<bool>
88 PrintEachXForm("p", cl::desc("Print module after each transformation"));
89
90 static cl::opt<bool>
91 NoOutput("disable-output",
92 cl::desc("Do not write result bitcode file"), cl::Hidden);
93
94 static cl::opt<bool>
95 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
96
97 static cl::opt<bool>
98 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
99
100 static cl::opt<bool>
101 VerifyEach("verify-each", cl::desc("Verify after each transform"));
102
103 static cl::opt<bool>
104 StripDebug("strip-debug",
105 cl::desc("Strip debugger symbol info from translation unit"));
106
107 static cl::opt<bool>
108 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
109
110 static cl::opt<bool>
111 DisableOptimizations("disable-opt",
112 cl::desc("Do not run any optimization passes"));
113
114 static cl::opt<bool>
115 StandardLinkOpts("std-link-opts",
116 cl::desc("Include the standard link time optimizations"));
117
118 static cl::opt<bool>
119 OptLevelO1("O1",
120 cl::desc("Optimization level 1. Similar to clang -O1"));
121
122 static cl::opt<bool>
123 OptLevelO2("O2",
124 cl::desc("Optimization level 2. Similar to clang -O2"));
125
126 static cl::opt<bool>
127 OptLevelOs("Os",
128 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
129
130 static cl::opt<bool>
131 OptLevelOz("Oz",
132 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
133
134 static cl::opt<bool>
135 OptLevelO3("O3",
136 cl::desc("Optimization level 3. Similar to clang -O3"));
137
138 static cl::opt<std::string>
139 TargetTriple("mtriple", cl::desc("Override target triple for module"));
140
141 static cl::opt<bool>
142 UnitAtATime("funit-at-a-time",
143 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
144 cl::init(true));
145
146 static cl::opt<bool>
147 DisableLoopUnrolling("disable-loop-unrolling",
148 cl::desc("Disable loop unrolling in all relevant passes"),
149 cl::init(false));
150 static cl::opt<bool>
151 DisableLoopVectorization("disable-loop-vectorization",
152 cl::desc("Disable the loop vectorization pass"),
153 cl::init(false));
154
155 static cl::opt<bool>
156 DisableSLPVectorization("disable-slp-vectorization",
157 cl::desc("Disable the slp vectorization pass"),
158 cl::init(false));
159
160
161 static cl::opt<bool>
162 DisableSimplifyLibCalls("disable-simplify-libcalls",
163 cl::desc("Disable simplify-libcalls"));
164
165 static cl::opt<bool>
166 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
167
168 static cl::alias
169 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
170
171 static cl::opt<bool>
172 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
173
174 static cl::opt<bool>
175 PrintBreakpoints("print-breakpoints-for-testing",
176 cl::desc("Print select breakpoints location for testing"));
177
178 static cl::opt<std::string>
179 DefaultDataLayout("default-data-layout",
180 cl::desc("data layout string to use if not specified by module"),
181 cl::value_desc("layout-string"), cl::init(""));
182
183 static 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);
187
188 static 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);
192
addPass(legacy::PassManagerBase & PM,Pass * P)193 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
194 // 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...
198 if (VerifyEach)
199 PM.add(createVerifierPass());
200 }
201
202 /// This routine adds optimization passes based on selected optimization level,
203 /// OptLevel.
204 ///
205 /// OptLevel - Optimization Level
AddOptimizationPasses(legacy::PassManagerBase & MPM,legacy::FunctionPassManager & FPM,unsigned OptLevel,unsigned SizeLevel)206 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
207 legacy::FunctionPassManager &FPM,
208 unsigned OptLevel, unsigned SizeLevel) {
209 FPM.add(createVerifierPass()); // Verify that input is correct
210
211 PassManagerBuilder Builder;
212 Builder.OptLevel = OptLevel;
213 Builder.SizeLevel = SizeLevel;
214
215 if (DisableInline) {
216 // No inlining pass
217 } else if (OptLevel > 1) {
218 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
219 } else {
220 Builder.Inliner = createAlwaysInlinerPass();
221 }
222 Builder.DisableUnitAtATime = !UnitAtATime;
223 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
224 DisableLoopUnrolling : OptLevel == 0;
225
226 // 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
234 Builder.SLPVectorize =
235 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
236
237 Builder.populateFunctionPassManager(FPM);
238 Builder.populateModulePassManager(MPM);
239 }
240
AddStandardLinkPasses(legacy::PassManagerBase & PM)241 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
242 PassManagerBuilder Builder;
243 Builder.VerifyInput = true;
244 if (DisableOptimizations)
245 Builder.OptLevel = 0;
246
247 if (!DisableInline)
248 Builder.Inliner = createFunctionInliningPass();
249 Builder.populateLTOPassManager(PM);
250 }
251
252 //===----------------------------------------------------------------------===//
253 // CodeGen-related helper functions.
254 //
255
GetCodeGenOptLevel()256 static CodeGenOpt::Level GetCodeGenOptLevel() {
257 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.
GetTargetMachine(Triple TheTriple)267 static TargetMachine* GetTargetMachine(Triple TheTriple) {
268 std::string Error;
269 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
270 Error);
271 // Some modules don't specify a triple, and this is okay.
272 if (!TheTarget) {
273 return nullptr;
274 }
275
276 // Package up features to be passed to target/subtarget
277 std::string FeaturesStr;
278 if (MAttrs.size() || MCPU == "native") {
279 SubtargetFeatures Features;
280
281 // If user asked for the 'native' CPU, we need to autodetect features.
282 // This is necessary for x86 where the CPU might not support all the
283 // features the autodetected CPU name lists in the target. For example,
284 // not all Sandybridge processors support AVX.
285 if (MCPU == "native") {
286 StringMap<bool> HostFeatures;
287 if (sys::getHostCPUFeatures(HostFeatures))
288 for (auto &F : HostFeatures)
289 Features.AddFeature(F.first(), F.second);
290 }
291
292 for (unsigned i = 0; i != MAttrs.size(); ++i)
293 Features.AddFeature(MAttrs[i]);
294 FeaturesStr = Features.getString();
295 }
296
297 if (MCPU == "native")
298 MCPU = sys::getHostCPUName();
299
300 return TheTarget->createTargetMachine(TheTriple.getTriple(),
301 MCPU, FeaturesStr,
302 InitTargetOptionsFromCodeGenFlags(),
303 RelocModel, CMModel,
304 GetCodeGenOptLevel());
305 }
306
307 #ifdef LINK_POLLY_INTO_TOOLS
308 namespace polly {
309 void initializePollyPasses(llvm::PassRegistry &Registry);
310 }
311 #endif
312
313 //===----------------------------------------------------------------------===//
314 // main for opt
315 //
main(int argc,char ** argv)316 int main(int argc, char **argv) {
317 sys::PrintStackTraceOnErrorSignal();
318 llvm::PrettyStackTraceProgram X(argc, argv);
319
320 // Enable debug stream buffering.
321 EnableDebugBuffering = true;
322
323 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
324 LLVMContext &Context = getGlobalContext();
325
326 InitializeAllTargets();
327 InitializeAllTargetMCs();
328 InitializeAllAsmPrinters();
329
330 // Initialize passes
331 PassRegistry &Registry = *PassRegistry::getPassRegistry();
332 initializeCore(Registry);
333 initializeScalarOpts(Registry);
334 initializeObjCARCOpts(Registry);
335 initializeVectorization(Registry);
336 initializeIPO(Registry);
337 initializeAnalysis(Registry);
338 initializeIPA(Registry);
339 initializeTransformUtils(Registry);
340 initializeInstCombine(Registry);
341 initializeInstrumentation(Registry);
342 initializeTarget(Registry);
343 // For codegen passes, only passes that do IR to IR transformation are
344 // supported.
345 initializeCodeGenPreparePass(Registry);
346 initializeAtomicExpandPass(Registry);
347 initializeRewriteSymbolsPass(Registry);
348 initializeWinEHPreparePass(Registry);
349 initializeDwarfEHPreparePass(Registry);
350
351 #ifdef LINK_POLLY_INTO_TOOLS
352 polly::initializePollyPasses(Registry);
353 #endif
354
355 cl::ParseCommandLineOptions(argc, argv,
356 "llvm .bc -> .bc modular optimizer and analysis printer\n");
357
358 if (AnalyzeOnly && NoOutput) {
359 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
360 return 1;
361 }
362
363 SMDiagnostic Err;
364
365 // Load the input module...
366 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
367
368 if (!M) {
369 Err.print(argv[0], errs());
370 return 1;
371 }
372
373 // Strip debug info before running the verifier.
374 if (StripDebug)
375 StripDebugInfo(*M);
376
377 // Immediately run the verifier to catch any problems before starting up the
378 // pass pipelines. Otherwise we can crash on broken code during
379 // doInitialization().
380 if (!NoVerify && verifyModule(*M, &errs())) {
381 errs() << argv[0] << ": " << InputFilename
382 << ": error: input module is broken!\n";
383 return 1;
384 }
385
386 // If we are supposed to override the target triple, do so now.
387 if (!TargetTriple.empty())
388 M->setTargetTriple(Triple::normalize(TargetTriple));
389
390 // Figure out what stream we are supposed to write to...
391 std::unique_ptr<tool_output_file> Out;
392 if (NoOutput) {
393 if (!OutputFilename.empty())
394 errs() << "WARNING: The -o (output filename) option is ignored when\n"
395 "the --disable-output option is used.\n";
396 } else {
397 // Default to standard output.
398 if (OutputFilename.empty())
399 OutputFilename = "-";
400
401 std::error_code EC;
402 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
403 if (EC) {
404 errs() << EC.message() << '\n';
405 return 1;
406 }
407 }
408
409 Triple ModuleTriple(M->getTargetTriple());
410 TargetMachine *Machine = nullptr;
411 if (ModuleTriple.getArch())
412 Machine = GetTargetMachine(ModuleTriple);
413 std::unique_ptr<TargetMachine> TM(Machine);
414
415 // If the output is set to be emitted to standard out, and standard out is a
416 // console, print out a warning message and refuse to do it. We don't
417 // impress anyone by spewing tons of binary goo to a terminal.
418 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
419 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
420 NoOutput = true;
421
422 if (PassPipeline.getNumOccurrences() > 0) {
423 OutputKind OK = OK_NoOutput;
424 if (!NoOutput)
425 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
426
427 VerifierKind VK = VK_VerifyInAndOut;
428 if (NoVerify)
429 VK = VK_NoVerifier;
430 else if (VerifyEach)
431 VK = VK_VerifyEachPass;
432
433 // The user has asked to use the new pass manager and provided a pipeline
434 // string. Hand off the rest of the functionality to the new code for that
435 // layer.
436 return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(),
437 PassPipeline, OK, VK, PreserveAssemblyUseListOrder,
438 PreserveBitcodeUseListOrder)
439 ? 0
440 : 1;
441 }
442
443 // Create a PassManager to hold and optimize the collection of passes we are
444 // about to build.
445 //
446 legacy::PassManager Passes;
447
448 // Add an appropriate TargetLibraryInfo pass for the module's triple.
449 TargetLibraryInfoImpl TLII(ModuleTriple);
450
451 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
452 if (DisableSimplifyLibCalls)
453 TLII.disableAllFunctions();
454 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
455
456 // Add an appropriate DataLayout instance for this module.
457 const DataLayout &DL = M->getDataLayout();
458 if (DL.isDefault() && !DefaultDataLayout.empty()) {
459 M->setDataLayout(DefaultDataLayout);
460 }
461
462 // Add internal analysis passes from the target machine.
463 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
464 : TargetIRAnalysis()));
465
466 std::unique_ptr<legacy::FunctionPassManager> FPasses;
467 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
468 FPasses.reset(new legacy::FunctionPassManager(M.get()));
469 FPasses->add(createTargetTransformInfoWrapperPass(
470 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
471 }
472
473 if (PrintBreakpoints) {
474 // Default to standard output.
475 if (!Out) {
476 if (OutputFilename.empty())
477 OutputFilename = "-";
478
479 std::error_code EC;
480 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
481 sys::fs::F_None);
482 if (EC) {
483 errs() << EC.message() << '\n';
484 return 1;
485 }
486 }
487 Passes.add(createBreakpointPrinter(Out->os()));
488 NoOutput = true;
489 }
490
491 // Create a new optimization pass for each one specified on the command line
492 for (unsigned i = 0; i < PassList.size(); ++i) {
493 if (StandardLinkOpts &&
494 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
495 AddStandardLinkPasses(Passes);
496 StandardLinkOpts = false;
497 }
498
499 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
500 AddOptimizationPasses(Passes, *FPasses, 1, 0);
501 OptLevelO1 = false;
502 }
503
504 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
505 AddOptimizationPasses(Passes, *FPasses, 2, 0);
506 OptLevelO2 = false;
507 }
508
509 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
510 AddOptimizationPasses(Passes, *FPasses, 2, 1);
511 OptLevelOs = false;
512 }
513
514 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
515 AddOptimizationPasses(Passes, *FPasses, 2, 2);
516 OptLevelOz = false;
517 }
518
519 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
520 AddOptimizationPasses(Passes, *FPasses, 3, 0);
521 OptLevelO3 = false;
522 }
523
524 const PassInfo *PassInf = PassList[i];
525 Pass *P = nullptr;
526 if (PassInf->getTargetMachineCtor())
527 P = PassInf->getTargetMachineCtor()(TM.get());
528 else if (PassInf->getNormalCtor())
529 P = PassInf->getNormalCtor()();
530 else
531 errs() << argv[0] << ": cannot create pass: "
532 << PassInf->getPassName() << "\n";
533 if (P) {
534 PassKind Kind = P->getPassKind();
535 addPass(Passes, P);
536
537 if (AnalyzeOnly) {
538 switch (Kind) {
539 case PT_BasicBlock:
540 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
541 break;
542 case PT_Region:
543 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
544 break;
545 case PT_Loop:
546 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
547 break;
548 case PT_Function:
549 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
550 break;
551 case PT_CallGraphSCC:
552 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
553 break;
554 default:
555 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
556 break;
557 }
558 }
559 }
560
561 if (PrintEachXForm)
562 Passes.add(
563 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
564 }
565
566 if (StandardLinkOpts) {
567 AddStandardLinkPasses(Passes);
568 StandardLinkOpts = false;
569 }
570
571 if (OptLevelO1)
572 AddOptimizationPasses(Passes, *FPasses, 1, 0);
573
574 if (OptLevelO2)
575 AddOptimizationPasses(Passes, *FPasses, 2, 0);
576
577 if (OptLevelOs)
578 AddOptimizationPasses(Passes, *FPasses, 2, 1);
579
580 if (OptLevelOz)
581 AddOptimizationPasses(Passes, *FPasses, 2, 2);
582
583 if (OptLevelO3)
584 AddOptimizationPasses(Passes, *FPasses, 3, 0);
585
586 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
587 FPasses->doInitialization();
588 for (Function &F : *M)
589 FPasses->run(F);
590 FPasses->doFinalization();
591 }
592
593 // Check that the module is well formed on completion of optimization
594 if (!NoVerify && !VerifyEach)
595 Passes.add(createVerifierPass());
596
597 // Write bitcode or assembly to the output as the last step...
598 if (!NoOutput && !AnalyzeOnly) {
599 if (OutputAssembly)
600 Passes.add(
601 createPrintModulePass(Out->os(), "", PreserveAssemblyUseListOrder));
602 else
603 Passes.add(
604 createBitcodeWriterPass(Out->os(), PreserveBitcodeUseListOrder));
605 }
606
607 // Before executing passes, print the final values of the LLVM options.
608 cl::PrintOptionValues();
609
610 // Now that we have all of the passes ready, run them.
611 Passes.run(*M);
612
613 // Declare success.
614 if (!NoOutput || PrintBreakpoints)
615 Out->keep();
616
617 return 0;
618 }
619