xref: /llvm-project-15.0.7/llvm/tools/opt/opt.cpp (revision 2fa83cb7)
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   initializeStackProtectorPass(Registry);
521   initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
522   initializeGlobalMergePass(Registry);
523   initializeIndirectBrExpandPassPass(Registry);
524   initializeInterleavedLoadCombinePass(Registry);
525   initializeInterleavedAccessPass(Registry);
526   initializeEntryExitInstrumenterPass(Registry);
527   initializePostInlineEntryExitInstrumenterPass(Registry);
528   initializeUnreachableBlockElimLegacyPassPass(Registry);
529   initializeExpandReductionsPass(Registry);
530   initializeWasmEHPreparePass(Registry);
531   initializeWriteBitcodePassPass(Registry);
532   initializeHardwareLoopsPass(Registry);
533 
534 #ifdef LINK_POLLY_INTO_TOOLS
535   polly::initializePollyPasses(Registry);
536 #endif
537 
538   cl::ParseCommandLineOptions(argc, argv,
539     "llvm .bc -> .bc modular optimizer and analysis printer\n");
540 
541   if (AnalyzeOnly && NoOutput) {
542     errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
543     return 1;
544   }
545 
546   SMDiagnostic Err;
547 
548   Context.setDiscardValueNames(DiscardValueNames);
549   if (!DisableDITypeMap)
550     Context.enableDebugTypeODRUniquing();
551 
552   if (PassRemarksWithHotness)
553     Context.setDiagnosticsHotnessRequested(true);
554 
555   if (PassRemarksHotnessThreshold)
556     Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold);
557 
558   std::unique_ptr<ToolOutputFile> OptRemarkFile;
559   if (RemarksFilename != "") {
560     std::error_code EC;
561     OptRemarkFile =
562         llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
563     if (EC) {
564       errs() << EC.message() << '\n';
565       return 1;
566     }
567     Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
568         RemarksFilename,
569         llvm::make_unique<remarks::YAMLSerializer>(OptRemarkFile->os())));
570 
571     if (!RemarksPasses.empty())
572       if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses)) {
573         errs() << E << '\n';
574         return 1;
575       }
576   }
577 
578   // Load the input module...
579   std::unique_ptr<Module> M =
580       parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout);
581 
582   if (!M) {
583     Err.print(argv[0], errs());
584     return 1;
585   }
586 
587   // Strip debug info before running the verifier.
588   if (StripDebug)
589     StripDebugInfo(*M);
590 
591   // Erase module-level named metadata, if requested.
592   if (StripNamedMetadata) {
593     while (!M->named_metadata_empty()) {
594       NamedMDNode *NMD = &*M->named_metadata_begin();
595       M->eraseNamedMetadata(NMD);
596     }
597   }
598 
599   // If we are supposed to override the target triple or data layout, do so now.
600   if (!TargetTriple.empty())
601     M->setTargetTriple(Triple::normalize(TargetTriple));
602 
603   // Immediately run the verifier to catch any problems before starting up the
604   // pass pipelines.  Otherwise we can crash on broken code during
605   // doInitialization().
606   if (!NoVerify && verifyModule(*M, &errs())) {
607     errs() << argv[0] << ": " << InputFilename
608            << ": error: input module is broken!\n";
609     return 1;
610   }
611 
612   // Figure out what stream we are supposed to write to...
613   std::unique_ptr<ToolOutputFile> Out;
614   std::unique_ptr<ToolOutputFile> ThinLinkOut;
615   if (NoOutput) {
616     if (!OutputFilename.empty())
617       errs() << "WARNING: The -o (output filename) option is ignored when\n"
618                 "the --disable-output option is used.\n";
619   } else {
620     // Default to standard output.
621     if (OutputFilename.empty())
622       OutputFilename = "-";
623 
624     std::error_code EC;
625     Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
626     if (EC) {
627       errs() << EC.message() << '\n';
628       return 1;
629     }
630 
631     if (!ThinLinkBitcodeFile.empty()) {
632       ThinLinkOut.reset(
633           new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None));
634       if (EC) {
635         errs() << EC.message() << '\n';
636         return 1;
637       }
638     }
639   }
640 
641   Triple ModuleTriple(M->getTargetTriple());
642   std::string CPUStr, FeaturesStr;
643   TargetMachine *Machine = nullptr;
644   const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
645 
646   if (ModuleTriple.getArch()) {
647     CPUStr = getCPUStr();
648     FeaturesStr = getFeaturesStr();
649     Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
650   } else if (ModuleTriple.getArchName() != "unknown" &&
651              ModuleTriple.getArchName() != "") {
652     errs() << argv[0] << ": unrecognized architecture '"
653            << ModuleTriple.getArchName() << "' provided.\n";
654     return 1;
655   }
656 
657   std::unique_ptr<TargetMachine> TM(Machine);
658 
659   // Override function attributes based on CPUStr, FeaturesStr, and command line
660   // flags.
661   setFunctionAttributes(CPUStr, FeaturesStr, *M);
662 
663   // If the output is set to be emitted to standard out, and standard out is a
664   // console, print out a warning message and refuse to do it.  We don't
665   // impress anyone by spewing tons of binary goo to a terminal.
666   if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
667     if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
668       NoOutput = true;
669 
670   if (OutputThinLTOBC)
671     M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
672 
673   if (PassPipeline.getNumOccurrences() > 0) {
674     OutputKind OK = OK_NoOutput;
675     if (!NoOutput)
676       OK = OutputAssembly
677                ? OK_OutputAssembly
678                : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
679 
680     VerifierKind VK = VK_VerifyInAndOut;
681     if (NoVerify)
682       VK = VK_NoVerifier;
683     else if (VerifyEach)
684       VK = VK_VerifyEachPass;
685 
686     // The user has asked to use the new pass manager and provided a pipeline
687     // string. Hand off the rest of the functionality to the new code for that
688     // layer.
689     return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(),
690                            OptRemarkFile.get(), PassPipeline, OK, VK,
691                            PreserveAssemblyUseListOrder,
692                            PreserveBitcodeUseListOrder, EmitSummaryIndex,
693                            EmitModuleHash, EnableDebugify)
694                ? 0
695                : 1;
696   }
697 
698   // Create a PassManager to hold and optimize the collection of passes we are
699   // about to build.
700   OptCustomPassManager Passes;
701   bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach;
702 
703   // Add an appropriate TargetLibraryInfo pass for the module's triple.
704   TargetLibraryInfoImpl TLII(ModuleTriple);
705 
706   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
707   if (DisableSimplifyLibCalls)
708     TLII.disableAllFunctions();
709   Passes.add(new TargetLibraryInfoWrapperPass(TLII));
710 
711   // Add internal analysis passes from the target machine.
712   Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
713                                                      : TargetIRAnalysis()));
714 
715   if (AddOneTimeDebugifyPasses)
716     Passes.add(createDebugifyModulePass());
717 
718   std::unique_ptr<legacy::FunctionPassManager> FPasses;
719   if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
720       OptLevelO3) {
721     FPasses.reset(new legacy::FunctionPassManager(M.get()));
722     FPasses->add(createTargetTransformInfoWrapperPass(
723         TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
724   }
725 
726   if (PrintBreakpoints) {
727     // Default to standard output.
728     if (!Out) {
729       if (OutputFilename.empty())
730         OutputFilename = "-";
731 
732       std::error_code EC;
733       Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC,
734                                               sys::fs::F_None);
735       if (EC) {
736         errs() << EC.message() << '\n';
737         return 1;
738       }
739     }
740     Passes.add(createBreakpointPrinter(Out->os()));
741     NoOutput = true;
742   }
743 
744   if (TM) {
745     // FIXME: We should dyn_cast this when supported.
746     auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
747     Pass *TPC = LTM.createPassConfig(Passes);
748     Passes.add(TPC);
749   }
750 
751   // Create a new optimization pass for each one specified on the command line
752   for (unsigned i = 0; i < PassList.size(); ++i) {
753     if (StandardLinkOpts &&
754         StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
755       AddStandardLinkPasses(Passes);
756       StandardLinkOpts = false;
757     }
758 
759     if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
760       AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
761       OptLevelO0 = false;
762     }
763 
764     if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
765       AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
766       OptLevelO1 = false;
767     }
768 
769     if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
770       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
771       OptLevelO2 = false;
772     }
773 
774     if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
775       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
776       OptLevelOs = false;
777     }
778 
779     if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
780       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
781       OptLevelOz = false;
782     }
783 
784     if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
785       AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
786       OptLevelO3 = false;
787     }
788 
789     const PassInfo *PassInf = PassList[i];
790     Pass *P = nullptr;
791     if (PassInf->getNormalCtor())
792       P = PassInf->getNormalCtor()();
793     else
794       errs() << argv[0] << ": cannot create pass: "
795              << PassInf->getPassName() << "\n";
796     if (P) {
797       PassKind Kind = P->getPassKind();
798       addPass(Passes, P);
799 
800       if (AnalyzeOnly) {
801         switch (Kind) {
802         case PT_BasicBlock:
803           Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
804           break;
805         case PT_Region:
806           Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
807           break;
808         case PT_Loop:
809           Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
810           break;
811         case PT_Function:
812           Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
813           break;
814         case PT_CallGraphSCC:
815           Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
816           break;
817         default:
818           Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
819           break;
820         }
821       }
822     }
823 
824     if (PrintEachXForm)
825       Passes.add(
826           createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
827   }
828 
829   if (StandardLinkOpts) {
830     AddStandardLinkPasses(Passes);
831     StandardLinkOpts = false;
832   }
833 
834   if (OptLevelO0)
835     AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
836 
837   if (OptLevelO1)
838     AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
839 
840   if (OptLevelO2)
841     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
842 
843   if (OptLevelOs)
844     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
845 
846   if (OptLevelOz)
847     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
848 
849   if (OptLevelO3)
850     AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
851 
852   if (FPasses) {
853     FPasses->doInitialization();
854     for (Function &F : *M)
855       FPasses->run(F);
856     FPasses->doFinalization();
857   }
858 
859   // Check that the module is well formed on completion of optimization
860   if (!NoVerify && !VerifyEach)
861     Passes.add(createVerifierPass());
862 
863   if (AddOneTimeDebugifyPasses)
864     Passes.add(createCheckDebugifyModulePass(false));
865 
866   // In run twice mode, we want to make sure the output is bit-by-bit
867   // equivalent if we run the pass manager again, so setup two buffers and
868   // a stream to write to them. Note that llc does something similar and it
869   // may be worth to abstract this out in the future.
870   SmallVector<char, 0> Buffer;
871   SmallVector<char, 0> FirstRunBuffer;
872   std::unique_ptr<raw_svector_ostream> BOS;
873   raw_ostream *OS = nullptr;
874 
875   // Write bitcode or assembly to the output as the last step...
876   if (!NoOutput && !AnalyzeOnly) {
877     assert(Out);
878     OS = &Out->os();
879     if (RunTwice) {
880       BOS = make_unique<raw_svector_ostream>(Buffer);
881       OS = BOS.get();
882     }
883     if (OutputAssembly) {
884       if (EmitSummaryIndex)
885         report_fatal_error("Text output is incompatible with -module-summary");
886       if (EmitModuleHash)
887         report_fatal_error("Text output is incompatible with -module-hash");
888       Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
889     } else if (OutputThinLTOBC)
890       Passes.add(createWriteThinLTOBitcodePass(
891           *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
892     else
893       Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
894                                          EmitSummaryIndex, EmitModuleHash));
895   }
896 
897   // Before executing passes, print the final values of the LLVM options.
898   cl::PrintOptionValues();
899 
900   if (!RunTwice) {
901     // Now that we have all of the passes ready, run them.
902     Passes.run(*M);
903   } else {
904     // If requested, run all passes twice with the same pass manager to catch
905     // bugs caused by persistent state in the passes.
906     std::unique_ptr<Module> M2(CloneModule(*M));
907     // Run all passes on the original module first, so the second run processes
908     // the clone to catch CloneModule bugs.
909     Passes.run(*M);
910     FirstRunBuffer = Buffer;
911     Buffer.clear();
912 
913     Passes.run(*M2);
914 
915     // Compare the two outputs and make sure they're the same
916     assert(Out);
917     if (Buffer.size() != FirstRunBuffer.size() ||
918         (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
919       errs()
920           << "Running the pass manager twice changed the output.\n"
921              "Writing the result of the second run to the specified output.\n"
922              "To generate the one-run comparison binary, just run without\n"
923              "the compile-twice option\n";
924       Out->os() << BOS->str();
925       Out->keep();
926       if (OptRemarkFile)
927         OptRemarkFile->keep();
928       return 1;
929     }
930     Out->os() << BOS->str();
931   }
932 
933   if (DebugifyEach && !DebugifyExport.empty())
934     exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
935 
936   // Declare success.
937   if (!NoOutput || PrintBreakpoints)
938     Out->keep();
939 
940   if (OptRemarkFile)
941     OptRemarkFile->keep();
942 
943   if (ThinLinkOut)
944     ThinLinkOut->keep();
945 
946   return 0;
947 }
948