xref: /llvm-project-15.0.7/llvm/tools/llc/llc.cpp (revision 5ac257da)
1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
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 // This is the llc code generator driver. It provides a convenient
10 // command-line interface for generating native assembly-language code
11 // or C code, given LLVM bitcode.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/ScopeExit.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/CodeGen/CommandFlags.h"
20 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
21 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
22 #include "llvm/CodeGen/MIRParser/MIRParser.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/CodeGen/TargetSubtargetInfo.h"
27 #include "llvm/IR/AutoUpgrade.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/DiagnosticPrinter.h"
31 #include "llvm/IR/IRPrintingPasses.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/LLVMRemarkStreamer.h"
34 #include "llvm/IR/LegacyPassManager.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Verifier.h"
37 #include "llvm/IRReader/IRReader.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/MC/SubtargetFeature.h"
40 #include "llvm/MC/TargetRegistry.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Remarks/HotnessThresholdParser.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/FormattedStream.h"
47 #include "llvm/Support/Host.h"
48 #include "llvm/Support/InitLLVM.h"
49 #include "llvm/Support/ManagedStatic.h"
50 #include "llvm/Support/PluginLoader.h"
51 #include "llvm/Support/SourceMgr.h"
52 #include "llvm/Support/TargetSelect.h"
53 #include "llvm/Support/TimeProfiler.h"
54 #include "llvm/Support/ToolOutputFile.h"
55 #include "llvm/Support/WithColor.h"
56 #include "llvm/Target/TargetLoweringObjectFile.h"
57 #include "llvm/Target/TargetMachine.h"
58 #include "llvm/Transforms/Utils/Cloning.h"
59 #include <memory>
60 using namespace llvm;
61 
62 static codegen::RegisterCodeGenFlags CGF;
63 
64 // General options for llc.  Other pass-specific options are specified
65 // within the corresponding llc passes, and target-specific options
66 // and back-end code generation options are specified with the target machine.
67 //
68 static cl::opt<std::string>
69 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
70 
71 static cl::opt<std::string>
72 InputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
73 
74 static cl::opt<std::string>
75 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
76 
77 static cl::opt<std::string>
78     SplitDwarfOutputFile("split-dwarf-output",
79                          cl::desc(".dwo output filename"),
80                          cl::value_desc("filename"));
81 
82 static cl::opt<unsigned>
83 TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
84                  cl::value_desc("N"),
85                  cl::desc("Repeat compilation N times for timing"));
86 
87 static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
88 
89 static cl::opt<unsigned> TimeTraceGranularity(
90     "time-trace-granularity",
91     cl::desc(
92         "Minimum time granularity (in microseconds) traced by time profiler"),
93     cl::init(500), cl::Hidden);
94 
95 static cl::opt<std::string>
96     TimeTraceFile("time-trace-file",
97                   cl::desc("Specify time trace file destination"),
98                   cl::value_desc("filename"));
99 
100 static cl::opt<std::string>
101     BinutilsVersion("binutils-version", cl::Hidden,
102                     cl::desc("Produced object files can use all ELF features "
103                              "supported by this binutils version and newer."
104                              "If -no-integrated-as is specified, the generated "
105                              "assembly will consider GNU as support."
106                              "'none' means that all ELF features can be used, "
107                              "regardless of binutils support"));
108 
109 static cl::opt<bool>
110 NoIntegratedAssembler("no-integrated-as", cl::Hidden,
111                       cl::desc("Disable integrated assembler"));
112 
113 static cl::opt<bool>
114     PreserveComments("preserve-as-comments", cl::Hidden,
115                      cl::desc("Preserve Comments in outputted assembly"),
116                      cl::init(true));
117 
118 // Determine optimization level.
119 static cl::opt<char>
120 OptLevel("O",
121          cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
122                   "(default = '-O2')"),
123          cl::Prefix,
124          cl::ZeroOrMore,
125          cl::init(' '));
126 
127 static cl::opt<std::string>
128 TargetTriple("mtriple", cl::desc("Override target triple for module"));
129 
130 static cl::opt<std::string> SplitDwarfFile(
131     "split-dwarf-file",
132     cl::desc(
133         "Specify the name of the .dwo file to encode in the DWARF output"));
134 
135 static cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
136                               cl::desc("Do not verify input module"));
137 
138 static cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls",
139                                              cl::desc("Disable simplify-libcalls"));
140 
141 static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
142                                     cl::desc("Show encoding in .s output"));
143 
144 static cl::opt<bool>
145     DwarfDirectory("dwarf-directory", cl::Hidden,
146                    cl::desc("Use .file directives with an explicit directory"),
147                    cl::init(true));
148 
149 static cl::opt<bool> AsmVerbose("asm-verbose",
150                                 cl::desc("Add comments to directives."),
151                                 cl::init(true));
152 
153 static cl::opt<bool>
154     CompileTwice("compile-twice", cl::Hidden,
155                  cl::desc("Run everything twice, re-using the same pass "
156                           "manager and verify the result is the same."),
157                  cl::init(false));
158 
159 static cl::opt<bool> DiscardValueNames(
160     "discard-value-names",
161     cl::desc("Discard names from Value (other than GlobalValue)."),
162     cl::init(false), cl::Hidden);
163 
164 static cl::list<std::string> IncludeDirs("I", cl::desc("include search path"));
165 
166 static cl::opt<bool> RemarksWithHotness(
167     "pass-remarks-with-hotness",
168     cl::desc("With PGO, include profile count in optimization remarks"),
169     cl::Hidden);
170 
171 static cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
172     RemarksHotnessThreshold(
173         "pass-remarks-hotness-threshold",
174         cl::desc("Minimum profile count required for "
175                  "an optimization remark to be output. "
176                  "Use 'auto' to apply the threshold from profile summary."),
177         cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden);
178 
179 static cl::opt<std::string>
180     RemarksFilename("pass-remarks-output",
181                     cl::desc("Output filename for pass remarks"),
182                     cl::value_desc("filename"));
183 
184 static cl::opt<std::string>
185     RemarksPasses("pass-remarks-filter",
186                   cl::desc("Only record optimization remarks from passes whose "
187                            "names match the given regular expression"),
188                   cl::value_desc("regex"));
189 
190 static cl::opt<std::string> RemarksFormat(
191     "pass-remarks-format",
192     cl::desc("The format used for serializing remarks (default: YAML)"),
193     cl::value_desc("format"), cl::init("yaml"));
194 
195 namespace {
196 static ManagedStatic<std::vector<std::string>> RunPassNames;
197 
198 struct RunPassOption {
199   void operator=(const std::string &Val) const {
200     if (Val.empty())
201       return;
202     SmallVector<StringRef, 8> PassNames;
203     StringRef(Val).split(PassNames, ',', -1, false);
204     for (auto PassName : PassNames)
205       RunPassNames->push_back(std::string(PassName));
206   }
207 };
208 }
209 
210 static RunPassOption RunPassOpt;
211 
212 static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
213     "run-pass",
214     cl::desc("Run compiler only for specified passes (comma separated list)"),
215     cl::value_desc("pass-name"), cl::ZeroOrMore, cl::location(RunPassOpt));
216 
217 static int compileModule(char **, LLVMContext &);
218 
219 [[noreturn]] static void reportError(Twine Msg, StringRef Filename = "") {
220   SmallString<256> Prefix;
221   if (!Filename.empty()) {
222     if (Filename == "-")
223       Filename = "<stdin>";
224     ("'" + Twine(Filename) + "': ").toStringRef(Prefix);
225   }
226   WithColor::error(errs(), "llc") << Prefix << Msg << "\n";
227   exit(1);
228 }
229 
230 [[noreturn]] static void reportError(Error Err, StringRef Filename) {
231   assert(Err);
232   handleAllErrors(createFileError(Filename, std::move(Err)),
233                   [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
234   llvm_unreachable("reportError() should not return");
235 }
236 
237 static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
238                                                        Triple::OSType OS,
239                                                        const char *ProgName) {
240   // If we don't yet have an output filename, make one.
241   if (OutputFilename.empty()) {
242     if (InputFilename == "-")
243       OutputFilename = "-";
244     else {
245       // If InputFilename ends in .bc or .ll, remove it.
246       StringRef IFN = InputFilename;
247       if (IFN.endswith(".bc") || IFN.endswith(".ll"))
248         OutputFilename = std::string(IFN.drop_back(3));
249       else if (IFN.endswith(".mir"))
250         OutputFilename = std::string(IFN.drop_back(4));
251       else
252         OutputFilename = std::string(IFN);
253 
254       switch (codegen::getFileType()) {
255       case CGFT_AssemblyFile:
256         if (TargetName[0] == 'c') {
257           if (TargetName[1] == 0)
258             OutputFilename += ".cbe.c";
259           else if (TargetName[1] == 'p' && TargetName[2] == 'p')
260             OutputFilename += ".cpp";
261           else
262             OutputFilename += ".s";
263         } else
264           OutputFilename += ".s";
265         break;
266       case CGFT_ObjectFile:
267         if (OS == Triple::Win32)
268           OutputFilename += ".obj";
269         else
270           OutputFilename += ".o";
271         break;
272       case CGFT_Null:
273         OutputFilename = "-";
274         break;
275       }
276     }
277   }
278 
279   // Decide if we need "binary" output.
280   bool Binary = false;
281   switch (codegen::getFileType()) {
282   case CGFT_AssemblyFile:
283     break;
284   case CGFT_ObjectFile:
285   case CGFT_Null:
286     Binary = true;
287     break;
288   }
289 
290   // Open the file.
291   std::error_code EC;
292   sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
293   if (!Binary)
294     OpenFlags |= sys::fs::OF_TextWithCRLF;
295   auto FDOut = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
296   if (EC) {
297     reportError(EC.message());
298     return nullptr;
299   }
300 
301   return FDOut;
302 }
303 
304 struct LLCDiagnosticHandler : public DiagnosticHandler {
305   bool *HasError;
306   LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {}
307   bool handleDiagnostics(const DiagnosticInfo &DI) override {
308     if (DI.getKind() == llvm::DK_SrcMgr) {
309       const auto &DISM = cast<DiagnosticInfoSrcMgr>(DI);
310       const SMDiagnostic &SMD = DISM.getSMDiag();
311 
312       if (SMD.getKind() == SourceMgr::DK_Error)
313         *HasError = true;
314 
315       SMD.print(nullptr, errs());
316 
317       // For testing purposes, we print the LocCookie here.
318       if (DISM.isInlineAsmDiag() && DISM.getLocCookie())
319         WithColor::note() << "!srcloc = " << DISM.getLocCookie() << "\n";
320 
321       return true;
322     }
323 
324     if (DI.getSeverity() == DS_Error)
325       *HasError = true;
326 
327     if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
328       if (!Remark->isEnabled())
329         return true;
330 
331     DiagnosticPrinterRawOStream DP(errs());
332     errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
333     DI.print(DP);
334     errs() << "\n";
335     return true;
336   }
337 };
338 
339 // main - Entry point for the llc compiler.
340 //
341 int main(int argc, char **argv) {
342   InitLLVM X(argc, argv);
343 
344   // Enable debug stream buffering.
345   EnableDebugBuffering = true;
346 
347   // Initialize targets first, so that --version shows registered targets.
348   InitializeAllTargets();
349   InitializeAllTargetMCs();
350   InitializeAllAsmPrinters();
351   InitializeAllAsmParsers();
352 
353   // Initialize codegen and IR passes used by llc so that the -print-after,
354   // -print-before, and -stop-after options work.
355   PassRegistry *Registry = PassRegistry::getPassRegistry();
356   initializeCore(*Registry);
357   initializeCodeGen(*Registry);
358   initializeLoopStrengthReducePass(*Registry);
359   initializeLowerIntrinsicsPass(*Registry);
360   initializeEntryExitInstrumenterPass(*Registry);
361   initializePostInlineEntryExitInstrumenterPass(*Registry);
362   initializeUnreachableBlockElimLegacyPassPass(*Registry);
363   initializeConstantHoistingLegacyPassPass(*Registry);
364   initializeScalarOpts(*Registry);
365   initializeVectorization(*Registry);
366   initializeScalarizeMaskedMemIntrinLegacyPassPass(*Registry);
367   initializeExpandReductionsPass(*Registry);
368   initializeExpandVectorPredicationPass(*Registry);
369   initializeHardwareLoopsPass(*Registry);
370   initializeTransformUtils(*Registry);
371   initializeReplaceWithVeclibLegacyPass(*Registry);
372   initializeTLSVariableHoistLegacyPassPass(*Registry);
373 
374   // Initialize debugging passes.
375   initializeScavengerTestPass(*Registry);
376 
377   // Register the target printer for --version.
378   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
379 
380   cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
381 
382   if (TimeTrace)
383     timeTraceProfilerInitialize(TimeTraceGranularity, argv[0]);
384   auto TimeTraceScopeExit = make_scope_exit([]() {
385     if (TimeTrace) {
386       if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
387         handleAllErrors(std::move(E), [&](const StringError &SE) {
388           errs() << SE.getMessage() << "\n";
389         });
390         return;
391       }
392       timeTraceProfilerCleanup();
393     }
394   });
395 
396   LLVMContext Context;
397   Context.setDiscardValueNames(DiscardValueNames);
398 
399   // Set a diagnostic handler that doesn't exit on the first error
400   bool HasError = false;
401   Context.setDiagnosticHandler(
402       std::make_unique<LLCDiagnosticHandler>(&HasError));
403 
404   Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
405       setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
406                                    RemarksFormat, RemarksWithHotness,
407                                    RemarksHotnessThreshold);
408   if (Error E = RemarksFileOrErr.takeError())
409     reportError(std::move(E), RemarksFilename);
410   std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
411 
412   if (InputLanguage != "" && InputLanguage != "ir" && InputLanguage != "mir")
413     reportError("input language must be '', 'IR' or 'MIR'");
414 
415   // Compile the module TimeCompilations times to give better compile time
416   // metrics.
417   for (unsigned I = TimeCompilations; I; --I)
418     if (int RetVal = compileModule(argv, Context))
419       return RetVal;
420 
421   if (RemarksFile)
422     RemarksFile->keep();
423   return 0;
424 }
425 
426 static bool addPass(PassManagerBase &PM, const char *argv0,
427                     StringRef PassName, TargetPassConfig &TPC) {
428   if (PassName == "none")
429     return false;
430 
431   const PassRegistry *PR = PassRegistry::getPassRegistry();
432   const PassInfo *PI = PR->getPassInfo(PassName);
433   if (!PI) {
434     WithColor::error(errs(), argv0)
435         << "run-pass " << PassName << " is not registered.\n";
436     return true;
437   }
438 
439   Pass *P;
440   if (PI->getNormalCtor())
441     P = PI->getNormalCtor()();
442   else {
443     WithColor::error(errs(), argv0)
444         << "cannot create pass: " << PI->getPassName() << "\n";
445     return true;
446   }
447   std::string Banner = std::string("After ") + std::string(P->getPassName());
448   TPC.addMachinePrePasses();
449   PM.add(P);
450   TPC.addMachinePostPasses(Banner);
451 
452   return false;
453 }
454 
455 static int compileModule(char **argv, LLVMContext &Context) {
456   // Load the module to be compiled...
457   SMDiagnostic Err;
458   std::unique_ptr<Module> M;
459   std::unique_ptr<MIRParser> MIR;
460   Triple TheTriple;
461   std::string CPUStr = codegen::getCPUStr(),
462               FeaturesStr = codegen::getFeaturesStr();
463 
464   // Set attributes on functions as loaded from MIR from command line arguments.
465   auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) {
466     codegen::setFunctionAttributes(CPUStr, FeaturesStr, F);
467   };
468 
469   auto MAttrs = codegen::getMAttrs();
470   bool SkipModule = codegen::getMCPU() == "help" ||
471                     (!MAttrs.empty() && MAttrs.front() == "help");
472 
473   CodeGenOpt::Level OLvl = CodeGenOpt::Default;
474   switch (OptLevel) {
475   default:
476     WithColor::error(errs(), argv[0]) << "invalid optimization level.\n";
477     return 1;
478   case ' ': break;
479   case '0': OLvl = CodeGenOpt::None; break;
480   case '1': OLvl = CodeGenOpt::Less; break;
481   case '2': OLvl = CodeGenOpt::Default; break;
482   case '3': OLvl = CodeGenOpt::Aggressive; break;
483   }
484 
485   // Parse 'none' or '$major.$minor'. Disallow -binutils-version=0 because we
486   // use that to indicate the MC default.
487   if (!BinutilsVersion.empty() && BinutilsVersion != "none") {
488     StringRef V = BinutilsVersion.getValue();
489     unsigned Num;
490     if (V.consumeInteger(10, Num) || Num == 0 ||
491         !(V.empty() ||
492           (V.consume_front(".") && !V.consumeInteger(10, Num) && V.empty()))) {
493       WithColor::error(errs(), argv[0])
494           << "invalid -binutils-version, accepting 'none' or major.minor\n";
495       return 1;
496     }
497   }
498   TargetOptions Options;
499   auto InitializeOptions = [&](const Triple &TheTriple) {
500     Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
501     Options.BinutilsVersion =
502         TargetMachine::parseBinutilsVersion(BinutilsVersion);
503     Options.DisableIntegratedAS = NoIntegratedAssembler;
504     Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
505     Options.MCOptions.MCUseDwarfDirectory = DwarfDirectory;
506     Options.MCOptions.AsmVerbose = AsmVerbose;
507     Options.MCOptions.PreserveAsmComments = PreserveComments;
508     Options.MCOptions.IASSearchPaths = IncludeDirs;
509     Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
510   };
511 
512   Optional<Reloc::Model> RM = codegen::getExplicitRelocModel();
513 
514   const Target *TheTarget = nullptr;
515   std::unique_ptr<TargetMachine> Target;
516 
517   // If user just wants to list available options, skip module loading
518   if (!SkipModule) {
519     auto SetDataLayout =
520         [&](StringRef DataLayoutTargetTriple) -> Optional<std::string> {
521       // If we are supposed to override the target triple, do so now.
522       std::string IRTargetTriple = DataLayoutTargetTriple.str();
523       if (!TargetTriple.empty())
524         IRTargetTriple = Triple::normalize(TargetTriple);
525       TheTriple = Triple(IRTargetTriple);
526       if (TheTriple.getTriple().empty())
527         TheTriple.setTriple(sys::getDefaultTargetTriple());
528 
529       std::string Error;
530       TheTarget =
531           TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
532       if (!TheTarget) {
533         WithColor::error(errs(), argv[0]) << Error;
534         exit(1);
535       }
536 
537       // On AIX, setting the relocation model to anything other than PIC is
538       // considered a user error.
539       if (TheTriple.isOSAIX() && RM.hasValue() && *RM != Reloc::PIC_)
540         reportError("invalid relocation model, AIX only supports PIC",
541                     InputFilename);
542 
543       InitializeOptions(TheTriple);
544       Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
545           TheTriple.getTriple(), CPUStr, FeaturesStr, Options, RM,
546           codegen::getExplicitCodeModel(), OLvl));
547       assert(Target && "Could not allocate target machine!");
548 
549       return Target->createDataLayout().getStringRepresentation();
550     };
551     if (InputLanguage == "mir" ||
552         (InputLanguage == "" && StringRef(InputFilename).endswith(".mir"))) {
553       MIR = createMIRParserFromFile(InputFilename, Err, Context,
554                                     setMIRFunctionAttributes);
555       if (MIR)
556         M = MIR->parseIRModule(SetDataLayout);
557     } else {
558       M = parseIRFile(InputFilename, Err, Context, SetDataLayout);
559     }
560     if (!M) {
561       Err.print(argv[0], WithColor::error(errs(), argv[0]));
562       return 1;
563     }
564     if (!TargetTriple.empty())
565       M->setTargetTriple(Triple::normalize(TargetTriple));
566   } else {
567     TheTriple = Triple(Triple::normalize(TargetTriple));
568     if (TheTriple.getTriple().empty())
569       TheTriple.setTriple(sys::getDefaultTargetTriple());
570 
571     // Get the target specific parser.
572     std::string Error;
573     TheTarget =
574         TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
575     if (!TheTarget) {
576       WithColor::error(errs(), argv[0]) << Error;
577       return 1;
578     }
579 
580     // On AIX, setting the relocation model to anything other than PIC is
581     // considered a user error.
582     if (TheTriple.isOSAIX() && RM.hasValue() && *RM != Reloc::PIC_) {
583       WithColor::error(errs(), argv[0])
584           << "invalid relocation model, AIX only supports PIC.\n";
585       return 1;
586     }
587 
588     InitializeOptions(TheTriple);
589     Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
590         TheTriple.getTriple(), CPUStr, FeaturesStr, Options, RM,
591         codegen::getExplicitCodeModel(), OLvl));
592     assert(Target && "Could not allocate target machine!");
593 
594     // If we don't have a module then just exit now. We do this down
595     // here since the CPU/Feature help is underneath the target machine
596     // creation.
597     return 0;
598   }
599 
600   assert(M && "Should have exited if we didn't have a module!");
601   if (codegen::getFloatABIForCalls() != FloatABI::Default)
602     Options.FloatABIType = codegen::getFloatABIForCalls();
603 
604   // Figure out where we are going to send the output.
605   std::unique_ptr<ToolOutputFile> Out =
606       GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]);
607   if (!Out) return 1;
608 
609   // Ensure the filename is passed down to CodeViewDebug.
610   Target->Options.ObjectFilenameForDebug = Out->outputFilename();
611 
612   std::unique_ptr<ToolOutputFile> DwoOut;
613   if (!SplitDwarfOutputFile.empty()) {
614     std::error_code EC;
615     DwoOut = std::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC,
616                                                sys::fs::OF_None);
617     if (EC)
618       reportError(EC.message(), SplitDwarfOutputFile);
619   }
620 
621   // Build up all of the passes that we want to do to the module.
622   legacy::PassManager PM;
623 
624   // Add an appropriate TargetLibraryInfo pass for the module's triple.
625   TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
626 
627   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
628   if (DisableSimplifyLibCalls)
629     TLII.disableAllFunctions();
630   PM.add(new TargetLibraryInfoWrapperPass(TLII));
631 
632   // Verify module immediately to catch problems before doInitialization() is
633   // called on any passes.
634   if (!NoVerify && verifyModule(*M, &errs()))
635     reportError("input module cannot be verified", InputFilename);
636 
637   // Override function attributes based on CPUStr, FeaturesStr, and command line
638   // flags.
639   codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
640 
641   if (mc::getExplicitRelaxAll() && codegen::getFileType() != CGFT_ObjectFile)
642     WithColor::warning(errs(), argv[0])
643         << ": warning: ignoring -mc-relax-all because filetype != obj";
644 
645   {
646     raw_pwrite_stream *OS = &Out->os();
647 
648     // Manually do the buffering rather than using buffer_ostream,
649     // so we can memcmp the contents in CompileTwice mode
650     SmallVector<char, 0> Buffer;
651     std::unique_ptr<raw_svector_ostream> BOS;
652     if ((codegen::getFileType() != CGFT_AssemblyFile &&
653          !Out->os().supportsSeeking()) ||
654         CompileTwice) {
655       BOS = std::make_unique<raw_svector_ostream>(Buffer);
656       OS = BOS.get();
657     }
658 
659     const char *argv0 = argv[0];
660     LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine &>(*Target);
661     MachineModuleInfoWrapperPass *MMIWP =
662         new MachineModuleInfoWrapperPass(&LLVMTM);
663 
664     // Construct a custom pass pipeline that starts after instruction
665     // selection.
666     if (!RunPassNames->empty()) {
667       if (!MIR) {
668         WithColor::warning(errs(), argv[0])
669             << "run-pass is for .mir file only.\n";
670         return 1;
671       }
672       TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM);
673       if (TPC.hasLimitedCodeGenPipeline()) {
674         WithColor::warning(errs(), argv[0])
675             << "run-pass cannot be used with "
676             << TPC.getLimitedCodeGenPipelineReason(" and ") << ".\n";
677         return 1;
678       }
679 
680       TPC.setDisableVerify(NoVerify);
681       PM.add(&TPC);
682       PM.add(MMIWP);
683       TPC.printAndVerify("");
684       for (const std::string &RunPassName : *RunPassNames) {
685         if (addPass(PM, argv0, RunPassName, TPC))
686           return 1;
687       }
688       TPC.setInitialized();
689       PM.add(createPrintMIRPass(*OS));
690       PM.add(createFreeMachineFunctionPass());
691     } else if (Target->addPassesToEmitFile(
692                    PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
693                    codegen::getFileType(), NoVerify, MMIWP)) {
694       reportError("target does not support generation of this file type");
695     }
696 
697     const_cast<TargetLoweringObjectFile *>(LLVMTM.getObjFileLowering())
698         ->Initialize(MMIWP->getMMI().getContext(), *Target);
699     if (MIR) {
700       assert(MMIWP && "Forgot to create MMIWP?");
701       if (MIR->parseMachineFunctions(*M, MMIWP->getMMI()))
702         return 1;
703     }
704 
705     // Before executing passes, print the final values of the LLVM options.
706     cl::PrintOptionValues();
707 
708     // If requested, run the pass manager over the same module again,
709     // to catch any bugs due to persistent state in the passes. Note that
710     // opt has the same functionality, so it may be worth abstracting this out
711     // in the future.
712     SmallVector<char, 0> CompileTwiceBuffer;
713     if (CompileTwice) {
714       std::unique_ptr<Module> M2(llvm::CloneModule(*M));
715       PM.run(*M2);
716       CompileTwiceBuffer = Buffer;
717       Buffer.clear();
718     }
719 
720     PM.run(*M);
721 
722     auto HasError =
723         ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError;
724     if (*HasError)
725       return 1;
726 
727     // Compare the two outputs and make sure they're the same
728     if (CompileTwice) {
729       if (Buffer.size() != CompileTwiceBuffer.size() ||
730           (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
731            0)) {
732         errs()
733             << "Running the pass manager twice changed the output.\n"
734                "Writing the result of the second run to the specified output\n"
735                "To generate the one-run comparison binary, just run without\n"
736                "the compile-twice option\n";
737         Out->os() << Buffer;
738         Out->keep();
739         return 1;
740       }
741     }
742 
743     if (BOS) {
744       Out->os() << Buffer;
745     }
746   }
747 
748   // Declare success.
749   Out->keep();
750   if (DwoOut)
751     DwoOut->keep();
752 
753   return 0;
754 }
755