xref: /llvm-project-15.0.7/llvm/tools/opt/opt.cpp (revision d3d0ecbf)
1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Optimizations may be specified an arbitrary number of times on the command
10 // line, They are run in the order specified.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BreakpointPrinter.h"
15 #include "Debugify.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.inc"
27 #include "llvm/CodeGen/TargetPassConfig.h"
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DebugInfo.h"
31 #include "llvm/IR/IRPrintingPasses.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/LegacyPassManager.h"
34 #include "llvm/IR/LegacyPassNameParser.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/RemarkStreamer.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/IRReader/IRReader.h"
39 #include "llvm/InitializePasses.h"
40 #include "llvm/LinkAllIR.h"
41 #include "llvm/LinkAllPasses.h"
42 #include "llvm/MC/SubtargetFeature.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/FileSystem.h"
45 #include "llvm/Support/Host.h"
46 #include "llvm/Support/InitLLVM.h"
47 #include "llvm/Support/PluginLoader.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/SystemUtils.h"
50 #include "llvm/Support/TargetRegistry.h"
51 #include "llvm/Support/TargetSelect.h"
52 #include "llvm/Support/ToolOutputFile.h"
53 #include "llvm/Support/YAMLTraits.h"
54 #include "llvm/Target/TargetMachine.h"
55 #include "llvm/Transforms/Coroutines.h"
56 #include "llvm/Transforms/IPO/AlwaysInliner.h"
57 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
58 #include "llvm/Transforms/Utils/Cloning.h"
59 #include <algorithm>
60 #include <memory>
61 using namespace llvm;
62 using namespace opt_tool;
63 
64 // The OptimizationList is automatically populated with registered Passes by the
65 // PassNameParser.
66 //
67 static cl::list<const PassInfo*, bool, PassNameParser>
68 PassList(cl::desc("Optimizations available:"));
69 
70 // This flag specifies a textual description of the optimization pass pipeline
71 // to run over the module. This flag switches opt to use the new pass manager
72 // infrastructure, completely disabling all of the flags specific to the old
73 // pass management.
74 static cl::opt<std::string> PassPipeline(
75     "passes",
76     cl::desc("A textual description of the pass pipeline for optimizing"),
77     cl::Hidden);
78 
79 // Other command line options...
80 //
81 static cl::opt<std::string>
82 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
83     cl::init("-"), cl::value_desc("filename"));
84 
85 static cl::opt<std::string>
86 OutputFilename("o", cl::desc("Override output filename"),
87                cl::value_desc("filename"));
88 
89 static cl::opt<bool>
90 Force("f", cl::desc("Enable binary output on terminals"));
91 
92 static cl::opt<bool>
93 PrintEachXForm("p", cl::desc("Print module after each transformation"));
94 
95 static cl::opt<bool>
96 NoOutput("disable-output",
97          cl::desc("Do not write result bitcode file"), cl::Hidden);
98 
99 static cl::opt<bool>
100 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
101 
102 static cl::opt<bool>
103     OutputThinLTOBC("thinlto-bc",
104                     cl::desc("Write output as ThinLTO-ready bitcode"));
105 
106 static cl::opt<bool>
107     SplitLTOUnit("thinlto-split-lto-unit",
108                  cl::desc("Enable splitting of a ThinLTO LTOUnit"));
109 
110 static cl::opt<std::string> ThinLinkBitcodeFile(
111     "thin-link-bitcode-file", cl::value_desc("filename"),
112     cl::desc(
113         "A file in which to write minimized bitcode for the thin link only"));
114 
115 static cl::opt<bool>
116 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
117 
118 static cl::opt<bool>
119 VerifyEach("verify-each", cl::desc("Verify after each transform"));
120 
121 static cl::opt<bool>
122     DisableDITypeMap("disable-debug-info-type-map",
123                      cl::desc("Don't use a uniquing type map for debug info"));
124 
125 static cl::opt<bool>
126 StripDebug("strip-debug",
127            cl::desc("Strip debugger symbol info from translation unit"));
128 
129 static cl::opt<bool>
130     StripNamedMetadata("strip-named-metadata",
131                        cl::desc("Strip module-level named metadata"));
132 
133 static cl::opt<bool> DisableInline("disable-inlining",
134                                    cl::desc("Do not run the inliner pass"));
135 
136 static cl::opt<bool>
137 DisableOptimizations("disable-opt",
138                      cl::desc("Do not run any optimization passes"));
139 
140 static cl::opt<bool>
141 StandardLinkOpts("std-link-opts",
142                  cl::desc("Include the standard link time optimizations"));
143 
144 static cl::opt<bool>
145 OptLevelO0("O0",
146   cl::desc("Optimization level 0. Similar to clang -O0"));
147 
148 static cl::opt<bool>
149 OptLevelO1("O1",
150            cl::desc("Optimization level 1. Similar to clang -O1"));
151 
152 static cl::opt<bool>
153 OptLevelO2("O2",
154            cl::desc("Optimization level 2. Similar to clang -O2"));
155 
156 static cl::opt<bool>
157 OptLevelOs("Os",
158            cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
159 
160 static cl::opt<bool>
161 OptLevelOz("Oz",
162            cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
163 
164 static cl::opt<bool>
165 OptLevelO3("O3",
166            cl::desc("Optimization level 3. Similar to clang -O3"));
167 
168 static cl::opt<unsigned>
169 CodeGenOptLevel("codegen-opt-level",
170                 cl::desc("Override optimization level for codegen hooks"));
171 
172 static cl::opt<std::string>
173 TargetTriple("mtriple", cl::desc("Override target triple for module"));
174 
175 static cl::opt<bool>
176 DisableLoopUnrolling("disable-loop-unrolling",
177                      cl::desc("Disable loop unrolling in all relevant passes"),
178                      cl::init(false));
179 
180 static cl::opt<bool>
181 DisableSLPVectorization("disable-slp-vectorization",
182                         cl::desc("Disable the slp vectorization pass"),
183                         cl::init(false));
184 
185 static cl::opt<bool> EmitSummaryIndex("module-summary",
186                                       cl::desc("Emit module summary index"),
187                                       cl::init(false));
188 
189 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
190                                     cl::init(false));
191 
192 static cl::opt<bool>
193 DisableSimplifyLibCalls("disable-simplify-libcalls",
194                         cl::desc("Disable simplify-libcalls"));
195 
196 static cl::opt<bool>
197 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
198 
199 static cl::alias
200 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
201 
202 static cl::opt<bool>
203 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
204 
205 static cl::opt<bool> EnableDebugify(
206     "enable-debugify",
207     cl::desc(
208         "Start the pipeline with debugify and end it with check-debugify"));
209 
210 static cl::opt<bool> DebugifyEach(
211     "debugify-each",
212     cl::desc(
213         "Start each pass with debugify and end it with check-debugify"));
214 
215 static cl::opt<std::string>
216     DebugifyExport("debugify-export",
217                    cl::desc("Export per-pass debugify statistics to this file"),
218                    cl::value_desc("filename"), cl::init(""));
219 
220 static cl::opt<bool>
221 PrintBreakpoints("print-breakpoints-for-testing",
222                  cl::desc("Print select breakpoints location for testing"));
223 
224 static cl::opt<std::string> ClDataLayout("data-layout",
225                                          cl::desc("data layout string to use"),
226                                          cl::value_desc("layout-string"),
227                                          cl::init(""));
228 
229 static cl::opt<bool> PreserveBitcodeUseListOrder(
230     "preserve-bc-uselistorder",
231     cl::desc("Preserve use-list order when writing LLVM bitcode."),
232     cl::init(true), cl::Hidden);
233 
234 static cl::opt<bool> PreserveAssemblyUseListOrder(
235     "preserve-ll-uselistorder",
236     cl::desc("Preserve use-list order when writing LLVM assembly."),
237     cl::init(false), cl::Hidden);
238 
239 static cl::opt<bool>
240     RunTwice("run-twice",
241              cl::desc("Run all passes twice, re-using the same pass manager."),
242              cl::init(false), cl::Hidden);
243 
244 static cl::opt<bool> DiscardValueNames(
245     "discard-value-names",
246     cl::desc("Discard names from Value (other than GlobalValue)."),
247     cl::init(false), cl::Hidden);
248 
249 static cl::opt<bool> Coroutines(
250   "enable-coroutines",
251   cl::desc("Enable coroutine passes."),
252   cl::init(false), cl::Hidden);
253 
254 static cl::opt<bool> PassRemarksWithHotness(
255     "pass-remarks-with-hotness",
256     cl::desc("With PGO, include profile count in optimization remarks"),
257     cl::Hidden);
258 
259 static cl::opt<unsigned> PassRemarksHotnessThreshold(
260     "pass-remarks-hotness-threshold",
261     cl::desc("Minimum profile count required for an optimization remark to be output"),
262     cl::Hidden);
263 
264 static cl::opt<std::string>
265     RemarksFilename("pass-remarks-output",
266                     cl::desc("YAML output filename for pass remarks"),
267                     cl::value_desc("filename"));
268 
269 static cl::opt<std::string>
270     RemarksPasses("pass-remarks-filter",
271                   cl::desc("Only record optimization remarks from passes whose "
272                            "names match the given regular expression"),
273                   cl::value_desc("regex"));
274 
275 cl::opt<PGOKind>
276     PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
277                 cl::desc("The kind of profile guided optimization"),
278                 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
279                            clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
280                                       "Instrument the IR to generate profile."),
281                            clEnumValN(InstrUse, "pgo-instr-use-pipeline",
282                                       "Use instrumented profile to guide PGO."),
283                            clEnumValN(SampleUse, "pgo-sample-use-pipeline",
284                                       "Use sampled profile to guide PGO.")));
285 cl::opt<std::string> ProfileFile("profile-file",
286                                  cl::desc("Path to the profile."), cl::Hidden);
287 
288 cl::opt<CSPGOKind> CSPGOKindFlag(
289     "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
290     cl::desc("The kind of context sensitive profile guided optimization"),
291     cl::values(
292         clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
293         clEnumValN(
294             CSInstrGen, "cspgo-instr-gen-pipeline",
295             "Instrument (context sensitive) the IR to generate profile."),
296         clEnumValN(
297             CSInstrUse, "cspgo-instr-use-pipeline",
298             "Use instrumented (context sensitive) profile to guide PGO.")));
299 cl::opt<std::string> CSProfileGenFile(
300     "cs-profilegen-file",
301     cl::desc("Path to the instrumented context sensitive profile."),
302     cl::Hidden);
303 
304 class OptCustomPassManager : public legacy::PassManager {
305   DebugifyStatsMap DIStatsMap;
306 
307 public:
308   using super = legacy::PassManager;
309 
310   void add(Pass *P) override {
311     // Wrap each pass with (-check)-debugify passes if requested, making
312     // exceptions for passes which shouldn't see -debugify instrumentation.
313     bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() &&
314                             !isIRPrintingPass(P) && !isBitcodeWriterPass(P);
315     if (!WrapWithDebugify) {
316       super::add(P);
317       return;
318     }
319 
320     // Apply -debugify/-check-debugify before/after each pass and collect
321     // debug info loss statistics.
322     PassKind Kind = P->getPassKind();
323     StringRef Name = P->getPassName();
324 
325     // TODO: Implement Debugify for BasicBlockPass, LoopPass.
326     switch (Kind) {
327       case PT_Function:
328         super::add(createDebugifyFunctionPass());
329         super::add(P);
330         super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap));
331         break;
332       case PT_Module:
333         super::add(createDebugifyModulePass());
334         super::add(P);
335         super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap));
336         break;
337       default:
338         super::add(P);
339         break;
340     }
341   }
342 
343   const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; }
344 };
345 
346 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
347   // Add the pass to the pass manager...
348   PM.add(P);
349 
350   // If we are verifying all of the intermediate steps, add the verifier...
351   if (VerifyEach)
352     PM.add(createVerifierPass());
353 }
354 
355 /// This routine adds optimization passes based on selected optimization level,
356 /// OptLevel.
357 ///
358 /// OptLevel - Optimization Level
359 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
360                                   legacy::FunctionPassManager &FPM,
361                                   TargetMachine *TM, unsigned OptLevel,
362                                   unsigned SizeLevel) {
363   if (!NoVerify || VerifyEach)
364     FPM.add(createVerifierPass()); // Verify that input is correct
365 
366   PassManagerBuilder Builder;
367   Builder.OptLevel = OptLevel;
368   Builder.SizeLevel = SizeLevel;
369 
370   if (DisableInline) {
371     // No inlining pass
372   } else if (OptLevel > 1) {
373     Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
374   } else {
375     Builder.Inliner = createAlwaysInlinerLegacyPass();
376   }
377   Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
378                                DisableLoopUnrolling : OptLevel == 0;
379 
380   // Check if vectorization is explicitly disabled via -vectorize-loops=false.
381   // The flag enables vectorization in the LoopVectorize pass, it is on by
382   // default, and if it was disabled, leave it disabled here.
383   // Another flag that exists: -loop-vectorize, controls adding the pass to the
384   // pass manager. If set, the pass is added, and there is no additional check
385   // here for it.
386   if (Builder.LoopVectorize)
387     Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
388 
389   // When #pragma vectorize is on for SLP, do the same as above
390   Builder.SLPVectorize =
391       DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
392 
393   if (TM)
394     TM->adjustPassManager(Builder);
395 
396   if (Coroutines)
397     addCoroutinePassesToExtensionPoints(Builder);
398 
399   switch (PGOKindFlag) {
400   case InstrGen:
401     Builder.EnablePGOInstrGen = true;
402     Builder.PGOInstrGen = ProfileFile;
403     break;
404   case InstrUse:
405     Builder.PGOInstrUse = ProfileFile;
406     break;
407   case SampleUse:
408     Builder.PGOSampleUse = ProfileFile;
409     break;
410   default:
411     break;
412   }
413 
414   switch (CSPGOKindFlag) {
415   case CSInstrGen:
416     Builder.EnablePGOCSInstrGen = true;
417     break;
418   case CSInstrUse:
419     Builder.EnablePGOCSInstrUse = true;
420     break;
421   default:
422     break;
423   }
424 
425   Builder.populateFunctionPassManager(FPM);
426   Builder.populateModulePassManager(MPM);
427 }
428 
429 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
430   PassManagerBuilder Builder;
431   Builder.VerifyInput = true;
432   if (DisableOptimizations)
433     Builder.OptLevel = 0;
434 
435   if (!DisableInline)
436     Builder.Inliner = createFunctionInliningPass();
437   Builder.populateLTOPassManager(PM);
438 }
439 
440 //===----------------------------------------------------------------------===//
441 // CodeGen-related helper functions.
442 //
443 
444 static CodeGenOpt::Level GetCodeGenOptLevel() {
445   if (CodeGenOptLevel.getNumOccurrences())
446     return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
447   if (OptLevelO1)
448     return CodeGenOpt::Less;
449   if (OptLevelO2)
450     return CodeGenOpt::Default;
451   if (OptLevelO3)
452     return CodeGenOpt::Aggressive;
453   return CodeGenOpt::None;
454 }
455 
456 // Returns the TargetMachine instance or zero if no triple is provided.
457 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
458                                        StringRef FeaturesStr,
459                                        const TargetOptions &Options) {
460   std::string Error;
461   const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
462                                                          Error);
463   // Some modules don't specify a triple, and this is okay.
464   if (!TheTarget) {
465     return nullptr;
466   }
467 
468   return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
469                                         FeaturesStr, Options, getRelocModel(),
470                                         getCodeModel(), GetCodeGenOptLevel());
471 }
472 
473 #ifdef LINK_POLLY_INTO_TOOLS
474 namespace polly {
475 void initializePollyPasses(llvm::PassRegistry &Registry);
476 }
477 #endif
478 
479 //===----------------------------------------------------------------------===//
480 // main for opt
481 //
482 int main(int argc, char **argv) {
483   InitLLVM X(argc, argv);
484 
485   // Enable debug stream buffering.
486   EnableDebugBuffering = true;
487 
488   LLVMContext Context;
489 
490   InitializeAllTargets();
491   InitializeAllTargetMCs();
492   InitializeAllAsmPrinters();
493   InitializeAllAsmParsers();
494 
495   // Initialize passes
496   PassRegistry &Registry = *PassRegistry::getPassRegistry();
497   initializeCore(Registry);
498   initializeCoroutines(Registry);
499   initializeScalarOpts(Registry);
500   initializeObjCARCOpts(Registry);
501   initializeVectorization(Registry);
502   initializeIPO(Registry);
503   initializeAnalysis(Registry);
504   initializeTransformUtils(Registry);
505   initializeInstCombine(Registry);
506   initializeAggressiveInstCombine(Registry);
507   initializeInstrumentation(Registry);
508   initializeTarget(Registry);
509   // For codegen passes, only passes that do IR to IR transformation are
510   // supported.
511   initializeExpandMemCmpPassPass(Registry);
512   initializeScalarizeMaskedMemIntrinPass(Registry);
513   initializeCodeGenPreparePass(Registry);
514   initializeAtomicExpandPass(Registry);
515   initializeRewriteSymbolsLegacyPassPass(Registry);
516   initializeWinEHPreparePass(Registry);
517   initializeDwarfEHPreparePass(Registry);
518   initializeSafeStackLegacyPassPass(Registry);
519   initializeSjLjEHPreparePass(Registry);
520   initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
521   initializeGlobalMergePass(Registry);
522   initializeIndirectBrExpandPassPass(Registry);
523   initializeInterleavedLoadCombinePass(Registry);
524   initializeInterleavedAccessPass(Registry);
525   initializeEntryExitInstrumenterPass(Registry);
526   initializePostInlineEntryExitInstrumenterPass(Registry);
527   initializeUnreachableBlockElimLegacyPassPass(Registry);
528   initializeExpandReductionsPass(Registry);
529   initializeWasmEHPreparePass(Registry);
530   initializeWriteBitcodePassPass(Registry);
531 
532 #ifdef LINK_POLLY_INTO_TOOLS
533   polly::initializePollyPasses(Registry);
534 #endif
535 
536   cl::ParseCommandLineOptions(argc, argv,
537     "llvm .bc -> .bc modular optimizer and analysis printer\n");
538 
539   if (AnalyzeOnly && NoOutput) {
540     errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
541     return 1;
542   }
543 
544   SMDiagnostic Err;
545 
546   Context.setDiscardValueNames(DiscardValueNames);
547   if (!DisableDITypeMap)
548     Context.enableDebugTypeODRUniquing();
549 
550   if (PassRemarksWithHotness)
551     Context.setDiagnosticsHotnessRequested(true);
552 
553   if (PassRemarksHotnessThreshold)
554     Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold);
555 
556   std::unique_ptr<ToolOutputFile> OptRemarkFile;
557   if (RemarksFilename != "") {
558     std::error_code EC;
559     OptRemarkFile =
560         llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
561     if (EC) {
562       errs() << EC.message() << '\n';
563       return 1;
564     }
565     Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
566         RemarksFilename, OptRemarkFile->os()));
567 
568     if (!RemarksPasses.empty())
569       if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses)) {
570         errs() << E << '\n';
571         return 1;
572       }
573   }
574 
575   // Load the input module...
576   std::unique_ptr<Module> M =
577       parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout);
578 
579   if (!M) {
580     Err.print(argv[0], errs());
581     return 1;
582   }
583 
584   // Strip debug info before running the verifier.
585   if (StripDebug)
586     StripDebugInfo(*M);
587 
588   // Erase module-level named metadata, if requested.
589   if (StripNamedMetadata) {
590     while (!M->named_metadata_empty()) {
591       NamedMDNode *NMD = &*M->named_metadata_begin();
592       M->eraseNamedMetadata(NMD);
593     }
594   }
595 
596   // If we are supposed to override the target triple or data layout, do so now.
597   if (!TargetTriple.empty())
598     M->setTargetTriple(Triple::normalize(TargetTriple));
599 
600   // Immediately run the verifier to catch any problems before starting up the
601   // pass pipelines.  Otherwise we can crash on broken code during
602   // doInitialization().
603   if (!NoVerify && verifyModule(*M, &errs())) {
604     errs() << argv[0] << ": " << InputFilename
605            << ": error: input module is broken!\n";
606     return 1;
607   }
608 
609   // Figure out what stream we are supposed to write to...
610   std::unique_ptr<ToolOutputFile> Out;
611   std::unique_ptr<ToolOutputFile> ThinLinkOut;
612   if (NoOutput) {
613     if (!OutputFilename.empty())
614       errs() << "WARNING: The -o (output filename) option is ignored when\n"
615                 "the --disable-output option is used.\n";
616   } else {
617     // Default to standard output.
618     if (OutputFilename.empty())
619       OutputFilename = "-";
620 
621     std::error_code EC;
622     Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
623     if (EC) {
624       errs() << EC.message() << '\n';
625       return 1;
626     }
627 
628     if (!ThinLinkBitcodeFile.empty()) {
629       ThinLinkOut.reset(
630           new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None));
631       if (EC) {
632         errs() << EC.message() << '\n';
633         return 1;
634       }
635     }
636   }
637 
638   Triple ModuleTriple(M->getTargetTriple());
639   std::string CPUStr, FeaturesStr;
640   TargetMachine *Machine = nullptr;
641   const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
642 
643   if (ModuleTriple.getArch()) {
644     CPUStr = getCPUStr();
645     FeaturesStr = getFeaturesStr();
646     Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
647   } else if (ModuleTriple.getArchName() != "unknown" &&
648              ModuleTriple.getArchName() != "") {
649     errs() << argv[0] << ": unrecognized architecture '"
650            << ModuleTriple.getArchName() << "' provided.\n";
651     return 1;
652   }
653 
654   std::unique_ptr<TargetMachine> TM(Machine);
655 
656   // Override function attributes based on CPUStr, FeaturesStr, and command line
657   // flags.
658   setFunctionAttributes(CPUStr, FeaturesStr, *M);
659 
660   // If the output is set to be emitted to standard out, and standard out is a
661   // console, print out a warning message and refuse to do it.  We don't
662   // impress anyone by spewing tons of binary goo to a terminal.
663   if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
664     if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
665       NoOutput = true;
666 
667   if (OutputThinLTOBC)
668     M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
669 
670   if (PassPipeline.getNumOccurrences() > 0) {
671     OutputKind OK = OK_NoOutput;
672     if (!NoOutput)
673       OK = OutputAssembly
674                ? OK_OutputAssembly
675                : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
676 
677     VerifierKind VK = VK_VerifyInAndOut;
678     if (NoVerify)
679       VK = VK_NoVerifier;
680     else if (VerifyEach)
681       VK = VK_VerifyEachPass;
682 
683     // The user has asked to use the new pass manager and provided a pipeline
684     // string. Hand off the rest of the functionality to the new code for that
685     // layer.
686     return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(),
687                            OptRemarkFile.get(), PassPipeline, OK, VK,
688                            PreserveAssemblyUseListOrder,
689                            PreserveBitcodeUseListOrder, EmitSummaryIndex,
690                            EmitModuleHash, EnableDebugify)
691                ? 0
692                : 1;
693   }
694 
695   // Create a PassManager to hold and optimize the collection of passes we are
696   // about to build.
697   OptCustomPassManager Passes;
698   bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach;
699 
700   // Add an appropriate TargetLibraryInfo pass for the module's triple.
701   TargetLibraryInfoImpl TLII(ModuleTriple);
702 
703   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
704   if (DisableSimplifyLibCalls)
705     TLII.disableAllFunctions();
706   Passes.add(new TargetLibraryInfoWrapperPass(TLII));
707 
708   // Add internal analysis passes from the target machine.
709   Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
710                                                      : TargetIRAnalysis()));
711 
712   if (AddOneTimeDebugifyPasses)
713     Passes.add(createDebugifyModulePass());
714 
715   std::unique_ptr<legacy::FunctionPassManager> FPasses;
716   if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
717       OptLevelO3) {
718     FPasses.reset(new legacy::FunctionPassManager(M.get()));
719     FPasses->add(createTargetTransformInfoWrapperPass(
720         TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
721   }
722 
723   if (PrintBreakpoints) {
724     // Default to standard output.
725     if (!Out) {
726       if (OutputFilename.empty())
727         OutputFilename = "-";
728 
729       std::error_code EC;
730       Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC,
731                                               sys::fs::F_None);
732       if (EC) {
733         errs() << EC.message() << '\n';
734         return 1;
735       }
736     }
737     Passes.add(createBreakpointPrinter(Out->os()));
738     NoOutput = true;
739   }
740 
741   if (TM) {
742     // FIXME: We should dyn_cast this when supported.
743     auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
744     Pass *TPC = LTM.createPassConfig(Passes);
745     Passes.add(TPC);
746   }
747 
748   // Create a new optimization pass for each one specified on the command line
749   for (unsigned i = 0; i < PassList.size(); ++i) {
750     if (StandardLinkOpts &&
751         StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
752       AddStandardLinkPasses(Passes);
753       StandardLinkOpts = false;
754     }
755 
756     if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
757       AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
758       OptLevelO0 = false;
759     }
760 
761     if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
762       AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
763       OptLevelO1 = false;
764     }
765 
766     if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
767       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
768       OptLevelO2 = false;
769     }
770 
771     if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
772       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
773       OptLevelOs = false;
774     }
775 
776     if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
777       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
778       OptLevelOz = false;
779     }
780 
781     if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
782       AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
783       OptLevelO3 = false;
784     }
785 
786     const PassInfo *PassInf = PassList[i];
787     Pass *P = nullptr;
788     if (PassInf->getNormalCtor())
789       P = PassInf->getNormalCtor()();
790     else
791       errs() << argv[0] << ": cannot create pass: "
792              << PassInf->getPassName() << "\n";
793     if (P) {
794       PassKind Kind = P->getPassKind();
795       addPass(Passes, P);
796 
797       if (AnalyzeOnly) {
798         switch (Kind) {
799         case PT_BasicBlock:
800           Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
801           break;
802         case PT_Region:
803           Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
804           break;
805         case PT_Loop:
806           Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
807           break;
808         case PT_Function:
809           Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
810           break;
811         case PT_CallGraphSCC:
812           Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
813           break;
814         default:
815           Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
816           break;
817         }
818       }
819     }
820 
821     if (PrintEachXForm)
822       Passes.add(
823           createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
824   }
825 
826   if (StandardLinkOpts) {
827     AddStandardLinkPasses(Passes);
828     StandardLinkOpts = false;
829   }
830 
831   if (OptLevelO0)
832     AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
833 
834   if (OptLevelO1)
835     AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
836 
837   if (OptLevelO2)
838     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
839 
840   if (OptLevelOs)
841     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
842 
843   if (OptLevelOz)
844     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
845 
846   if (OptLevelO3)
847     AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
848 
849   if (FPasses) {
850     FPasses->doInitialization();
851     for (Function &F : *M)
852       FPasses->run(F);
853     FPasses->doFinalization();
854   }
855 
856   // Check that the module is well formed on completion of optimization
857   if (!NoVerify && !VerifyEach)
858     Passes.add(createVerifierPass());
859 
860   if (AddOneTimeDebugifyPasses)
861     Passes.add(createCheckDebugifyModulePass(false));
862 
863   // In run twice mode, we want to make sure the output is bit-by-bit
864   // equivalent if we run the pass manager again, so setup two buffers and
865   // a stream to write to them. Note that llc does something similar and it
866   // may be worth to abstract this out in the future.
867   SmallVector<char, 0> Buffer;
868   SmallVector<char, 0> FirstRunBuffer;
869   std::unique_ptr<raw_svector_ostream> BOS;
870   raw_ostream *OS = nullptr;
871 
872   // Write bitcode or assembly to the output as the last step...
873   if (!NoOutput && !AnalyzeOnly) {
874     assert(Out);
875     OS = &Out->os();
876     if (RunTwice) {
877       BOS = make_unique<raw_svector_ostream>(Buffer);
878       OS = BOS.get();
879     }
880     if (OutputAssembly) {
881       if (EmitSummaryIndex)
882         report_fatal_error("Text output is incompatible with -module-summary");
883       if (EmitModuleHash)
884         report_fatal_error("Text output is incompatible with -module-hash");
885       Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
886     } else if (OutputThinLTOBC)
887       Passes.add(createWriteThinLTOBitcodePass(
888           *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
889     else
890       Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
891                                          EmitSummaryIndex, EmitModuleHash));
892   }
893 
894   // Before executing passes, print the final values of the LLVM options.
895   cl::PrintOptionValues();
896 
897   if (!RunTwice) {
898     // Now that we have all of the passes ready, run them.
899     Passes.run(*M);
900   } else {
901     // If requested, run all passes twice with the same pass manager to catch
902     // bugs caused by persistent state in the passes.
903     std::unique_ptr<Module> M2(CloneModule(*M));
904     // Run all passes on the original module first, so the second run processes
905     // the clone to catch CloneModule bugs.
906     Passes.run(*M);
907     FirstRunBuffer = Buffer;
908     Buffer.clear();
909 
910     Passes.run(*M2);
911 
912     // Compare the two outputs and make sure they're the same
913     assert(Out);
914     if (Buffer.size() != FirstRunBuffer.size() ||
915         (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
916       errs()
917           << "Running the pass manager twice changed the output.\n"
918              "Writing the result of the second run to the specified output.\n"
919              "To generate the one-run comparison binary, just run without\n"
920              "the compile-twice option\n";
921       Out->os() << BOS->str();
922       Out->keep();
923       if (OptRemarkFile)
924         OptRemarkFile->keep();
925       return 1;
926     }
927     Out->os() << BOS->str();
928   }
929 
930   if (DebugifyEach && !DebugifyExport.empty())
931     exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
932 
933   // Declare success.
934   if (!NoOutput || PrintBreakpoints)
935     Out->keep();
936 
937   if (OptRemarkFile)
938     OptRemarkFile->keep();
939 
940   if (ThinLinkOut)
941     ThinLinkOut->keep();
942 
943   return 0;
944 }
945