LLVM  10.0.0svn
LTOCodeGenerator.cpp
Go to the documentation of this file.
1 //===-LTOCodeGenerator.cpp - LLVM Link Time 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 // This file implements the Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Config/config.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/DiagnosticInfo.h"
31 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/PassTimingInfo.h"
36 #include "llvm/IR/RemarkStreamer.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/LTO/LTO.h"
42 #include "llvm/Linker/Linker.h"
43 #include "llvm/MC/MCAsmInfo.h"
44 #include "llvm/MC/MCContext.h"
48 #include "llvm/Support/Host.h"
50 #include "llvm/Support/Signals.h"
57 #include "llvm/Transforms/IPO.h"
62 #include <system_error>
63 using namespace llvm;
64 
66 #ifdef LLVM_VERSION_INFO
67  return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
68 #else
69  return PACKAGE_NAME " version " PACKAGE_VERSION;
70 #endif
71 }
72 
73 namespace llvm {
75  "lto-discard-value-names",
76  cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
77 #ifdef NDEBUG
78  cl::init(true),
79 #else
80  cl::init(false),
81 #endif
82  cl::Hidden);
83 
85  "lto-pass-remarks-with-hotness",
86  cl::desc("With PGO, include profile count in optimization remarks"),
87  cl::Hidden);
88 
90  RemarksFilename("lto-pass-remarks-output",
91  cl::desc("Output filename for pass remarks"),
92  cl::value_desc("filename"));
93 
95  RemarksPasses("lto-pass-remarks-filter",
96  cl::desc("Only record optimization remarks from passes whose "
97  "names match the given regular expression"),
98  cl::value_desc("regex"));
99 
101  "lto-pass-remarks-format",
102  cl::desc("The format used for serializing remarks (default: YAML)"),
103  cl::value_desc("format"), cl::init("yaml"));
104 
106  "lto-stats-file",
107  cl::desc("Save statistics to the specified file"),
108  cl::Hidden);
109 }
110 
112  : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
113  TheLinker(new Linker(*MergedModule)) {
115  Context.enableDebugTypeODRUniquing();
116  initializeLTOPasses();
117 }
118 
120 
121 // Initialize LTO passes. Please keep this function in sync with
122 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
123 // passes are initialized.
124 void LTOCodeGenerator::initializeLTOPasses() {
126 
149 }
150 
152  const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
153  for (int i = 0, e = undefs.size(); i != e; ++i)
154  AsmUndefinedRefs[undefs[i]] = 1;
155 }
156 
158  assert(&Mod->getModule().getContext() == &Context &&
159  "Expected module in same context");
160 
161  bool ret = TheLinker->linkInModule(Mod->takeModule());
162  setAsmUndefinedRefs(Mod);
163 
164  // We've just changed the input, so let's make sure we verify it.
165  HasVerifiedInput = false;
166 
167  return !ret;
168 }
169 
170 void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
171  assert(&Mod->getModule().getContext() == &Context &&
172  "Expected module in same context");
173 
174  AsmUndefinedRefs.clear();
175 
176  MergedModule = Mod->takeModule();
177  TheLinker = std::make_unique<Linker>(*MergedModule);
178  setAsmUndefinedRefs(&*Mod);
179 
180  // We've just changed the input, so let's make sure we verify it.
181  HasVerifiedInput = false;
182 }
183 
185  this->Options = Options;
186 }
187 
189  switch (Debug) {
191  EmitDwarfDebugInfo = false;
192  return;
193 
195  EmitDwarfDebugInfo = true;
196  return;
197  }
198  llvm_unreachable("Unknown debug format!");
199 }
200 
202  OptLevel = Level;
203  switch (OptLevel) {
204  case 0:
205  CGOptLevel = CodeGenOpt::None;
206  return;
207  case 1:
208  CGOptLevel = CodeGenOpt::Less;
209  return;
210  case 2:
211  CGOptLevel = CodeGenOpt::Default;
212  return;
213  case 3:
214  CGOptLevel = CodeGenOpt::Aggressive;
215  return;
216  }
217  llvm_unreachable("Unknown optimization level!");
218 }
219 
221  if (!determineTarget())
222  return false;
223 
224  // We always run the verifier once on the merged module.
225  verifyMergedModuleOnce();
226 
227  // mark which symbols can not be internalized
228  applyScopeRestrictions();
229 
230  // create output file
231  std::error_code EC;
232  ToolOutputFile Out(Path, EC, sys::fs::OF_None);
233  if (EC) {
234  std::string ErrMsg = "could not open bitcode file for writing: ";
235  ErrMsg += Path.str() + ": " + EC.message();
236  emitError(ErrMsg);
237  return false;
238  }
239 
240  // write bitcode to it
241  WriteBitcodeToFile(*MergedModule, Out.os(), ShouldEmbedUselists);
242  Out.os().close();
243 
244  if (Out.os().has_error()) {
245  std::string ErrMsg = "could not write bitcode file: ";
246  ErrMsg += Path.str() + ": " + Out.os().error().message();
247  emitError(ErrMsg);
248  Out.os().clear_error();
249  return false;
250  }
251 
252  Out.keep();
253  return true;
254 }
255 
256 bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
257  // make unique temp output file to put generated code
258  SmallString<128> Filename;
259  int FD;
260 
262  (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
263 
264  std::error_code EC =
265  sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
266  if (EC) {
267  emitError(EC.message());
268  return false;
269  }
270 
271  // generate object file
272  ToolOutputFile objFile(Filename, FD);
273 
274  bool genResult = compileOptimized(&objFile.os());
275  objFile.os().close();
276  if (objFile.os().has_error()) {
277  emitError((Twine("could not write object file: ") + Filename + ": " +
278  objFile.os().error().message())
279  .str());
280  objFile.os().clear_error();
281  sys::fs::remove(Twine(Filename));
282  return false;
283  }
284 
285  objFile.keep();
286  if (!genResult) {
287  sys::fs::remove(Twine(Filename));
288  return false;
289  }
290 
291  NativeObjectPath = Filename.c_str();
292  *Name = NativeObjectPath.c_str();
293  return true;
294 }
295 
296 std::unique_ptr<MemoryBuffer>
298  const char *name;
299  if (!compileOptimizedToFile(&name))
300  return nullptr;
301 
302  // read .o file into memory buffer
304  MemoryBuffer::getFile(name, -1, false);
305  if (std::error_code EC = BufferOrErr.getError()) {
306  emitError(EC.message());
307  sys::fs::remove(NativeObjectPath);
308  return nullptr;
309  }
310 
311  // remove temp files
312  sys::fs::remove(NativeObjectPath);
313 
314  return std::move(*BufferOrErr);
315 }
316 
317 bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
318  bool DisableInline,
319  bool DisableGVNLoadPRE,
320  bool DisableVectorization) {
321  if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
322  DisableVectorization))
323  return false;
324 
325  return compileOptimizedToFile(Name);
326 }
327 
328 std::unique_ptr<MemoryBuffer>
329 LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
330  bool DisableGVNLoadPRE, bool DisableVectorization) {
331  if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
332  DisableVectorization))
333  return nullptr;
334 
335  return compileOptimized();
336 }
337 
338 bool LTOCodeGenerator::determineTarget() {
339  if (TargetMach)
340  return true;
341 
342  TripleStr = MergedModule->getTargetTriple();
343  if (TripleStr.empty()) {
344  TripleStr = sys::getDefaultTargetTriple();
345  MergedModule->setTargetTriple(TripleStr);
346  }
347  llvm::Triple Triple(TripleStr);
348 
349  // create target machine from info for merged modules
350  std::string ErrMsg;
351  MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
352  if (!MArch) {
353  emitError(ErrMsg);
354  return false;
355  }
356 
357  // Construct LTOModule, hand over ownership of module and target. Use MAttr as
358  // the default set of features.
359  SubtargetFeatures Features(MAttr);
360  Features.getDefaultSubtargetFeatures(Triple);
361  FeatureStr = Features.getString();
362  // Set a default CPU for Darwin triples.
363  if (MCpu.empty() && Triple.isOSDarwin()) {
364  if (Triple.getArch() == llvm::Triple::x86_64)
365  MCpu = "core2";
366  else if (Triple.getArch() == llvm::Triple::x86)
367  MCpu = "yonah";
368  else if (Triple.getArch() == llvm::Triple::aarch64 ||
369  Triple.getArch() == llvm::Triple::aarch64_32)
370  MCpu = "cyclone";
371  }
372 
373  TargetMach = createTargetMachine();
374  return true;
375 }
376 
377 std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
378  return std::unique_ptr<TargetMachine>(MArch->createTargetMachine(
379  TripleStr, MCpu, FeatureStr, Options, RelocModel, None, CGOptLevel));
380 }
381 
382 // If a linkonce global is present in the MustPreserveSymbols, we need to make
383 // sure we honor this. To force the compiler to not drop it, we add it to the
384 // "llvm.compiler.used" global.
385 void LTOCodeGenerator::preserveDiscardableGVs(
386  Module &TheModule,
388  std::vector<GlobalValue *> Used;
389  auto mayPreserveGlobal = [&](GlobalValue &GV) {
390  if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
391  !mustPreserveGV(GV))
392  return;
393  if (GV.hasAvailableExternallyLinkage())
394  return emitWarning(
395  (Twine("Linker asked to preserve available_externally global: '") +
396  GV.getName() + "'").str());
397  if (GV.hasInternalLinkage())
398  return emitWarning((Twine("Linker asked to preserve internal global: '") +
399  GV.getName() + "'").str());
400  Used.push_back(&GV);
401  };
402  for (auto &GV : TheModule)
403  mayPreserveGlobal(GV);
404  for (auto &GV : TheModule.globals())
405  mayPreserveGlobal(GV);
406  for (auto &GV : TheModule.aliases())
407  mayPreserveGlobal(GV);
408 
409  if (Used.empty())
410  return;
411 
412  appendToCompilerUsed(TheModule, Used);
413 }
414 
415 void LTOCodeGenerator::applyScopeRestrictions() {
416  if (ScopeRestrictionsDone)
417  return;
418 
419  // Declare a callback for the internalize pass that will ask for every
420  // candidate GlobalValue if it can be internalized or not.
421  Mangler Mang;
422  SmallString<64> MangledName;
423  auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
424  // Unnamed globals can't be mangled, but they can't be preserved either.
425  if (!GV.hasName())
426  return false;
427 
428  // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
429  // with the linker supplied name, which on Darwin includes a leading
430  // underscore.
431  MangledName.clear();
432  MangledName.reserve(GV.getName().size() + 1);
433  Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
434  return MustPreserveSymbols.count(MangledName);
435  };
436 
437  // Preserve linkonce value on linker request
438  preserveDiscardableGVs(*MergedModule, mustPreserveGV);
439 
440  if (!ShouldInternalize)
441  return;
442 
443  if (ShouldRestoreGlobalsLinkage) {
444  // Record the linkage type of non-local symbols so they can be restored
445  // prior
446  // to module splitting.
447  auto RecordLinkage = [&](const GlobalValue &GV) {
448  if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
449  GV.hasName())
450  ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
451  };
452  for (auto &GV : *MergedModule)
453  RecordLinkage(GV);
454  for (auto &GV : MergedModule->globals())
455  RecordLinkage(GV);
456  for (auto &GV : MergedModule->aliases())
457  RecordLinkage(GV);
458  }
459 
460  // Update the llvm.compiler_used globals to force preserving libcalls and
461  // symbols referenced from asm
462  updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
463 
464  internalizeModule(*MergedModule, mustPreserveGV);
465 
466  ScopeRestrictionsDone = true;
467 }
468 
469 /// Restore original linkage for symbols that may have been internalized
470 void LTOCodeGenerator::restoreLinkageForExternals() {
471  if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
472  return;
473 
474  assert(ScopeRestrictionsDone &&
475  "Cannot externalize without internalization!");
476 
477  if (ExternalSymbols.empty())
478  return;
479 
480  auto externalize = [this](GlobalValue &GV) {
481  if (!GV.hasLocalLinkage() || !GV.hasName())
482  return;
483 
484  auto I = ExternalSymbols.find(GV.getName());
485  if (I == ExternalSymbols.end())
486  return;
487 
488  GV.setLinkage(I->second);
489  };
490 
491  llvm::for_each(MergedModule->functions(), externalize);
492  llvm::for_each(MergedModule->globals(), externalize);
493  llvm::for_each(MergedModule->aliases(), externalize);
494 }
495 
496 void LTOCodeGenerator::verifyMergedModuleOnce() {
497  // Only run on the first call.
498  if (HasVerifiedInput)
499  return;
500  HasVerifiedInput = true;
501 
502  bool BrokenDebugInfo = false;
503  if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
504  report_fatal_error("Broken module found, compilation aborted!");
505  if (BrokenDebugInfo) {
506  emitWarning("Invalid debug info found, debug info will be stripped");
507  StripDebugInfo(*MergedModule);
508  }
509 }
510 
511 void LTOCodeGenerator::finishOptimizationRemarks() {
512  if (DiagnosticOutputFile) {
513  DiagnosticOutputFile->keep();
514  // FIXME: LTOCodeGenerator dtor is not invoked on Darwin
515  DiagnosticOutputFile->os().flush();
516  }
517 }
518 
519 /// Optimize merged modules using various IPO passes
520 bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
521  bool DisableGVNLoadPRE,
522  bool DisableVectorization) {
523  if (!this->determineTarget())
524  return false;
525 
526  auto DiagFileOrErr =
529  if (!DiagFileOrErr) {
530  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
531  report_fatal_error("Can't get an output file for the remarks");
532  }
533  DiagnosticOutputFile = std::move(*DiagFileOrErr);
534 
535  // Setup output file to emit statistics.
536  auto StatsFileOrErr = lto::setupStatsFile(LTOStatsFile);
537  if (!StatsFileOrErr) {
538  errs() << "Error: " << toString(StatsFileOrErr.takeError()) << "\n";
539  report_fatal_error("Can't get an output file for the statistics");
540  }
541  StatsFile = std::move(StatsFileOrErr.get());
542 
543  // We always run the verifier once on the merged module, the `DisableVerify`
544  // parameter only applies to subsequent verify.
545  verifyMergedModuleOnce();
546 
547  // Mark which symbols can not be internalized
548  this->applyScopeRestrictions();
549 
550  // Instantiate the pass manager to organize the passes.
551  legacy::PassManager passes;
552 
553  // Add an appropriate DataLayout instance for this module...
554  MergedModule->setDataLayout(TargetMach->createDataLayout());
555 
556  passes.add(
557  createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
558 
559  Triple TargetTriple(TargetMach->getTargetTriple());
560  PassManagerBuilder PMB;
561  PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
562  PMB.LoopVectorize = !DisableVectorization;
563  PMB.SLPVectorize = !DisableVectorization;
564  if (!DisableInline)
565  PMB.Inliner = createFunctionInliningPass();
566  PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
567  if (Freestanding)
568  PMB.LibraryInfo->disableAllFunctions();
569  PMB.OptLevel = OptLevel;
570  PMB.VerifyInput = !DisableVerify;
571  PMB.VerifyOutput = !DisableVerify;
572 
573  PMB.populateLTOPassManager(passes);
574 
575  // Run our queue of passes all at once now, efficiently.
576  passes.run(*MergedModule);
577 
578  return true;
579 }
580 
582  if (!this->determineTarget())
583  return false;
584 
585  // We always run the verifier once on the merged module. If it has already
586  // been called in optimize(), this call will return early.
587  verifyMergedModuleOnce();
588 
589  legacy::PassManager preCodeGenPasses;
590 
591  // If the bitcode files contain ARC code and were compiled with optimization,
592  // the ObjCARCContractPass must be run, so do it unconditionally here.
593  preCodeGenPasses.add(createObjCARCContractPass());
594  preCodeGenPasses.run(*MergedModule);
595 
596  // Re-externalize globals that may have been internalized to increase scope
597  // for splitting
598  restoreLinkageForExternals();
599 
600  // Do code generation. We need to preserve the module in case the client calls
601  // writeMergedModules() after compilation, but we only need to allow this at
602  // parallelism level 1. This is achieved by having splitCodeGen return the
603  // original module at parallelism level 1 which we then assign back to
604  // MergedModule.
605  MergedModule = splitCodeGen(std::move(MergedModule), Out, {},
606  [&]() { return createTargetMachine(); }, FileType,
607  ShouldRestoreGlobalsLinkage);
608 
609  // If statistics were requested, save them to the specified file or
610  // print them out after codegen.
611  if (StatsFile)
612  PrintStatisticsJSON(StatsFile->os());
613  else if (AreStatisticsEnabled())
614  PrintStatistics();
615 
617 
618  finishOptimizationRemarks();
619 
620  return true;
621 }
622 
623 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
624 /// LTO problems.
626  for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
627  o = getToken(o.second))
628  CodegenOptions.push_back(o.first);
629 }
630 
632  // if options were requested, set them
633  if (!CodegenOptions.empty()) {
634  // ParseCommandLineOptions() expects argv[0] to be program name.
635  std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
636  for (std::string &Arg : CodegenOptions)
637  CodegenArgv.push_back(Arg.c_str());
638  cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
639  }
640 }
641 
642 
644  // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
646  switch (DI.getSeverity()) {
647  case DS_Error:
648  Severity = LTO_DS_ERROR;
649  break;
650  case DS_Warning:
651  Severity = LTO_DS_WARNING;
652  break;
653  case DS_Remark:
654  Severity = LTO_DS_REMARK;
655  break;
656  case DS_Note:
657  Severity = LTO_DS_NOTE;
658  break;
659  }
660  // Create the string that will be reported to the external diagnostic handler.
661  std::string MsgStorage;
662  raw_string_ostream Stream(MsgStorage);
663  DiagnosticPrinterRawOStream DP(Stream);
664  DI.print(DP);
665  Stream.flush();
666 
667  // If this method has been called it means someone has set up an external
668  // diagnostic handler. Assert on that.
669  assert(DiagHandler && "Invalid diagnostic handler");
670  (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
671 }
672 
673 namespace {
674 struct LTODiagnosticHandler : public DiagnosticHandler {
675  LTOCodeGenerator *CodeGenerator;
676  LTODiagnosticHandler(LTOCodeGenerator *CodeGenPtr)
677  : CodeGenerator(CodeGenPtr) {}
678  bool handleDiagnostics(const DiagnosticInfo &DI) override {
679  CodeGenerator->DiagnosticHandler(DI);
680  return true;
681  }
682 };
683 }
684 
685 void
687  void *Ctxt) {
688  this->DiagHandler = DiagHandler;
689  this->DiagContext = Ctxt;
690  if (!DiagHandler)
691  return Context.setDiagnosticHandler(nullptr);
692  // Register the LTOCodeGenerator stub in the LLVMContext to forward the
693  // diagnostic to the external DiagHandler.
694  Context.setDiagnosticHandler(std::make_unique<LTODiagnosticHandler>(this),
695  true);
696 }
697 
698 namespace {
699 class LTODiagnosticInfo : public DiagnosticInfo {
700  const Twine &Msg;
701 public:
702  LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
703  : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
704  void print(DiagnosticPrinter &DP) const override { DP << Msg; }
705 };
706 }
707 
708 void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
709  if (DiagHandler)
710  (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
711  else
712  Context.diagnose(LTODiagnosticInfo(ErrMsg));
713 }
714 
715 void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
716  if (DiagHandler)
717  (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
718  else
719  Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
720 }
void setDiagnosticHandler(std::unique_ptr< DiagnosticHandler > &&DH, bool RespectFilters=false)
setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler to provide custom d...
void initializeDCELegacyPassPass(PassRegistry &)
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:481
This is the base class for diagnostic handling in LLVM.
void initializeGlobalOptLegacyPassPass(PassRegistry &)
Represents either an error or a value T.
Definition: ErrorOr.h:56
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:232
LLVMContext & Context
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
void initializeInternalizeLegacyPassPass(PassRegistry &)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
std::unique_ptr< MemoryBuffer > compileOptimized()
Compiles the merged optimized module into a single output file.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void initializeSimpleInlinerPass(PassRegistry &)
void initializeJumpThreadingPass(PassRegistry &)
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
lto_codegen_diagnostic_severity_t
Diagnostic severity.
Definition: lto.h:305
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
void enableDebugTypeODRUniquing()
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
std::string getString() const
Returns features as a string.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:104
void initializeGlobalDCELegacyPassPass(PassRegistry &)
iterator find(StringRef Key)
Definition: StringMap.h:332
bool addModule(struct LTOModule *)
Merge given module.
Implementation of the target library information.
bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
Optimizes the merged module.
std::unique_ptr< Module > splitCodeGen(std::unique_ptr< Module > M, ArrayRef< raw_pwrite_stream *> OSs, ArrayRef< llvm::raw_pwrite_stream *> BCOSs, const std::function< std::unique_ptr< TargetMachine >()> &TMFactory, TargetMachine::CodeGenFileType FileType=TargetMachine::CGFT_ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
Definition: ParallelCG.cpp:37
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
void reserve(size_type N)
Definition: SmallVector.h:369
std::unique_ptr< Module > takeModule()
Definition: LTOModule.h:120
std::error_code error() const
Definition: raw_ostream.h:466
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
void initializeReversePostOrderFunctionAttrsLegacyPassPass(PassRegistry &)
static void externalize(GlobalValue *GV)
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
void initializePruneEHPass(PassRegistry &)
bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", raw_ostream *Errs=nullptr, const char *EnvVar=nullptr, bool LongOptionsUseDoubleDash=false)
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
void add(Pass *P) override
Add a pass to the queue of passes to run.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
void initializeArgPromotionPass(PassRegistry &)
std::unique_ptr< MemoryBuffer > compile(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
As with compile_to_file(), this function compiles the merged module into single output file...
Interface for custom diagnostic printing.
This header defines classes/functions to handle pass execution timing information with interfaces for...
bool writeMergedModules(StringRef Path)
Write the merged module to the file specified by the given path.
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:296
Pass * createObjCARCContractPass()
static const char * getVersionString()
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:350
void initializeAttributorLegacyPassPass(PassRegistry &)
This class provides the core functionality of linking in LLVM.
Definition: Linker.h:24
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
void reportAndResetTimings(raw_ostream *OutStream=nullptr)
If -time-passes has been specified, report the timings immediately and then reset the timers to zero...
Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition: LTO.cpp:1394
lto_debug_model
Definition: lto.h:77
void initializeGVNLegacyPassPass(PassRegistry &)
PassManager manages ModulePassManagers.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
This is the base abstract class for diagnostic reporting in the backend.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:358
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::error_code getError() const
Definition: ErrorOr.h:159
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
void initializeSROALegacyPassPass(PassRegistry &)
void setOptLevel(unsigned OptLevel)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
void setTargetOptions(const TargetOptions &Options)
const Module & getModule() const
Definition: LTOModule.h:117
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:38
void setCodeGenDebugOptions(StringRef Opts)
Pass options to the driver and optimization passes.
void initializeMemCpyOptLegacyPassPass(PassRegistry &)
void initializeConstantMergeLegacyPassPass(PassRegistry &)
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:472
C++ class which implements the opaque lto_code_gen_t type.
void initializeGlobalsAAWrapperPassPass(PassRegistry &)
void(* lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt)
Diagnostic handler type.
Definition: lto.h:321
const std::vector< StringRef > & getAsmUndefinedRefs()
Definition: LTOModule.h:159
void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:201
cl::opt< std::string > LTOStatsFile("lto-stats-file", cl::desc("Save statistics to the specified file"), cl::Hidden)
Module.h This file contains the declarations for the Module class.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:370
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
The access may modify the value stored in memory.
Manages the enabling and disabling of subtarget specific features.
void initializeDAHPass(PassRegistry &)
#define NDEBUG
Definition: regutils.h:48
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:5063
Basic diagnostic printer that uses an underlying raw_ostream.
void DiagnosticHandler(const DiagnosticInfo &DI)
cl::opt< bool > LTODiscardValueNames("lto-discard-value-names", cl::desc("Strip names from Value during LTO (other than GlobalValue)."), cl::init(false), cl::Hidden)
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
Definition: Path.cpp:812
void initializePostOrderFunctionAttrsLegacyPassPass(PassRegistry &)
void initializeIPSCCPLegacyPassPass(PassRegistry &)
const char * c_str()
Definition: SmallString.h:269
#define I(x, y, z)
Definition: MD5.cpp:58
bool compile_to_file(const char **Name, bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
Compile the merged module into a single output file; the path to output file is returned to the calle...
DiagnosticSeverity getSeverity() const
static bool mustPreserveGV(const GlobalValue &GV)
Predicate for Internalize pass.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void initializeInstructionCombiningPassPass(PassRegistry &)
void keep()
Indicate that the tool&#39;s job wrt this output file has been successful and the file should not be dele...
void close()
Manually flush the stream and close the file.
void updateCompilerUsed(Module &TheModule, const TargetMachine &TM, const StringSet<> &AsmUndefinedRefs)
Find all globals in TheModule that are referenced in AsmUndefinedRefs, as well as the user-supplied f...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
void setAsmUndefinedRefs(struct LTOModule *)
void initializeCFGSimplifyPassPass(PassRegistry &)
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:227
bool empty() const
Definition: StringMap.h:110
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Expected< std::unique_ptr< ToolOutputFile > > setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1375
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
Pass * createFunctionInliningPass()
createFunctionInliningPass - Return a new pass object that uses a heuristic to inline direct function...
static const char * name
LTOCodeGenerator(LLVMContext &Context)
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
static cl::opt< bool, true > Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag))
void initializeLegacyLICMPassPass(PassRegistry &)
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
This pass exposes codegen information to IR-level passes.
void parseCodeGenDebugOptions()
Parse the options set in setCodeGenDebugOptions.
UnaryPredicate for_each(R &&Range, UnaryPredicate P)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1158
void setModule(std::unique_ptr< LTOModule > M)
Set the destination module.
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry &)
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV, CallGraph *CG=nullptr)
Helper function to internalize functions and variables in a Module.
Definition: Internalize.h:70
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:133
iterator end()
Definition: StringMap.h:317
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
void setDiagnosticHandler(lto_diagnostic_handler_t, void *)
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:483
void setDebugInfo(lto_debug_model)