1193323Sed//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
2193323Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193323Sed//
7193323Sed//===----------------------------------------------------------------------===//
8193323Sed//
9193323Sed// This is the llc code generator driver. It provides a convenient
10193323Sed// command-line interface for generating native assembly-language code
11193323Sed// or C code, given LLVM bitcode.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15288943Sdim#include "llvm/ADT/STLExtras.h"
16198090Srdivacky#include "llvm/ADT/Triple.h"
17288943Sdim#include "llvm/Analysis/TargetLibraryInfo.h"
18341825Sdim#include "llvm/CodeGen/CommandFlags.inc"
19198090Srdivacky#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
20198090Srdivacky#include "llvm/CodeGen/LinkAllCodegenComponents.h"
21288943Sdim#include "llvm/CodeGen/MIRParser/MIRParser.h"
22309124Sdim#include "llvm/CodeGen/MachineFunctionPass.h"
23309124Sdim#include "llvm/CodeGen/MachineModuleInfo.h"
24309124Sdim#include "llvm/CodeGen/TargetPassConfig.h"
25327952Sdim#include "llvm/CodeGen/TargetSubtargetInfo.h"
26341825Sdim#include "llvm/IR/AutoUpgrade.h"
27249423Sdim#include "llvm/IR/DataLayout.h"
28309124Sdim#include "llvm/IR/DiagnosticInfo.h"
29309124Sdim#include "llvm/IR/DiagnosticPrinter.h"
30276479Sdim#include "llvm/IR/IRPrintingPasses.h"
31276479Sdim#include "llvm/IR/LLVMContext.h"
32288943Sdim#include "llvm/IR/LegacyPassManager.h"
33249423Sdim#include "llvm/IR/Module.h"
34353358Sdim#include "llvm/IR/RemarkStreamer.h"
35288943Sdim#include "llvm/IR/Verifier.h"
36249423Sdim#include "llvm/IRReader/IRReader.h"
37360784Sdim#include "llvm/InitializePasses.h"
38224133Sdim#include "llvm/MC/SubtargetFeature.h"
39249423Sdim#include "llvm/Pass.h"
40193323Sed#include "llvm/Support/CommandLine.h"
41202375Srdivacky#include "llvm/Support/Debug.h"
42276479Sdim#include "llvm/Support/FileSystem.h"
43198090Srdivacky#include "llvm/Support/FormattedStream.h"
44249423Sdim#include "llvm/Support/Host.h"
45341825Sdim#include "llvm/Support/InitLLVM.h"
46193323Sed#include "llvm/Support/ManagedStatic.h"
47193323Sed#include "llvm/Support/PluginLoader.h"
48249423Sdim#include "llvm/Support/SourceMgr.h"
49226584Sdim#include "llvm/Support/TargetRegistry.h"
50226584Sdim#include "llvm/Support/TargetSelect.h"
51249423Sdim#include "llvm/Support/ToolOutputFile.h"
52341825Sdim#include "llvm/Support/WithColor.h"
53198090Srdivacky#include "llvm/Target/TargetMachine.h"
54296417Sdim#include "llvm/Transforms/Utils/Cloning.h"
55193323Sed#include <memory>
56193323Sedusing namespace llvm;
57193323Sed
58193323Sed// General options for llc.  Other pass-specific options are specified
59193323Sed// within the corresponding llc passes, and target-specific options
60193323Sed// and back-end code generation options are specified with the target machine.
61193323Sed//
62193323Sedstatic cl::opt<std::string>
63193323SedInputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
64193323Sed
65193323Sedstatic cl::opt<std::string>
66321369SdimInputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
67321369Sdim
68321369Sdimstatic cl::opt<std::string>
69193323SedOutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
70193323Sed
71341825Sdimstatic cl::opt<std::string>
72341825Sdim    SplitDwarfOutputFile("split-dwarf-output",
73341825Sdim                         cl::desc(".dwo output filename"),
74341825Sdim                         cl::value_desc("filename"));
75341825Sdim
76249423Sdimstatic cl::opt<unsigned>
77249423SdimTimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
78249423Sdim                 cl::value_desc("N"),
79249423Sdim                 cl::desc("Repeat compilation N times for timing"));
80249423Sdim
81276479Sdimstatic cl::opt<bool>
82276479SdimNoIntegratedAssembler("no-integrated-as", cl::Hidden,
83276479Sdim                      cl::desc("Disable integrated assembler"));
84276479Sdim
85309124Sdimstatic cl::opt<bool>
86309124Sdim    PreserveComments("preserve-as-comments", cl::Hidden,
87309124Sdim                     cl::desc("Preserve Comments in outputted assembly"),
88309124Sdim                     cl::init(true));
89309124Sdim
90193323Sed// Determine optimization level.
91193323Sedstatic cl::opt<char>
92193323SedOptLevel("O",
93193323Sed         cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
94193323Sed                  "(default = '-O2')"),
95193323Sed         cl::Prefix,
96193323Sed         cl::ZeroOrMore,
97193323Sed         cl::init(' '));
98193323Sed
99193323Sedstatic cl::opt<std::string>
100193323SedTargetTriple("mtriple", cl::desc("Override target triple for module"));
101193323Sed
102321369Sdimstatic cl::opt<std::string> SplitDwarfFile(
103321369Sdim    "split-dwarf-file",
104321369Sdim    cl::desc(
105321369Sdim        "Specify the name of the .dwo file to encode in the DWARF output"));
106321369Sdim
107276479Sdimstatic cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
108276479Sdim                              cl::desc("Do not verify input module"));
109193323Sed
110276479Sdimstatic cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls",
111276479Sdim                                             cl::desc("Disable simplify-libcalls"));
112239462Sdim
113276479Sdimstatic cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
114276479Sdim                                    cl::desc("Show encoding in .s output"));
115249423Sdim
116276479Sdimstatic cl::opt<bool> EnableDwarfDirectory(
117276479Sdim    "enable-dwarf-directory", cl::Hidden,
118276479Sdim    cl::desc("Use .file directives with an explicit directory."));
119276479Sdim
120276479Sdimstatic cl::opt<bool> AsmVerbose("asm-verbose",
121276479Sdim                                cl::desc("Add comments to directives."),
122276479Sdim                                cl::init(true));
123276479Sdim
124296417Sdimstatic cl::opt<bool>
125296417Sdim    CompileTwice("compile-twice", cl::Hidden,
126296417Sdim                 cl::desc("Run everything twice, re-using the same pass "
127296417Sdim                          "manager and verify the result is the same."),
128296417Sdim                 cl::init(false));
129296417Sdim
130309124Sdimstatic cl::opt<bool> DiscardValueNames(
131309124Sdim    "discard-value-names",
132309124Sdim    cl::desc("Discard names from Value (other than GlobalValue)."),
133309124Sdim    cl::init(false), cl::Hidden);
134309124Sdim
135314564Sdimstatic cl::list<std::string> IncludeDirs("I", cl::desc("include search path"));
136314564Sdim
137353358Sdimstatic cl::opt<bool> RemarksWithHotness(
138321369Sdim    "pass-remarks-with-hotness",
139321369Sdim    cl::desc("With PGO, include profile count in optimization remarks"),
140321369Sdim    cl::Hidden);
141321369Sdim
142353358Sdimstatic cl::opt<unsigned>
143353358Sdim    RemarksHotnessThreshold("pass-remarks-hotness-threshold",
144353358Sdim                            cl::desc("Minimum profile count required for "
145353358Sdim                                     "an optimization remark to be output"),
146353358Sdim                            cl::Hidden);
147321369Sdim
148321369Sdimstatic cl::opt<std::string>
149321369Sdim    RemarksFilename("pass-remarks-output",
150353358Sdim                    cl::desc("Output filename for pass remarks"),
151321369Sdim                    cl::value_desc("filename"));
152321369Sdim
153353358Sdimstatic cl::opt<std::string>
154353358Sdim    RemarksPasses("pass-remarks-filter",
155353358Sdim                  cl::desc("Only record optimization remarks from passes whose "
156353358Sdim                           "names match the given regular expression"),
157353358Sdim                  cl::value_desc("regex"));
158353358Sdim
159353358Sdimstatic cl::opt<std::string> RemarksFormat(
160353358Sdim    "pass-remarks-format",
161353358Sdim    cl::desc("The format used for serializing remarks (default: YAML)"),
162353358Sdim    cl::value_desc("format"), cl::init("yaml"));
163353358Sdim
164309124Sdimnamespace {
165309124Sdimstatic ManagedStatic<std::vector<std::string>> RunPassNames;
166309124Sdim
167309124Sdimstruct RunPassOption {
168309124Sdim  void operator=(const std::string &Val) const {
169309124Sdim    if (Val.empty())
170309124Sdim      return;
171309124Sdim    SmallVector<StringRef, 8> PassNames;
172309124Sdim    StringRef(Val).split(PassNames, ',', -1, false);
173309124Sdim    for (auto PassName : PassNames)
174309124Sdim      RunPassNames->push_back(PassName);
175309124Sdim  }
176309124Sdim};
177309124Sdim}
178309124Sdim
179309124Sdimstatic RunPassOption RunPassOpt;
180309124Sdim
181309124Sdimstatic cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
182309124Sdim    "run-pass",
183309124Sdim    cl::desc("Run compiler only for specified passes (comma separated list)"),
184309124Sdim    cl::value_desc("pass-name"), cl::ZeroOrMore, cl::location(RunPassOpt));
185309124Sdim
186276479Sdimstatic int compileModule(char **, LLVMContext &);
187276479Sdim
188327952Sdimstatic std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
189327952Sdim                                                       Triple::OSType OS,
190327952Sdim                                                       const char *ProgName) {
191212793Sdim  // If we don't yet have an output filename, make one.
192212793Sdim  if (OutputFilename.empty()) {
193212793Sdim    if (InputFilename == "-")
194212793Sdim      OutputFilename = "-";
195212793Sdim    else {
196280031Sdim      // If InputFilename ends in .bc or .ll, remove it.
197280031Sdim      StringRef IFN = InputFilename;
198280031Sdim      if (IFN.endswith(".bc") || IFN.endswith(".ll"))
199280031Sdim        OutputFilename = IFN.drop_back(3);
200288943Sdim      else if (IFN.endswith(".mir"))
201288943Sdim        OutputFilename = IFN.drop_back(4);
202280031Sdim      else
203280031Sdim        OutputFilename = IFN;
204193323Sed
205212793Sdim      switch (FileType) {
206360784Sdim      case CGFT_AssemblyFile:
207212793Sdim        if (TargetName[0] == 'c') {
208212793Sdim          if (TargetName[1] == 0)
209212793Sdim            OutputFilename += ".cbe.c";
210212793Sdim          else if (TargetName[1] == 'p' && TargetName[2] == 'p')
211212793Sdim            OutputFilename += ".cpp";
212212793Sdim          else
213212793Sdim            OutputFilename += ".s";
214212793Sdim        } else
215212793Sdim          OutputFilename += ".s";
216212793Sdim        break;
217360784Sdim      case CGFT_ObjectFile:
218212793Sdim        if (OS == Triple::Win32)
219212793Sdim          OutputFilename += ".obj";
220212793Sdim        else
221212793Sdim          OutputFilename += ".o";
222212793Sdim        break;
223360784Sdim      case CGFT_Null:
224212793Sdim        OutputFilename += ".null";
225212793Sdim        break;
226212793Sdim      }
227193323Sed    }
228193323Sed  }
229193323Sed
230212793Sdim  // Decide if we need "binary" output.
231193323Sed  bool Binary = false;
232193323Sed  switch (FileType) {
233360784Sdim  case CGFT_AssemblyFile:
234193323Sed    break;
235360784Sdim  case CGFT_ObjectFile:
236360784Sdim  case CGFT_Null:
237193323Sed    Binary = true;
238193323Sed    break;
239193323Sed  }
240193323Sed
241212793Sdim  // Open the file.
242280031Sdim  std::error_code EC;
243360784Sdim  sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
244276479Sdim  if (!Binary)
245360784Sdim    OpenFlags |= sys::fs::OF_Text;
246360784Sdim  auto FDOut = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
247280031Sdim  if (EC) {
248341825Sdim    WithColor::error() << EC.message() << '\n';
249276479Sdim    return nullptr;
250193323Sed  }
251193323Sed
252212793Sdim  return FDOut;
253193323Sed}
254193323Sed
255327952Sdimstruct LLCDiagnosticHandler : public DiagnosticHandler {
256327952Sdim  bool *HasError;
257327952Sdim  LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {}
258327952Sdim  bool handleDiagnostics(const DiagnosticInfo &DI) override {
259327952Sdim    if (DI.getSeverity() == DS_Error)
260327952Sdim      *HasError = true;
261309124Sdim
262327952Sdim    if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
263327952Sdim      if (!Remark->isEnabled())
264327952Sdim        return true;
265321369Sdim
266327952Sdim    DiagnosticPrinterRawOStream DP(errs());
267327952Sdim    errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
268327952Sdim    DI.print(DP);
269327952Sdim    errs() << "\n";
270327952Sdim    return true;
271327952Sdim  }
272327952Sdim};
273309124Sdim
274321369Sdimstatic void InlineAsmDiagHandler(const SMDiagnostic &SMD, void *Context,
275321369Sdim                                 unsigned LocCookie) {
276321369Sdim  bool *HasError = static_cast<bool *>(Context);
277321369Sdim  if (SMD.getKind() == SourceMgr::DK_Error)
278321369Sdim    *HasError = true;
279321369Sdim
280321369Sdim  SMD.print(nullptr, errs());
281321369Sdim
282321369Sdim  // For testing purposes, we print the LocCookie here.
283321369Sdim  if (LocCookie)
284341825Sdim    WithColor::note() << "!srcloc = " << LocCookie << "\n";
285321369Sdim}
286321369Sdim
287193323Sed// main - Entry point for the llc compiler.
288193323Sed//
289193323Sedint main(int argc, char **argv) {
290341825Sdim  InitLLVM X(argc, argv);
291202375Srdivacky
292202375Srdivacky  // Enable debug stream buffering.
293202375Srdivacky  EnableDebugBuffering = true;
294202375Srdivacky
295309124Sdim  LLVMContext Context;
296193323Sed
297198090Srdivacky  // Initialize targets first, so that --version shows registered targets.
298194612Sed  InitializeAllTargets();
299226584Sdim  InitializeAllTargetMCs();
300194612Sed  InitializeAllAsmPrinters();
301206274Srdivacky  InitializeAllAsmParsers();
302198090Srdivacky
303239462Sdim  // Initialize codegen and IR passes used by llc so that the -print-after,
304239462Sdim  // -print-before, and -stop-after options work.
305239462Sdim  PassRegistry *Registry = PassRegistry::getPassRegistry();
306239462Sdim  initializeCore(*Registry);
307239462Sdim  initializeCodeGen(*Registry);
308239462Sdim  initializeLoopStrengthReducePass(*Registry);
309239462Sdim  initializeLowerIntrinsicsPass(*Registry);
310327952Sdim  initializeEntryExitInstrumenterPass(*Registry);
311327952Sdim  initializePostInlineEntryExitInstrumenterPass(*Registry);
312309124Sdim  initializeUnreachableBlockElimLegacyPassPass(*Registry);
313314564Sdim  initializeConstantHoistingLegacyPassPass(*Registry);
314321369Sdim  initializeScalarOpts(*Registry);
315321369Sdim  initializeVectorization(*Registry);
316321369Sdim  initializeScalarizeMaskedMemIntrinPass(*Registry);
317321369Sdim  initializeExpandReductionsPass(*Registry);
318353358Sdim  initializeHardwareLoopsPass(*Registry);
319239462Sdim
320321369Sdim  // Initialize debugging passes.
321321369Sdim  initializeScavengerTestPass(*Registry);
322321369Sdim
323226584Sdim  // Register the target printer for --version.
324226584Sdim  cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
325226584Sdim
326198090Srdivacky  cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
327221337Sdim
328309124Sdim  Context.setDiscardValueNames(DiscardValueNames);
329309124Sdim
330309124Sdim  // Set a diagnostic handler that doesn't exit on the first error
331309124Sdim  bool HasError = false;
332327952Sdim  Context.setDiagnosticHandler(
333360784Sdim      std::make_unique<LLCDiagnosticHandler>(&HasError));
334321369Sdim  Context.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, &HasError);
335309124Sdim
336353358Sdim  Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
337353358Sdim      setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
338353358Sdim                               RemarksFormat, RemarksWithHotness,
339353358Sdim                               RemarksHotnessThreshold);
340353358Sdim  if (Error E = RemarksFileOrErr.takeError()) {
341353358Sdim    WithColor::error(errs(), argv[0]) << toString(std::move(E)) << '\n';
342353358Sdim    return 1;
343321369Sdim  }
344353358Sdim  std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
345321369Sdim
346321369Sdim  if (InputLanguage != "" && InputLanguage != "ir" &&
347321369Sdim      InputLanguage != "mir") {
348341825Sdim    WithColor::error(errs(), argv[0])
349341825Sdim        << "input language must be '', 'IR' or 'MIR'\n";
350321369Sdim    return 1;
351321369Sdim  }
352321369Sdim
353249423Sdim  // Compile the module TimeCompilations times to give better compile time
354249423Sdim  // metrics.
355249423Sdim  for (unsigned I = TimeCompilations; I; --I)
356249423Sdim    if (int RetVal = compileModule(argv, Context))
357249423Sdim      return RetVal;
358321369Sdim
359353358Sdim  if (RemarksFile)
360353358Sdim    RemarksFile->keep();
361249423Sdim  return 0;
362249423Sdim}
363249423Sdim
364309124Sdimstatic bool addPass(PassManagerBase &PM, const char *argv0,
365309124Sdim                    StringRef PassName, TargetPassConfig &TPC) {
366309124Sdim  if (PassName == "none")
367309124Sdim    return false;
368309124Sdim
369309124Sdim  const PassRegistry *PR = PassRegistry::getPassRegistry();
370309124Sdim  const PassInfo *PI = PR->getPassInfo(PassName);
371309124Sdim  if (!PI) {
372341825Sdim    WithColor::error(errs(), argv0)
373341825Sdim        << "run-pass " << PassName << " is not registered.\n";
374309124Sdim    return true;
375309124Sdim  }
376309124Sdim
377309124Sdim  Pass *P;
378321369Sdim  if (PI->getNormalCtor())
379309124Sdim    P = PI->getNormalCtor()();
380309124Sdim  else {
381341825Sdim    WithColor::error(errs(), argv0)
382341825Sdim        << "cannot create pass: " << PI->getPassName() << "\n";
383309124Sdim    return true;
384309124Sdim  }
385309124Sdim  std::string Banner = std::string("After ") + std::string(P->getPassName());
386309124Sdim  PM.add(P);
387309124Sdim  TPC.printAndVerify(Banner);
388309124Sdim
389309124Sdim  return false;
390309124Sdim}
391309124Sdim
392249423Sdimstatic int compileModule(char **argv, LLVMContext &Context) {
393193323Sed  // Load the module to be compiled...
394198090Srdivacky  SMDiagnostic Err;
395276479Sdim  std::unique_ptr<Module> M;
396288943Sdim  std::unique_ptr<MIRParser> MIR;
397239462Sdim  Triple TheTriple;
398360784Sdim  std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
399193323Sed
400360784Sdim  // Set attributes on functions as loaded from MIR from command line arguments.
401360784Sdim  auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) {
402360784Sdim    setFunctionAttributes(CPUStr, FeaturesStr, F);
403360784Sdim  };
404360784Sdim
405239462Sdim  bool SkipModule = MCPU == "help" ||
406239462Sdim                    (!MAttrs.empty() && MAttrs.front() == "help");
407193323Sed
408239462Sdim  // If user just wants to list available options, skip module loading
409239462Sdim  if (!SkipModule) {
410321369Sdim    if (InputLanguage == "mir" ||
411321369Sdim        (InputLanguage == "" && StringRef(InputFilename).endswith(".mir"))) {
412360784Sdim      MIR = createMIRParserFromFile(InputFilename, Err, Context,
413360784Sdim                                    setMIRFunctionAttributes);
414309124Sdim      if (MIR)
415321369Sdim        M = MIR->parseIRModule();
416288943Sdim    } else
417341825Sdim      M = parseIRFile(InputFilename, Err, Context, false);
418280031Sdim    if (!M) {
419341825Sdim      Err.print(argv[0], WithColor::error(errs(), argv[0]));
420198090Srdivacky      return 1;
421198090Srdivacky    }
422198090Srdivacky
423239462Sdim    // If we are supposed to override the target triple, do so now.
424239462Sdim    if (!TargetTriple.empty())
425280031Sdim      M->setTargetTriple(Triple::normalize(TargetTriple));
426280031Sdim    TheTriple = Triple(M->getTargetTriple());
427198090Srdivacky  } else {
428239462Sdim    TheTriple = Triple(Triple::normalize(TargetTriple));
429193323Sed  }
430193323Sed
431239462Sdim  if (TheTriple.getTriple().empty())
432239462Sdim    TheTriple.setTriple(sys::getDefaultTargetTriple());
433239462Sdim
434239462Sdim  // Get the target specific parser.
435239462Sdim  std::string Error;
436239462Sdim  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
437239462Sdim                                                         Error);
438239462Sdim  if (!TheTarget) {
439341825Sdim    WithColor::error(errs(), argv[0]) << Error;
440239462Sdim    return 1;
441239462Sdim  }
442239462Sdim
443234353Sdim  CodeGenOpt::Level OLvl = CodeGenOpt::Default;
444234353Sdim  switch (OptLevel) {
445234353Sdim  default:
446341825Sdim    WithColor::error(errs(), argv[0]) << "invalid optimization level.\n";
447234353Sdim    return 1;
448234353Sdim  case ' ': break;
449234353Sdim  case '0': OLvl = CodeGenOpt::None; break;
450234353Sdim  case '1': OLvl = CodeGenOpt::Less; break;
451234353Sdim  case '2': OLvl = CodeGenOpt::Default; break;
452234353Sdim  case '3': OLvl = CodeGenOpt::Aggressive; break;
453234353Sdim  }
454234353Sdim
455276479Sdim  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
456276479Sdim  Options.DisableIntegratedAS = NoIntegratedAssembler;
457276479Sdim  Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
458276479Sdim  Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory;
459276479Sdim  Options.MCOptions.AsmVerbose = AsmVerbose;
460309124Sdim  Options.MCOptions.PreserveAsmComments = PreserveComments;
461314564Sdim  Options.MCOptions.IASSearchPaths = IncludeDirs;
462321369Sdim  Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
463234353Sdim
464327952Sdim  std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
465327952Sdim      TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
466327952Sdim      getCodeModel(), OLvl));
467288943Sdim
468280031Sdim  assert(Target && "Could not allocate target machine!");
469193323Sed
470276479Sdim  // If we don't have a module then just exit now. We do this down
471276479Sdim  // here since the CPU/Feature help is underneath the target machine
472276479Sdim  // creation.
473276479Sdim  if (SkipModule)
474276479Sdim    return 0;
475218885Sdim
476280031Sdim  assert(M && "Should have exited if we didn't have a module!");
477288943Sdim  if (FloatABIForCalls != FloatABI::Default)
478288943Sdim    Options.FloatABIType = FloatABIForCalls;
479221337Sdim
480239462Sdim  // Figure out where we are going to send the output.
481327952Sdim  std::unique_ptr<ToolOutputFile> Out =
482280031Sdim      GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]);
483212793Sdim  if (!Out) return 1;
484193323Sed
485341825Sdim  std::unique_ptr<ToolOutputFile> DwoOut;
486341825Sdim  if (!SplitDwarfOutputFile.empty()) {
487341825Sdim    std::error_code EC;
488360784Sdim    DwoOut = std::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC,
489360784Sdim                                               sys::fs::OF_None);
490341825Sdim    if (EC) {
491341825Sdim      WithColor::error(errs(), argv[0]) << EC.message() << '\n';
492341825Sdim      return 1;
493341825Sdim    }
494341825Sdim  }
495341825Sdim
496208599Srdivacky  // Build up all of the passes that we want to do to the module.
497288943Sdim  legacy::PassManager PM;
498198090Srdivacky
499239462Sdim  // Add an appropriate TargetLibraryInfo pass for the module's triple.
500288943Sdim  TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
501288943Sdim
502288943Sdim  // The -disable-simplify-libcalls flag actually disables all builtin optzns.
503239462Sdim  if (DisableSimplifyLibCalls)
504288943Sdim    TLII.disableAllFunctions();
505288943Sdim  PM.add(new TargetLibraryInfoWrapperPass(TLII));
506239462Sdim
507208599Srdivacky  // Add the target data from the target machine, if it exists, or the module.
508296417Sdim  M->setDataLayout(Target->createDataLayout());
509198090Srdivacky
510341825Sdim  // This needs to be done after setting datalayout since it calls verifier
511341825Sdim  // to check debug info whereas verifier relies on correct datalayout.
512341825Sdim  UpgradeDebugInfo(*M);
513341825Sdim
514341825Sdim  // Verify module immediately to catch problems before doInitialization() is
515341825Sdim  // called on any passes.
516341825Sdim  if (!NoVerify && verifyModule(*M, &errs())) {
517341825Sdim    std::string Prefix =
518341825Sdim        (Twine(argv[0]) + Twine(": ") + Twine(InputFilename)).str();
519341825Sdim    WithColor::error(errs(), Prefix) << "input module is broken!\n";
520341825Sdim    return 1;
521341825Sdim  }
522341825Sdim
523288943Sdim  // Override function attributes based on CPUStr, FeaturesStr, and command line
524288943Sdim  // flags.
525288943Sdim  setFunctionAttributes(CPUStr, FeaturesStr, *M);
526288943Sdim
527276479Sdim  if (RelaxAll.getNumOccurrences() > 0 &&
528360784Sdim      FileType != CGFT_ObjectFile)
529341825Sdim    WithColor::warning(errs(), argv[0])
530341825Sdim        << ": warning: ignoring -mc-relax-all because filetype != obj";
531198090Srdivacky
532212793Sdim  {
533288943Sdim    raw_pwrite_stream *OS = &Out->os();
534296417Sdim
535296417Sdim    // Manually do the buffering rather than using buffer_ostream,
536296417Sdim    // so we can memcmp the contents in CompileTwice mode
537296417Sdim    SmallVector<char, 0> Buffer;
538296417Sdim    std::unique_ptr<raw_svector_ostream> BOS;
539360784Sdim    if ((FileType != CGFT_AssemblyFile &&
540296417Sdim         !Out->os().supportsSeeking()) ||
541296417Sdim        CompileTwice) {
542360784Sdim      BOS = std::make_unique<raw_svector_ostream>(Buffer);
543288943Sdim      OS = BOS.get();
544288943Sdim    }
545193323Sed
546321369Sdim    const char *argv0 = argv[0];
547360784Sdim    LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine &>(*Target);
548360784Sdim    MachineModuleInfoWrapperPass *MMIWP =
549360784Sdim        new MachineModuleInfoWrapperPass(&LLVMTM);
550321369Sdim
551327952Sdim    // Construct a custom pass pipeline that starts after instruction
552327952Sdim    // selection.
553327952Sdim    if (!RunPassNames->empty()) {
554327952Sdim      if (!MIR) {
555341825Sdim        WithColor::warning(errs(), argv[0])
556341825Sdim            << "run-pass is for .mir file only.\n";
557327952Sdim        return 1;
558327952Sdim      }
559309124Sdim      TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM);
560327952Sdim      if (TPC.hasLimitedCodeGenPipeline()) {
561341825Sdim        WithColor::warning(errs(), argv[0])
562341825Sdim            << "run-pass cannot be used with "
563341825Sdim            << TPC.getLimitedCodeGenPipelineReason(" and ") << ".\n";
564327952Sdim        return 1;
565327952Sdim      }
566327952Sdim
567321369Sdim      TPC.setDisableVerify(NoVerify);
568309124Sdim      PM.add(&TPC);
569360784Sdim      PM.add(MMIWP);
570309124Sdim      TPC.printAndVerify("");
571327952Sdim      for (const std::string &RunPassName : *RunPassNames) {
572327952Sdim        if (addPass(PM, argv0, RunPassName, TPC))
573309124Sdim          return 1;
574288943Sdim      }
575321369Sdim      TPC.setInitialized();
576327952Sdim      PM.add(createPrintMIRPass(*OS));
577321369Sdim      PM.add(createFreeMachineFunctionPass());
578341825Sdim    } else if (Target->addPassesToEmitFile(PM, *OS,
579341825Sdim                                           DwoOut ? &DwoOut->os() : nullptr,
580360784Sdim                                           FileType, NoVerify, MMIWP)) {
581341825Sdim      WithColor::warning(errs(), argv[0])
582341825Sdim          << "target does not support generation of this"
583341825Sdim          << " file type!\n";
584321369Sdim      return 1;
585212793Sdim    }
586193323Sed
587327952Sdim    if (MIR) {
588360784Sdim      assert(MMIWP && "Forgot to create MMIWP?");
589360784Sdim      if (MIR->parseMachineFunctions(*M, MMIWP->getMMI()))
590327952Sdim        return 1;
591327952Sdim    }
592327952Sdim
593221337Sdim    // Before executing passes, print the final values of the LLVM options.
594221337Sdim    cl::PrintOptionValues();
595221337Sdim
596296417Sdim    // If requested, run the pass manager over the same module again,
597296417Sdim    // to catch any bugs due to persistent state in the passes. Note that
598296417Sdim    // opt has the same functionality, so it may be worth abstracting this out
599296417Sdim    // in the future.
600296417Sdim    SmallVector<char, 0> CompileTwiceBuffer;
601296417Sdim    if (CompileTwice) {
602341825Sdim      std::unique_ptr<Module> M2(llvm::CloneModule(*M));
603296417Sdim      PM.run(*M2);
604296417Sdim      CompileTwiceBuffer = Buffer;
605296417Sdim      Buffer.clear();
606296417Sdim    }
607296417Sdim
608280031Sdim    PM.run(*M);
609296417Sdim
610327952Sdim    auto HasError =
611327952Sdim        ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError;
612327952Sdim    if (*HasError)
613309124Sdim      return 1;
614309124Sdim
615296417Sdim    // Compare the two outputs and make sure they're the same
616296417Sdim    if (CompileTwice) {
617296417Sdim      if (Buffer.size() != CompileTwiceBuffer.size() ||
618296417Sdim          (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
619296417Sdim           0)) {
620296417Sdim        errs()
621296417Sdim            << "Running the pass manager twice changed the output.\n"
622296417Sdim               "Writing the result of the second run to the specified output\n"
623296417Sdim               "To generate the one-run comparison binary, just run without\n"
624296417Sdim               "the compile-twice option\n";
625296417Sdim        Out->os() << Buffer;
626296417Sdim        Out->keep();
627296417Sdim        return 1;
628296417Sdim      }
629296417Sdim    }
630296417Sdim
631296417Sdim    if (BOS) {
632296417Sdim      Out->os() << Buffer;
633296417Sdim    }
634212793Sdim  }
635212793Sdim
636212793Sdim  // Declare success.
637212793Sdim  Out->keep();
638341825Sdim  if (DwoOut)
639341825Sdim    DwoOut->keep();
640212793Sdim
641193323Sed  return 0;
642193323Sed}
643