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