LLVM  14.0.0git
LTO.cpp
Go to the documentation of this file.
1 //===-LTO.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 functions and classes used to support LTO.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/LTO/LTO.h"
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/CodeGen/Analysis.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/AutoUpgrade.h"
28 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/LTO/LTOBackend.h"
35 #include "llvm/Linker/IRMover.h"
36 #include "llvm/MC/TargetRegistry.h"
39 #include "llvm/Support/Error.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/SHA1.h"
45 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Support/Threading.h"
49 #include "llvm/Support/VCSRevision.h"
53 #include "llvm/Transforms/IPO.h"
58 
59 #include <set>
60 
61 using namespace llvm;
62 using namespace lto;
63 using namespace object;
64 
65 #define DEBUG_TYPE "lto"
66 
67 static cl::opt<bool>
68  DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
69  cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
70 
71 /// Enable global value internalization in LTO.
73  "enable-lto-internalization", cl::init(true), cl::Hidden,
74  cl::desc("Enable global value internalization in LTO"));
75 
76 // Computes a unique hash for the Module considering the current list of
77 // export/import and other global analysis results.
78 // The hash is produced in \p Key.
80  SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
81  StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
82  const FunctionImporter::ExportSetTy &ExportList,
83  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
84  const GVSummaryMapTy &DefinedGlobals,
85  const std::set<GlobalValue::GUID> &CfiFunctionDefs,
86  const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
87  // Compute the unique hash for this entry.
88  // This is based on the current compiler version, the module itself, the
89  // export list, the hash for every single module in the import list, the
90  // list of ResolvedODR for the module, and the list of preserved symbols.
91  SHA1 Hasher;
92 
93  // Start with the compiler revision
94  Hasher.update(LLVM_VERSION_STRING);
95 #ifdef LLVM_REVISION
96  Hasher.update(LLVM_REVISION);
97 #endif
98 
99  // Include the parts of the LTO configuration that affect code generation.
100  auto AddString = [&](StringRef Str) {
101  Hasher.update(Str);
102  Hasher.update(ArrayRef<uint8_t>{0});
103  };
104  auto AddUnsigned = [&](unsigned I) {
105  uint8_t Data[4];
107  Hasher.update(ArrayRef<uint8_t>{Data, 4});
108  };
109  auto AddUint64 = [&](uint64_t I) {
110  uint8_t Data[8];
112  Hasher.update(ArrayRef<uint8_t>{Data, 8});
113  };
114  AddString(Conf.CPU);
115  // FIXME: Hash more of Options. For now all clients initialize Options from
116  // command-line flags (which is unsupported in production), but may set
117  // RelaxELFRelocations. The clang driver can also pass FunctionSections,
118  // DataSections and DebuggerTuning via command line flags.
119  AddUnsigned(Conf.Options.RelaxELFRelocations);
120  AddUnsigned(Conf.Options.FunctionSections);
121  AddUnsigned(Conf.Options.DataSections);
122  AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
123  for (auto &A : Conf.MAttrs)
124  AddString(A);
125  if (Conf.RelocModel)
126  AddUnsigned(*Conf.RelocModel);
127  else
128  AddUnsigned(-1);
129  if (Conf.CodeModel)
130  AddUnsigned(*Conf.CodeModel);
131  else
132  AddUnsigned(-1);
133  AddUnsigned(Conf.CGOptLevel);
134  AddUnsigned(Conf.CGFileType);
135  AddUnsigned(Conf.OptLevel);
136  AddUnsigned(Conf.UseNewPM);
137  AddUnsigned(Conf.Freestanding);
138  AddString(Conf.OptPipeline);
139  AddString(Conf.AAPipeline);
140  AddString(Conf.OverrideTriple);
141  AddString(Conf.DefaultTriple);
142  AddString(Conf.DwoDir);
143 
144  // Include the hash for the current module
145  auto ModHash = Index.getModuleHash(ModuleID);
146  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
147 
148  std::vector<uint64_t> ExportsGUID;
149  ExportsGUID.reserve(ExportList.size());
150  for (const auto &VI : ExportList) {
151  auto GUID = VI.getGUID();
152  ExportsGUID.push_back(GUID);
153  }
154 
155  // Sort the export list elements GUIDs.
156  llvm::sort(ExportsGUID);
157  for (uint64_t GUID : ExportsGUID) {
158  // The export list can impact the internalization, be conservative here
159  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
160  }
161 
162  // Include the hash for every module we import functions from. The set of
163  // imported symbols for each module may affect code generation and is
164  // sensitive to link order, so include that as well.
165  using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
166  std::vector<ImportMapIteratorTy> ImportModulesVector;
167  ImportModulesVector.reserve(ImportList.size());
168 
169  for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
170  ++It) {
171  ImportModulesVector.push_back(It);
172  }
173  llvm::sort(ImportModulesVector,
174  [](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
175  -> bool { return Lhs->getKey() < Rhs->getKey(); });
176  for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
177  auto ModHash = Index.getModuleHash(EntryIt->first());
178  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
179 
180  AddUint64(EntryIt->second.size());
181  for (auto &Fn : EntryIt->second)
182  AddUint64(Fn);
183  }
184 
185  // Include the hash for the resolved ODR.
186  for (auto &Entry : ResolvedODR) {
187  Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
188  sizeof(GlobalValue::GUID)));
189  Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
190  sizeof(GlobalValue::LinkageTypes)));
191  }
192 
193  // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
194  // defined in this module.
195  std::set<GlobalValue::GUID> UsedCfiDefs;
196  std::set<GlobalValue::GUID> UsedCfiDecls;
197 
198  // Typeids used in this module.
199  std::set<GlobalValue::GUID> UsedTypeIds;
200 
201  auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
202  if (CfiFunctionDefs.count(ValueGUID))
203  UsedCfiDefs.insert(ValueGUID);
204  if (CfiFunctionDecls.count(ValueGUID))
205  UsedCfiDecls.insert(ValueGUID);
206  };
207 
208  auto AddUsedThings = [&](GlobalValueSummary *GS) {
209  if (!GS) return;
210  AddUnsigned(GS->getVisibility());
211  AddUnsigned(GS->isLive());
212  AddUnsigned(GS->canAutoHide());
213  for (const ValueInfo &VI : GS->refs()) {
214  AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
215  AddUsedCfiGlobal(VI.getGUID());
216  }
217  if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
218  AddUnsigned(GVS->maybeReadOnly());
219  AddUnsigned(GVS->maybeWriteOnly());
220  }
221  if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
222  for (auto &TT : FS->type_tests())
223  UsedTypeIds.insert(TT);
224  for (auto &TT : FS->type_test_assume_vcalls())
225  UsedTypeIds.insert(TT.GUID);
226  for (auto &TT : FS->type_checked_load_vcalls())
227  UsedTypeIds.insert(TT.GUID);
228  for (auto &TT : FS->type_test_assume_const_vcalls())
229  UsedTypeIds.insert(TT.VFunc.GUID);
230  for (auto &TT : FS->type_checked_load_const_vcalls())
231  UsedTypeIds.insert(TT.VFunc.GUID);
232  for (auto &ET : FS->calls()) {
233  AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
234  AddUsedCfiGlobal(ET.first.getGUID());
235  }
236  }
237  };
238 
239  // Include the hash for the linkage type to reflect internalization and weak
240  // resolution, and collect any used type identifier resolutions.
241  for (auto &GS : DefinedGlobals) {
242  GlobalValue::LinkageTypes Linkage = GS.second->linkage();
243  Hasher.update(
244  ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
245  AddUsedCfiGlobal(GS.first);
246  AddUsedThings(GS.second);
247  }
248 
249  // Imported functions may introduce new uses of type identifier resolutions,
250  // so we need to collect their used resolutions as well.
251  for (auto &ImpM : ImportList)
252  for (auto &ImpF : ImpM.second) {
253  GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
254  AddUsedThings(S);
255  // If this is an alias, we also care about any types/etc. that the aliasee
256  // may reference.
257  if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
258  AddUsedThings(AS->getBaseObject());
259  }
260 
261  auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
262  AddString(TId);
263 
264  AddUnsigned(S.TTRes.TheKind);
265  AddUnsigned(S.TTRes.SizeM1BitWidth);
266 
267  AddUint64(S.TTRes.AlignLog2);
268  AddUint64(S.TTRes.SizeM1);
269  AddUint64(S.TTRes.BitMask);
270  AddUint64(S.TTRes.InlineBits);
271 
272  AddUint64(S.WPDRes.size());
273  for (auto &WPD : S.WPDRes) {
274  AddUnsigned(WPD.first);
275  AddUnsigned(WPD.second.TheKind);
276  AddString(WPD.second.SingleImplName);
277 
278  AddUint64(WPD.second.ResByArg.size());
279  for (auto &ByArg : WPD.second.ResByArg) {
280  AddUint64(ByArg.first.size());
281  for (uint64_t Arg : ByArg.first)
282  AddUint64(Arg);
283  AddUnsigned(ByArg.second.TheKind);
284  AddUint64(ByArg.second.Info);
285  AddUnsigned(ByArg.second.Byte);
286  AddUnsigned(ByArg.second.Bit);
287  }
288  }
289  };
290 
291  // Include the hash for all type identifiers used by this module.
292  for (GlobalValue::GUID TId : UsedTypeIds) {
293  auto TidIter = Index.typeIds().equal_range(TId);
294  for (auto It = TidIter.first; It != TidIter.second; ++It)
295  AddTypeIdSummary(It->second.first, It->second.second);
296  }
297 
298  AddUnsigned(UsedCfiDefs.size());
299  for (auto &V : UsedCfiDefs)
300  AddUint64(V);
301 
302  AddUnsigned(UsedCfiDecls.size());
303  for (auto &V : UsedCfiDecls)
304  AddUint64(V);
305 
306  if (!Conf.SampleProfile.empty()) {
307  auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
308  if (FileOrErr) {
309  Hasher.update(FileOrErr.get()->getBuffer());
310 
311  if (!Conf.ProfileRemapping.empty()) {
312  FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
313  if (FileOrErr)
314  Hasher.update(FileOrErr.get()->getBuffer());
315  }
316  }
317  }
318 
319  Key = toHex(Hasher.result());
320 }
321 
323  const Config &C, ValueInfo VI,
324  DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
326  isPrevailing,
328  recordNewLinkage,
329  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
330  GlobalValue::VisibilityTypes Visibility =
331  C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
333  for (auto &S : VI.getSummaryList()) {
334  GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
335  // Ignore local and appending linkage values since the linker
336  // doesn't resolve them.
337  if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
339  continue;
340  // We need to emit only one of these. The prevailing module will keep it,
341  // but turned into a weak, while the others will drop it when possible.
342  // This is both a compile-time optimization and a correctness
343  // transformation. This is necessary for correctness when we have exported
344  // a reference - we need to convert the linkonce to weak to
345  // ensure a copy is kept to satisfy the exported reference.
346  // FIXME: We may want to split the compile time and correctness
347  // aspects into separate routines.
348  if (isPrevailing(VI.getGUID(), S.get())) {
349  if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
350  S->setLinkage(GlobalValue::getWeakLinkage(
351  GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
352  // The kept copy is eligible for auto-hiding (hidden visibility) if all
353  // copies were (i.e. they were all linkonce_odr global unnamed addr).
354  // If any copy is not (e.g. it was originally weak_odr), then the symbol
355  // must remain externally available (e.g. a weak_odr from an explicitly
356  // instantiated template). Additionally, if it is in the
357  // GUIDPreservedSymbols set, that means that it is visibile outside
358  // the summary (e.g. in a native object or a bitcode file without
359  // summary), and in that case we cannot hide it as it isn't possible to
360  // check all copies.
361  S->setCanAutoHide(VI.canAutoHide() &&
362  !GUIDPreservedSymbols.count(VI.getGUID()));
363  }
364  if (C.VisibilityScheme == Config::FromPrevailing)
365  Visibility = S->getVisibility();
366  }
367  // Alias and aliasee can't be turned into available_externally.
368  else if (!isa<AliasSummary>(S.get()) &&
369  !GlobalInvolvedWithAlias.count(S.get()))
371 
372  // For ELF, set visibility to the computed visibility from summaries. We
373  // don't track visibility from declarations so this may be more relaxed than
374  // the most constraining one.
375  if (C.VisibilityScheme == Config::ELF)
376  S->setVisibility(Visibility);
377 
378  if (S->linkage() != OriginalLinkage)
379  recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
380  }
381 
382  if (C.VisibilityScheme == Config::FromPrevailing) {
383  for (auto &S : VI.getSummaryList()) {
384  GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
385  if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
387  continue;
388  S->setVisibility(Visibility);
389  }
390  }
391 }
392 
393 /// Resolve linkage for prevailing symbols in the \p Index.
394 //
395 // We'd like to drop these functions if they are no longer referenced in the
396 // current module. However there is a chance that another module is still
397 // referencing them because of the import. We make sure we always emit at least
398 // one copy.
400  const Config &C, ModuleSummaryIndex &Index,
402  isPrevailing,
404  recordNewLinkage,
405  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
406  // We won't optimize the globals that are referenced by an alias for now
407  // Ideally we should turn the alias into a global and duplicate the definition
408  // when needed.
409  DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
410  for (auto &I : Index)
411  for (auto &S : I.second.SummaryList)
412  if (auto AS = dyn_cast<AliasSummary>(S.get()))
413  GlobalInvolvedWithAlias.insert(&AS->getAliasee());
414 
415  for (auto &I : Index)
416  thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
417  GlobalInvolvedWithAlias, isPrevailing,
418  recordNewLinkage, GUIDPreservedSymbols);
419 }
420 
422  if (auto *VarSummary = dyn_cast<GlobalVarSummary>(GVS->getBaseObject()))
423  return !VarSummary->maybeReadOnly() && !VarSummary->maybeWriteOnly() &&
424  (VarSummary->linkage() == GlobalValue::WeakODRLinkage ||
425  VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage);
426  return false;
427 }
428 
430  ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
432  isPrevailing) {
433  for (auto &S : VI.getSummaryList()) {
434  if (isExported(S->modulePath(), VI)) {
435  if (GlobalValue::isLocalLinkage(S->linkage()))
436  S->setLinkage(GlobalValue::ExternalLinkage);
437  } else if (EnableLTOInternalization &&
438  // Ignore local and appending linkage values since the linker
439  // doesn't resolve them.
440  !GlobalValue::isLocalLinkage(S->linkage()) &&
441  (!GlobalValue::isInterposableLinkage(S->linkage()) ||
442  isPrevailing(VI.getGUID(), S.get())) &&
443  S->linkage() != GlobalValue::AppendingLinkage &&
444  // We can't internalize available_externally globals because this
445  // can break function pointer equality.
447  // Functions and read-only variables with linkonce_odr and
448  // weak_odr linkage can be internalized. We can't internalize
449  // linkonce_odr and weak_odr variables which are both modified
450  // and read somewhere in the program because reads and writes
451  // will become inconsistent.
452  !isWeakObjectWithRWAccess(S.get()))
453  S->setLinkage(GlobalValue::InternalLinkage);
454  }
455 }
456 
457 // Update the linkages in the given \p Index to mark exported values
458 // as external and non-exported values as internal.
461  function_ref<bool(StringRef, ValueInfo)> isExported,
463  isPrevailing) {
464  for (auto &I : Index)
465  thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
466  isPrevailing);
467 }
468 
469 // Requires a destructor for std::vector<InputModule>.
470 InputFile::~InputFile() = default;
471 
473  std::unique_ptr<InputFile> File(new InputFile);
474 
476  if (!FOrErr)
477  return FOrErr.takeError();
478 
479  File->TargetTriple = FOrErr->TheReader.getTargetTriple();
480  File->SourceFileName = FOrErr->TheReader.getSourceFileName();
481  File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
482  File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
483  File->ComdatTable = FOrErr->TheReader.getComdatTable();
484 
485  for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
486  size_t Begin = File->Symbols.size();
487  for (const irsymtab::Reader::SymbolRef &Sym :
488  FOrErr->TheReader.module_symbols(I))
489  // Skip symbols that are irrelevant to LTO. Note that this condition needs
490  // to match the one in Skip() in LTO::addRegularLTO().
491  if (Sym.isGlobal() && !Sym.isFormatSpecific())
492  File->Symbols.push_back(Sym);
493  File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
494  }
495 
496  File->Mods = FOrErr->Mods;
497  File->Strtab = std::move(FOrErr->Strtab);
498  return std::move(File);
499 }
500 
502  return Mods[0].getModuleIdentifier();
503 }
504 
506  assert(Mods.size() == 1 && "Expect only one bitcode module");
507  return Mods[0];
508 }
509 
510 LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
511  const Config &Conf)
512  : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
513  Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
514  Mover(std::make_unique<IRMover>(*CombinedModule)) {}
515 
516 LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
517  : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
518  if (!Backend)
519  this->Backend =
521 }
522 
524  unsigned ParallelCodeGenParallelismLevel)
525  : Conf(std::move(Conf)),
526  RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
527  ThinLTO(std::move(Backend)) {}
528 
529 // Requires a destructor for MapVector<BitcodeModule>.
530 LTO::~LTO() = default;
531 
532 // Add the symbols in the given module to the GlobalResolutions map, and resolve
533 // their partitions.
534 void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
536  unsigned Partition, bool InSummary) {
537  auto *ResI = Res.begin();
538  auto *ResE = Res.end();
539  (void)ResE;
540  const Triple TT(RegularLTO.CombinedModule->getTargetTriple());
541  for (const InputFile::Symbol &Sym : Syms) {
542  assert(ResI != ResE);
543  SymbolResolution Res = *ResI++;
544 
545  StringRef Name = Sym.getName();
546  // Strip the __imp_ prefix from COFF dllimport symbols (similar to the
547  // way they are handled by lld), otherwise we can end up with two
548  // global resolutions (one with and one for a copy of the symbol without).
549  if (TT.isOSBinFormatCOFF() && Name.startswith("__imp_"))
550  Name = Name.substr(strlen("__imp_"));
551  auto &GlobalRes = GlobalResolutions[Name];
552  GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
553  if (Res.Prevailing) {
554  assert(!GlobalRes.Prevailing &&
555  "Multiple prevailing defs are not allowed");
556  GlobalRes.Prevailing = true;
557  GlobalRes.IRName = std::string(Sym.getIRName());
558  } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
559  // Sometimes it can be two copies of symbol in a module and prevailing
560  // symbol can have no IR name. That might happen if symbol is defined in
561  // module level inline asm block. In case we have multiple modules with
562  // the same symbol we want to use IR name of the prevailing symbol.
563  // Otherwise, if we haven't seen a prevailing symbol, set the name so that
564  // we can later use it to check if there is any prevailing copy in IR.
565  GlobalRes.IRName = std::string(Sym.getIRName());
566  }
567 
568  // Set the partition to external if we know it is re-defined by the linker
569  // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
570  // regular object, is referenced from llvm.compiler.used/llvm.used, or was
571  // already recorded as being referenced from a different partition.
572  if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
573  (GlobalRes.Partition != GlobalResolution::Unknown &&
574  GlobalRes.Partition != Partition)) {
575  GlobalRes.Partition = GlobalResolution::External;
576  } else
577  // First recorded reference, save the current partition.
578  GlobalRes.Partition = Partition;
579 
580  // Flag as visible outside of summary if visible from a regular object or
581  // from a module that does not have a summary.
582  GlobalRes.VisibleOutsideSummary |=
583  (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
584 
585  GlobalRes.ExportDynamic |= Res.ExportDynamic;
586  }
587 }
588 
591  StringRef Path = Input->getName();
592  OS << Path << '\n';
593  auto ResI = Res.begin();
594  for (const InputFile::Symbol &Sym : Input->symbols()) {
595  assert(ResI != Res.end());
596  SymbolResolution Res = *ResI++;
597 
598  OS << "-r=" << Path << ',' << Sym.getName() << ',';
599  if (Res.Prevailing)
600  OS << 'p';
602  OS << 'l';
603  if (Res.VisibleToRegularObj)
604  OS << 'x';
605  if (Res.LinkerRedefined)
606  OS << 'r';
607  OS << '\n';
608  }
609  OS.flush();
610  assert(ResI == Res.end());
611 }
612 
613 Error LTO::add(std::unique_ptr<InputFile> Input,
615  assert(!CalledGetMaxTasks);
616 
617  if (Conf.ResolutionFile)
618  writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
619 
620  if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
621  RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
622  if (Triple(Input->getTargetTriple()).isOSBinFormatELF())
624  }
625 
626  const SymbolResolution *ResI = Res.begin();
627  for (unsigned I = 0; I != Input->Mods.size(); ++I)
628  if (Error Err = addModule(*Input, I, ResI, Res.end()))
629  return Err;
630 
631  assert(ResI == Res.end());
632  return Error::success();
633 }
634 
635 Error LTO::addModule(InputFile &Input, unsigned ModI,
636  const SymbolResolution *&ResI,
637  const SymbolResolution *ResE) {
638  Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
639  if (!LTOInfo)
640  return LTOInfo.takeError();
641 
642  if (EnableSplitLTOUnit.hasValue()) {
643  // If only some modules were split, flag this in the index so that
644  // we can skip or error on optimizations that need consistently split
645  // modules (whole program devirt and lower type tests).
646  if (EnableSplitLTOUnit.getValue() != LTOInfo->EnableSplitLTOUnit)
647  ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
648  } else
649  EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
650 
651  BitcodeModule BM = Input.Mods[ModI];
652  auto ModSyms = Input.module_symbols(ModI);
653  addModuleToGlobalRes(ModSyms, {ResI, ResE},
654  LTOInfo->IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
655  LTOInfo->HasSummary);
656 
657  if (LTOInfo->IsThinLTO)
658  return addThinLTO(BM, ModSyms, ResI, ResE);
659 
660  RegularLTO.EmptyCombinedModule = false;
662  addRegularLTO(BM, ModSyms, ResI, ResE);
663  if (!ModOrErr)
664  return ModOrErr.takeError();
665 
666  if (!LTOInfo->HasSummary)
667  return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
668 
669  // Regular LTO module summaries are added to a dummy module that represents
670  // the combined regular LTO module.
671  if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, "", -1ull))
672  return Err;
673  RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
674  return Error::success();
675 }
676 
677 // Checks whether the given global value is in a non-prevailing comdat
678 // (comdat containing values the linker indicated were not prevailing,
679 // which we then dropped to available_externally), and if so, removes
680 // it from the comdat. This is called for all global values to ensure the
681 // comdat is empty rather than leaving an incomplete comdat. It is needed for
682 // regular LTO modules, in case we are in a mixed-LTO mode (both regular
683 // and thin LTO modules) compilation. Since the regular LTO module will be
684 // linked first in the final native link, we want to make sure the linker
685 // doesn't select any of these incomplete comdats that would be left
686 // in the regular LTO module without this cleanup.
687 static void
689  std::set<const Comdat *> &NonPrevailingComdats) {
690  Comdat *C = GV.getComdat();
691  if (!C)
692  return;
693 
694  if (!NonPrevailingComdats.count(C))
695  return;
696 
697  // Additionally need to drop externally visible global values from the comdat
698  // to available_externally, so that there aren't multiply defined linker
699  // errors.
700  if (!GV.hasLocalLinkage())
702 
703  if (auto GO = dyn_cast<GlobalObject>(&GV))
704  GO->setComdat(nullptr);
705 }
706 
707 // Add a regular LTO object to the link.
708 // The resulting module needs to be linked into the combined LTO module with
709 // linkRegularLTO.
711 LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
712  const SymbolResolution *&ResI,
713  const SymbolResolution *ResE) {
714  RegularLTOState::AddedModule Mod;
716  BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
717  /*IsImporting*/ false);
718  if (!MOrErr)
719  return MOrErr.takeError();
720  Module &M = **MOrErr;
721  Mod.M = std::move(*MOrErr);
722 
723  if (Error Err = M.materializeMetadata())
724  return std::move(Err);
725  UpgradeDebugInfo(M);
726 
727  ModuleSymbolTable SymTab;
728  SymTab.addModule(&M);
729 
730  for (GlobalVariable &GV : M.globals())
731  if (GV.hasAppendingLinkage())
732  Mod.Keep.push_back(&GV);
733 
734  DenseSet<GlobalObject *> AliasedGlobals;
735  for (auto &GA : M.aliases())
736  if (GlobalObject *GO = GA.getAliaseeObject())
737  AliasedGlobals.insert(GO);
738 
739  // In this function we need IR GlobalValues matching the symbols in Syms
740  // (which is not backed by a module), so we need to enumerate them in the same
741  // order. The symbol enumeration order of a ModuleSymbolTable intentionally
742  // matches the order of an irsymtab, but when we read the irsymtab in
743  // InputFile::create we omit some symbols that are irrelevant to LTO. The
744  // Skip() function skips the same symbols from the module as InputFile does
745  // from the symbol table.
746  auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
747  auto Skip = [&]() {
748  while (MsymI != MsymE) {
749  auto Flags = SymTab.getSymbolFlags(*MsymI);
750  if ((Flags & object::BasicSymbolRef::SF_Global) &&
752  return;
753  ++MsymI;
754  }
755  };
756  Skip();
757 
758  std::set<const Comdat *> NonPrevailingComdats;
759  SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
760  for (const InputFile::Symbol &Sym : Syms) {
761  assert(ResI != ResE);
762  SymbolResolution Res = *ResI++;
763 
764  assert(MsymI != MsymE);
765  ModuleSymbolTable::Symbol Msym = *MsymI++;
766  Skip();
767 
768  if (GlobalValue *GV = Msym.dyn_cast<GlobalValue *>()) {
769  if (Res.Prevailing) {
770  if (Sym.isUndefined())
771  continue;
772  Mod.Keep.push_back(GV);
773  // For symbols re-defined with linker -wrap and -defsym options,
774  // set the linkage to weak to inhibit IPO. The linkage will be
775  // restored by the linker.
776  if (Res.LinkerRedefined)
777  GV->setLinkage(GlobalValue::WeakAnyLinkage);
778 
779  GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
780  if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
781  GV->setLinkage(GlobalValue::getWeakLinkage(
782  GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
783  } else if (isa<GlobalObject>(GV) &&
784  (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
785  GV->hasAvailableExternallyLinkage()) &&
786  !AliasedGlobals.count(cast<GlobalObject>(GV))) {
787  // Any of the above three types of linkage indicates that the
788  // chosen prevailing symbol will have the same semantics as this copy of
789  // the symbol, so we may be able to link it with available_externally
790  // linkage. We will decide later whether to do that when we link this
791  // module (in linkRegularLTO), based on whether it is undefined.
792  Mod.Keep.push_back(GV);
794  if (GV->hasComdat())
795  NonPrevailingComdats.insert(GV->getComdat());
796  cast<GlobalObject>(GV)->setComdat(nullptr);
797  }
798 
799  // Set the 'local' flag based on the linker resolution for this symbol.
801  GV->setDSOLocal(true);
802  if (GV->hasDLLImportStorageClass())
803  GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
804  DefaultStorageClass);
805  }
806  } else if (auto *AS = Msym.dyn_cast<ModuleSymbolTable::AsmSymbol *>()) {
807  // Collect non-prevailing symbols.
808  if (!Res.Prevailing)
809  NonPrevailingAsmSymbols.insert(AS->first);
810  } else {
811  llvm_unreachable("unknown symbol type");
812  }
813 
814  // Common resolution: collect the maximum size/alignment over all commons.
815  // We also record if we see an instance of a common as prevailing, so that
816  // if none is prevailing we can ignore it later.
817  if (Sym.isCommon()) {
818  // FIXME: We should figure out what to do about commons defined by asm.
819  // For now they aren't reported correctly by ModuleSymbolTable.
820  auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
821  CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
822  MaybeAlign SymAlign(Sym.getCommonAlignment());
823  if (SymAlign)
824  CommonRes.Align = max(*SymAlign, CommonRes.Align);
825  CommonRes.Prevailing |= Res.Prevailing;
826  }
827  }
828 
829  if (!M.getComdatSymbolTable().empty())
830  for (GlobalValue &GV : M.global_values())
831  handleNonPrevailingComdat(GV, NonPrevailingComdats);
832 
833  // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
834  // block.
835  if (!M.getModuleInlineAsm().empty()) {
836  std::string NewIA = ".lto_discard";
837  if (!NonPrevailingAsmSymbols.empty()) {
838  // Don't dicard a symbol if there is a live .symver for it.
840  M, [&](StringRef Name, StringRef Alias) {
841  if (!NonPrevailingAsmSymbols.count(Alias))
842  NonPrevailingAsmSymbols.erase(Name);
843  });
844  NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
845  }
846  NewIA += "\n";
847  M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
848  }
849 
850  assert(MsymI == MsymE);
851  return std::move(Mod);
852 }
853 
854 Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
855  bool LivenessFromIndex) {
856  std::vector<GlobalValue *> Keep;
857  for (GlobalValue *GV : Mod.Keep) {
858  if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
859  if (Function *F = dyn_cast<Function>(GV)) {
860  if (DiagnosticOutputFile) {
861  if (Error Err = F->materialize())
862  return Err;
863  OptimizationRemarkEmitter ORE(F, nullptr);
864  ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
865  << ore::NV("Function", F)
866  << " not added to the combined module ");
867  }
868  }
869  continue;
870  }
871 
872  if (!GV->hasAvailableExternallyLinkage()) {
873  Keep.push_back(GV);
874  continue;
875  }
876 
877  // Only link available_externally definitions if we don't already have a
878  // definition.
879  GlobalValue *CombinedGV =
880  RegularLTO.CombinedModule->getNamedValue(GV->getName());
881  if (CombinedGV && !CombinedGV->isDeclaration())
882  continue;
883 
884  Keep.push_back(GV);
885  }
886 
887  return RegularLTO.Mover->move(std::move(Mod.M), Keep,
889  /* IsPerformingImport */ false);
890 }
891 
892 // Add a ThinLTO module to the link.
893 Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
894  const SymbolResolution *&ResI,
895  const SymbolResolution *ResE) {
896  if (Error Err =
897  BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
898  ThinLTO.ModuleMap.size()))
899  return Err;
900 
901  for (const InputFile::Symbol &Sym : Syms) {
902  assert(ResI != ResE);
903  SymbolResolution Res = *ResI++;
904 
905  if (!Sym.getIRName().empty()) {
907  Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
908  if (Res.Prevailing) {
909  ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
910 
911  // For linker redefined symbols (via --wrap or --defsym) we want to
912  // switch the linkage to `weak` to prevent IPOs from happening.
913  // Find the summary in the module for this very GV and record the new
914  // linkage so that we can switch it when we import the GV.
915  if (Res.LinkerRedefined)
916  if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
917  GUID, BM.getModuleIdentifier()))
918  S->setLinkage(GlobalValue::WeakAnyLinkage);
919  }
920 
921  // If the linker resolved the symbol to a local definition then mark it
922  // as local in the summary for the module we are adding.
924  if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
925  GUID, BM.getModuleIdentifier())) {
926  S->setDSOLocal(true);
927  }
928  }
929  }
930  }
931 
932  if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
933  return make_error<StringError>(
934  "Expected at most one ThinLTO module per bitcode file",
936 
937  if (!Conf.ThinLTOModulesToCompile.empty()) {
938  if (!ThinLTO.ModulesToCompile)
939  ThinLTO.ModulesToCompile = ModuleMapType();
940  // This is a fuzzy name matching where only modules with name containing the
941  // specified switch values are going to be compiled.
942  for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
943  if (BM.getModuleIdentifier().contains(Name)) {
944  ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
945  llvm::errs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
946  << " to compile\n";
947  }
948  }
949  }
950 
951  return Error::success();
952 }
953 
954 unsigned LTO::getMaxTasks() const {
955  CalledGetMaxTasks = true;
956  auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
957  : ThinLTO.ModuleMap.size();
958  return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
959 }
960 
961 // If only some of the modules were split, we cannot correctly handle
962 // code that contains type tests or type checked loads.
963 Error LTO::checkPartiallySplit() {
964  if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
965  return Error::success();
966 
967  Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
968  Intrinsic::getName(Intrinsic::type_test));
969  Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
970  Intrinsic::getName(Intrinsic::type_checked_load));
971 
972  // First check if there are type tests / type checked loads in the
973  // merged regular LTO module IR.
974  if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
975  (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()))
976  return make_error<StringError>(
977  "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
979 
980  // Otherwise check if there are any recorded in the combined summary from the
981  // ThinLTO modules.
982  for (auto &P : ThinLTO.CombinedIndex) {
983  for (auto &S : P.second.SummaryList) {
984  auto *FS = dyn_cast<FunctionSummary>(S.get());
985  if (!FS)
986  continue;
987  if (!FS->type_test_assume_vcalls().empty() ||
988  !FS->type_checked_load_vcalls().empty() ||
989  !FS->type_test_assume_const_vcalls().empty() ||
990  !FS->type_checked_load_const_vcalls().empty() ||
991  !FS->type_tests().empty())
992  return make_error<StringError>(
993  "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
995  }
996  }
997  return Error::success();
998 }
999 
1001  // Compute "dead" symbols, we don't want to import/export these!
1002  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1003  DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1004  for (auto &Res : GlobalResolutions) {
1005  // Normally resolution have IR name of symbol. We can do nothing here
1006  // otherwise. See comments in GlobalResolution struct for more details.
1007  if (Res.second.IRName.empty())
1008  continue;
1009 
1011  GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1012 
1013  if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1014  GUIDPreservedSymbols.insert(GUID);
1015 
1016  if (Res.second.ExportDynamic)
1017  DynamicExportSymbols.insert(GUID);
1018 
1019  GUIDPrevailingResolutions[GUID] =
1021  }
1022 
1023  auto isPrevailing = [&](GlobalValue::GUID G) {
1024  auto It = GUIDPrevailingResolutions.find(G);
1025  if (It == GUIDPrevailingResolutions.end())
1026  return PrevailingType::Unknown;
1027  return It->second;
1028  };
1029  computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1030  isPrevailing, Conf.OptLevel > 0);
1031 
1032  // Setup output file to emit statistics.
1033  auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1034  if (!StatsFileOrErr)
1035  return StatsFileOrErr.takeError();
1036  std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1037 
1038  Error Result = runRegularLTO(AddStream);
1039  if (!Result)
1040  Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1041 
1042  if (StatsFile)
1043  PrintStatisticsJSON(StatsFile->os());
1044 
1045  return Result;
1046 }
1047 
1048 Error LTO::runRegularLTO(AddStreamFn AddStream) {
1049  // Setup optimization remarks.
1050  auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1051  RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
1054  if (!DiagFileOrErr)
1055  return DiagFileOrErr.takeError();
1056  DiagnosticOutputFile = std::move(*DiagFileOrErr);
1057 
1058  // Finalize linking of regular LTO modules containing summaries now that
1059  // we have computed liveness information.
1060  for (auto &M : RegularLTO.ModsWithSummaries)
1061  if (Error Err = linkRegularLTO(std::move(M),
1062  /*LivenessFromIndex=*/true))
1063  return Err;
1064 
1065  // Ensure we don't have inconsistently split LTO units with type tests.
1066  // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1067  // this path both cases but eventually this should be split into two and
1068  // do the ThinLTO checks in `runThinLTO`.
1069  if (Error Err = checkPartiallySplit())
1070  return Err;
1071 
1072  // Make sure commons have the right size/alignment: we kept the largest from
1073  // all the prevailing when adding the inputs, and we apply it here.
1074  const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1075  for (auto &I : RegularLTO.Commons) {
1076  if (!I.second.Prevailing)
1077  // Don't do anything if no instance of this common was prevailing.
1078  continue;
1079  GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1080  if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1081  // Don't create a new global if the type is already correct, just make
1082  // sure the alignment is correct.
1083  OldGV->setAlignment(I.second.Align);
1084  continue;
1085  }
1086  ArrayType *Ty =
1087  ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1088  auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1090  ConstantAggregateZero::get(Ty), "");
1091  GV->setAlignment(I.second.Align);
1092  if (OldGV) {
1093  OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
1094  GV->takeName(OldGV);
1095  OldGV->eraseFromParent();
1096  } else {
1097  GV->setName(I.first);
1098  }
1099  }
1100 
1101  // If allowed, upgrade public vcall visibility metadata to linkage unit
1102  // visibility before whole program devirtualization in the optimizer.
1103  updateVCallVisibilityInModule(*RegularLTO.CombinedModule,
1105  DynamicExportSymbols);
1106 
1107  if (Conf.PreOptModuleHook &&
1108  !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1109  return Error::success();
1110 
1111  if (!Conf.CodeGenOnly) {
1112  for (const auto &R : GlobalResolutions) {
1113  if (!R.second.isPrevailingIRSymbol())
1114  continue;
1115  if (R.second.Partition != 0 &&
1116  R.second.Partition != GlobalResolution::External)
1117  continue;
1118 
1119  GlobalValue *GV =
1120  RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1121  // Ignore symbols defined in other partitions.
1122  // Also skip declarations, which are not allowed to have internal linkage.
1123  if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1124  continue;
1125  GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1127  if (EnableLTOInternalization && R.second.Partition == 0)
1129  }
1130 
1131  RegularLTO.CombinedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
1132 
1133  if (Conf.PostInternalizeModuleHook &&
1134  !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1135  return Error::success();
1136  }
1137 
1138  if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1139  if (Error Err =
1140  backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1141  *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1142  return Err;
1143  }
1144 
1145  return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1146 }
1147 
1148 static const char *libcallRoutineNames[] = {
1149 #define HANDLE_LIBCALL(code, name) name,
1150 #include "llvm/IR/RuntimeLibcalls.def"
1151 #undef HANDLE_LIBCALL
1152 };
1153 
1156 }
1157 
1158 /// This class defines the interface to the ThinLTO backend.
1160 protected:
1161  const Config &Conf;
1164 
1165 public:
1166  ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1167  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries)
1168  : Conf(Conf), CombinedIndex(CombinedIndex),
1169  ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
1170 
1171  virtual ~ThinBackendProc() {}
1172  virtual Error start(
1173  unsigned Task, BitcodeModule BM,
1174  const FunctionImporter::ImportMapTy &ImportList,
1175  const FunctionImporter::ExportSetTy &ExportList,
1176  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1177  MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
1178  virtual Error wait() = 0;
1179  virtual unsigned getThreadCount() = 0;
1180 };
1181 
1182 namespace {
1183 class InProcessThinBackend : public ThinBackendProc {
1184  ThreadPool BackendThreadPool;
1185  AddStreamFn AddStream;
1186  FileCache Cache;
1187  std::set<GlobalValue::GUID> CfiFunctionDefs;
1188  std::set<GlobalValue::GUID> CfiFunctionDecls;
1189 
1190  Optional<Error> Err;
1191  std::mutex ErrMu;
1192 
1193 public:
1194  InProcessThinBackend(
1195  const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1196  ThreadPoolStrategy ThinLTOParallelism,
1197  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1198  AddStreamFn AddStream, FileCache Cache)
1199  : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
1200  BackendThreadPool(ThinLTOParallelism), AddStream(std::move(AddStream)),
1201  Cache(std::move(Cache)) {
1202  for (auto &Name : CombinedIndex.cfiFunctionDefs())
1203  CfiFunctionDefs.insert(
1205  for (auto &Name : CombinedIndex.cfiFunctionDecls())
1206  CfiFunctionDecls.insert(
1208  }
1209 
1210  Error runThinLTOBackendThread(
1211  AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1212  ModuleSummaryIndex &CombinedIndex,
1213  const FunctionImporter::ImportMapTy &ImportList,
1214  const FunctionImporter::ExportSetTy &ExportList,
1215  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1216  const GVSummaryMapTy &DefinedGlobals,
1218  auto RunThinBackend = [&](AddStreamFn AddStream) {
1219  LTOLLVMContext BackendContext(Conf);
1220  Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1221  if (!MOrErr)
1222  return MOrErr.takeError();
1223 
1224  return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1225  ImportList, DefinedGlobals, &ModuleMap);
1226  };
1227 
1228  auto ModuleID = BM.getModuleIdentifier();
1229 
1230  if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
1231  all_of(CombinedIndex.getModuleHash(ModuleID),
1232  [](uint32_t V) { return V == 0; }))
1233  // Cache disabled or no entry for this module in the combined index or
1234  // no module hash.
1235  return RunThinBackend(AddStream);
1236 
1238  // The module may be cached, this helps handling it.
1239  computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
1240  ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
1241  CfiFunctionDecls);
1242  Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key);
1243  if (Error Err = CacheAddStreamOrErr.takeError())
1244  return Err;
1245  AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1246  if (CacheAddStream)
1247  return RunThinBackend(CacheAddStream);
1248 
1249  return Error::success();
1250  }
1251 
1252  Error start(
1253  unsigned Task, BitcodeModule BM,
1254  const FunctionImporter::ImportMapTy &ImportList,
1255  const FunctionImporter::ExportSetTy &ExportList,
1256  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1257  MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1258  StringRef ModulePath = BM.getModuleIdentifier();
1259  assert(ModuleToDefinedGVSummaries.count(ModulePath));
1260  const GVSummaryMapTy &DefinedGlobals =
1261  ModuleToDefinedGVSummaries.find(ModulePath)->second;
1262  BackendThreadPool.async(
1263  [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1264  const FunctionImporter::ImportMapTy &ImportList,
1265  const FunctionImporter::ExportSetTy &ExportList,
1266  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1267  &ResolvedODR,
1268  const GVSummaryMapTy &DefinedGlobals,
1270  if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1272  "thin backend");
1273  Error E = runThinLTOBackendThread(
1274  AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1275  ResolvedODR, DefinedGlobals, ModuleMap);
1276  if (E) {
1277  std::unique_lock<std::mutex> L(ErrMu);
1278  if (Err)
1279  Err = joinErrors(std::move(*Err), std::move(E));
1280  else
1281  Err = std::move(E);
1282  }
1283  if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1285  },
1286  BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1287  std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1288  return Error::success();
1289  }
1290 
1291  Error wait() override {
1292  BackendThreadPool.wait();
1293  if (Err)
1294  return std::move(*Err);
1295  else
1296  return Error::success();
1297  }
1298 
1299  unsigned getThreadCount() override {
1300  return BackendThreadPool.getThreadCount();
1301  }
1302 };
1303 } // end anonymous namespace
1304 
1306  return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1307  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1308  AddStreamFn AddStream, FileCache Cache) {
1309  return std::make_unique<InProcessThinBackend>(
1310  Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream,
1311  Cache);
1312  };
1313 }
1314 
1315 // Given the original \p Path to an output file, replace any path
1316 // prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1317 // resulting directory if it does not yet exist.
1318 std::string lto::getThinLTOOutputFile(const std::string &Path,
1319  const std::string &OldPrefix,
1320  const std::string &NewPrefix) {
1321  if (OldPrefix.empty() && NewPrefix.empty())
1322  return Path;
1323  SmallString<128> NewPath(Path);
1324  llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1325  StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1326  if (!ParentPath.empty()) {
1327  // Make sure the new directory exists, creating it if necessary.
1328  if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1329  llvm::errs() << "warning: could not create directory '" << ParentPath
1330  << "': " << EC.message() << '\n';
1331  }
1332  return std::string(NewPath.str());
1333 }
1334 
1335 namespace {
1336 class WriteIndexesThinBackend : public ThinBackendProc {
1337  std::string OldPrefix, NewPrefix;
1338  bool ShouldEmitImportsFiles;
1339  raw_fd_ostream *LinkedObjectsFile;
1340  lto::IndexWriteCallback OnWrite;
1341 
1342 public:
1343  WriteIndexesThinBackend(
1344  const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1345  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1346  std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1347  raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1348  : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
1349  OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1350  ShouldEmitImportsFiles(ShouldEmitImportsFiles),
1351  LinkedObjectsFile(LinkedObjectsFile), OnWrite(OnWrite) {}
1352 
1353  Error start(
1354  unsigned Task, BitcodeModule BM,
1355  const FunctionImporter::ImportMapTy &ImportList,
1356  const FunctionImporter::ExportSetTy &ExportList,
1357  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1358  MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1359  StringRef ModulePath = BM.getModuleIdentifier();
1360  std::string NewModulePath =
1361  getThinLTOOutputFile(std::string(ModulePath), OldPrefix, NewPrefix);
1362 
1363  if (LinkedObjectsFile)
1364  *LinkedObjectsFile << NewModulePath << '\n';
1365 
1366  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1367  gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1368  ImportList, ModuleToSummariesForIndex);
1369 
1370  std::error_code EC;
1371  raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1373  if (EC)
1374  return errorCodeToError(EC);
1375  WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
1376 
1377  if (ShouldEmitImportsFiles) {
1378  EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1379  ModuleToSummariesForIndex);
1380  if (EC)
1381  return errorCodeToError(EC);
1382  }
1383 
1384  if (OnWrite)
1385  OnWrite(std::string(ModulePath));
1386  return Error::success();
1387  }
1388 
1389  Error wait() override { return Error::success(); }
1390 
1391  // WriteIndexesThinBackend should always return 1 to prevent module
1392  // re-ordering and avoid non-determinism in the final link.
1393  unsigned getThreadCount() override { return 1; }
1394 };
1395 } // end anonymous namespace
1396 
1398  std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1399  raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1400  return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1401  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1402  AddStreamFn AddStream, FileCache Cache) {
1403  return std::make_unique<WriteIndexesThinBackend>(
1404  Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
1405  ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite);
1406  };
1407 }
1408 
1409 Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1410  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1411  timeTraceProfilerBegin("ThinLink", StringRef(""));
1412  auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
1415  });
1416  if (ThinLTO.ModuleMap.empty())
1417  return Error::success();
1418 
1419  if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
1420  llvm::errs() << "warning: [ThinLTO] No module compiled\n";
1421  return Error::success();
1422  }
1423 
1424  if (Conf.CombinedIndexHook &&
1425  !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
1426  return Error::success();
1427 
1428  // Collect for each module the list of function it defines (GUID ->
1429  // Summary).
1431  ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
1432  ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1433  ModuleToDefinedGVSummaries);
1434  // Create entries for any modules that didn't have any GV summaries
1435  // (either they didn't have any GVs to start with, or we suppressed
1436  // generation of the summaries because they e.g. had inline assembly
1437  // uses that couldn't be promoted/renamed on export). This is so
1438  // InProcessThinBackend::start can still launch a backend thread, which
1439  // is passed the map of summaries for the module, without any special
1440  // handling for this case.
1441  for (auto &Mod : ThinLTO.ModuleMap)
1442  if (!ModuleToDefinedGVSummaries.count(Mod.first))
1443  ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1444 
1445  // Synthesize entry counts for functions in the CombinedIndex.
1446  computeSyntheticCounts(ThinLTO.CombinedIndex);
1447 
1449  ThinLTO.ModuleMap.size());
1451  ThinLTO.ModuleMap.size());
1453 
1454  if (DumpThinCGSCCs)
1455  ThinLTO.CombinedIndex.dumpSCCs(outs());
1456 
1457  std::set<GlobalValue::GUID> ExportedGUIDs;
1458 
1459  // If allowed, upgrade public vcall visibility to linkage unit visibility in
1460  // the summaries before whole program devirtualization below.
1461  updateVCallVisibilityInIndex(ThinLTO.CombinedIndex,
1463  DynamicExportSymbols);
1464 
1465  // Perform index-based WPD. This will return immediately if there are
1466  // no index entries in the typeIdMetadata map (e.g. if we are instead
1467  // performing IR-based WPD in hybrid regular/thin LTO mode).
1468  std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1469  runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
1470  LocalWPDTargetsMap);
1471 
1472  if (Conf.OptLevel > 0)
1473  ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1474  ImportLists, ExportLists);
1475 
1476  // Figure out which symbols need to be internalized. This also needs to happen
1477  // at -O0 because summary-based DCE is implemented using internalization, and
1478  // we must apply DCE consistently with the full LTO module in order to avoid
1479  // undefined references during the final link.
1480  for (auto &Res : GlobalResolutions) {
1481  // If the symbol does not have external references or it is not prevailing,
1482  // then not need to mark it as exported from a ThinLTO partition.
1483  if (Res.second.Partition != GlobalResolution::External ||
1484  !Res.second.isPrevailingIRSymbol())
1485  continue;
1486  auto GUID = GlobalValue::getGUID(
1487  GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1488  // Mark exported unless index-based analysis determined it to be dead.
1489  if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
1490  ExportedGUIDs.insert(GUID);
1491  }
1492 
1493  // Any functions referenced by the jump table in the regular LTO object must
1494  // be exported.
1495  for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
1496  ExportedGUIDs.insert(
1498  for (auto &Decl : ThinLTO.CombinedIndex.cfiFunctionDecls())
1499  ExportedGUIDs.insert(
1501 
1502  auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
1503  const auto &ExportList = ExportLists.find(ModuleIdentifier);
1504  return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
1505  ExportedGUIDs.count(VI.getGUID());
1506  };
1507 
1508  // Update local devirtualized targets that were exported by cross-module
1509  // importing or by other devirtualizations marked in the ExportedGUIDs set.
1510  updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
1511  LocalWPDTargetsMap);
1512 
1513  auto isPrevailing = [&](GlobalValue::GUID GUID,
1514  const GlobalValueSummary *S) {
1515  return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
1516  };
1517  thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
1518  isPrevailing);
1519 
1520  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1521  GlobalValue::GUID GUID,
1522  GlobalValue::LinkageTypes NewLinkage) {
1523  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
1524  };
1525  thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
1526  recordNewLinkage, GUIDPreservedSymbols);
1527 
1528  thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
1529 
1530  generateParamAccessSummary(ThinLTO.CombinedIndex);
1531 
1534 
1535  TimeTraceScopeExit.release();
1536 
1537  std::unique_ptr<ThinBackendProc> BackendProc =
1538  ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1539  AddStream, Cache);
1540 
1541  auto &ModuleMap =
1542  ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
1543 
1544  auto ProcessOneModule = [&](int I) -> Error {
1545  auto &Mod = *(ModuleMap.begin() + I);
1546  // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
1547  // combined module and parallel code generation partitions.
1548  return BackendProc->start(RegularLTO.ParallelCodeGenParallelismLevel + I,
1549  Mod.second, ImportLists[Mod.first],
1550  ExportLists[Mod.first], ResolvedODR[Mod.first],
1551  ThinLTO.ModuleMap);
1552  };
1553 
1554  if (BackendProc->getThreadCount() == 1) {
1555  // Process the modules in the order they were provided on the command-line.
1556  // It is important for this codepath to be used for WriteIndexesThinBackend,
1557  // to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
1558  // order as the inputs, which otherwise would affect the final link order.
1559  for (int I = 0, E = ModuleMap.size(); I != E; ++I)
1560  if (Error E = ProcessOneModule(I))
1561  return E;
1562  } else {
1563  // When executing in parallel, process largest bitsize modules first to
1564  // improve parallelism, and avoid starving the thread pool near the end.
1565  // This saves about 15 sec on a 36-core machine while link `clang.exe` (out
1566  // of 100 sec).
1567  std::vector<BitcodeModule *> ModulesVec;
1568  ModulesVec.reserve(ModuleMap.size());
1569  for (auto &Mod : ModuleMap)
1570  ModulesVec.push_back(&Mod.second);
1571  for (int I : generateModulesOrdering(ModulesVec))
1572  if (Error E = ProcessOneModule(I))
1573  return E;
1574  }
1575  return BackendProc->wait();
1576 }
1577 
1582  std::string Filename = std::string(RemarksFilename);
1583  // For ThinLTO, file.opt.<format> becomes
1584  // file.opt.<format>.thin.<num>.<format>.
1585  if (!Filename.empty() && Count != -1)
1586  Filename =
1587  (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
1588  .str();
1589 
1590  auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
1593  if (Error E = ResultOrErr.takeError())
1594  return std::move(E);
1595 
1596  if (*ResultOrErr)
1597  (*ResultOrErr)->keep();
1598 
1599  return ResultOrErr;
1600 }
1601 
1604  // Setup output file to emit statistics.
1605  if (StatsFilename.empty())
1606  return nullptr;
1607 
1608  llvm::EnableStatistics(false);
1609  std::error_code EC;
1610  auto StatsFile =
1611  std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
1612  if (EC)
1613  return errorCodeToError(EC);
1614 
1615  StatsFile->keep();
1616  return std::move(StatsFile);
1617 }
1618 
1619 // Compute the ordering we will process the inputs: the rough heuristic here
1620 // is to sort them per size so that the largest module get schedule as soon as
1621 // possible. This is purely a compile-time optimization.
1623  std::vector<int> ModulesOrdering;
1624  ModulesOrdering.resize(R.size());
1625  std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
1626  llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1627  auto LSize = R[LeftIndex]->getBuffer().size();
1628  auto RSize = R[RightIndex]->getBuffer().size();
1629  return LSize > RSize;
1630  });
1631  return ModulesOrdering;
1632 }
llvm::RemarksPasses
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"))
Definition: ThinLTOCodeGenerator.cpp:82
MemoryBuffer.h
StackSafetyAnalysis.h
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:415
llvm::BitcodeModule::parseModule
Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
Read the entire bitcode module and return it.
Definition: BitcodeReader.cpp:6995
llvm::FileCache
std::function< Expected< AddStreamFn >(unsigned Task, StringRef Key)> FileCache
This is the type of a file cache.
Definition: Caching.h:53
FunctionImportUtils.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::tgtok::Def
@ Def
Definition: TGLexer.h:50
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModuleSymbolTable::AsmSymbol
std::pair< std::string, uint32_t > AsmSymbol
Definition: ModuleSymbolTable.h:35
llvm::sys::path::replace_path_prefix
bool replace_path_prefix(SmallVectorImpl< char > &Path, StringRef OldPrefix, StringRef NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition: Path.cpp:518
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:152
llvm::BitcodeModule::readSummary
Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, uint64_t ModuleId)
Parse the specified bitcode buffer and merge its module summary index into CombinedIndex.
Definition: BitcodeReader.cpp:6838
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:757
llvm::GlobalValue::getGlobalIdentifier
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:157
Metadata.h
llvm::object::readIRSymtab
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
Definition: IRObjectFile.cpp:136
llvm::UpgradeDebugInfo
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
Definition: AutoUpgrade.cpp:4027
llvm::lto::InputFile::Symbol
The purpose of this class is to only expose the symbol information that an LTO client should need in ...
Definition: LTO.h:134
llvm::ModuleSummaryIndex::getModuleHash
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
Definition: ModuleSummaryIndex.h:1444
llvm::lto::finalizeOptimizationRemarks
Error finalizeOptimizationRemarks(std::unique_ptr< ToolOutputFile > DiagOutputFile)
Definition: LTOBackend.cpp:496
llvm::GlobalValue::dropLLVMManglingEscape
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:488
llvm::ThreadPool
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:37
llvm::computeLTOCacheKey
void computeLTOCacheKey(SmallString< 40 > &Key, const lto::Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, const GVSummaryMapTy &DefinedGlobals, const std::set< GlobalValue::GUID > &CfiFunctionDefs={}, const std::set< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition: LTO.cpp:79
llvm::lto::Config::CGOptLevel
CodeGenOpt::Level CGOptLevel
Definition: Config.h:55
llvm::Function
Definition: Function.h:62
llvm::lto::Config::TimeTraceGranularity
unsigned TimeTraceGranularity
Time trace granularity.
Definition: Config.h:172
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::lto::Config::RemarksFormat
std::string RemarksFormat
The format used for serializing remarks (default: YAML).
Definition: Config.h:157
llvm::lto::Config::CodeModel
Optional< CodeModel::Model > CodeModel
Definition: Config.h:54
Statistic.h
llvm::lto::Config::RemarksWithHotness
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition: Config.h:139
handleNonPrevailingComdat
static void handleNonPrevailingComdat(GlobalValue &GV, std::set< const Comdat * > &NonPrevailingComdats)
Definition: LTO.cpp:688
Path.h
llvm::support::endian::write32le
void write32le(void *P, uint32_t V)
Definition: Endian.h:416
DEBUG_TYPE
#define DEBUG_TYPE
Definition: LTO.cpp:65
llvm::GlobalValue::isLocalLinkage
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:332
llvm::detail::DenseSetImpl::find
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:179
llvm::ThreadPoolStrategy
This tells how a thread pool will be used.
Definition: Threading.h:119
llvm::AddStreamFn
std::function< Expected< std::unique_ptr< CachedFileStream > >(unsigned Task)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
Definition: Caching.h:39
llvm::GlobalValue::UnnamedAddr::None
@ None
llvm::runWholeProgramDevirtOnIndex
void runWholeProgramDevirtOnIndex(ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary >> &LocalWPDTargetsMap)
Perform index-based whole program devirtualization on the Summary index.
Definition: WholeProgramDevirt.cpp:833
llvm::RemarksFilename
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
Definition: ThinLTOCodeGenerator.cpp:81
ManagedStatic.h
llvm::Intrinsic::getName
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:879
llvm::lto::InputFile::getSingleBitcodeModule
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:505
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2233
Error.h
OptimizationRemarkEmitter.h
llvm::lto::ThinBackendProc::Conf
const Config & Conf
Definition: LTO.cpp:1161
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::lto::InputFile::~InputFile
~InputFile()
llvm::lto::Config::ThinLTOModulesToCompile
std::vector< std::string > ThinLTOModulesToCompile
Specific thinLTO modules to compile.
Definition: Config.h:166
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::heavyweight_hardware_concurrency
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition: Threading.h:165
llvm::lto::ThinBackend
std::function< std::unique_ptr< ThinBackendProc >(const Config &C, ModuleSummaryIndex &CombinedIndex, StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache)> ThinBackend
A ThinBackend defines what happens after the thin-link phase during ThinLTO.
Definition: LTO.h:197
llvm::lto::SymbolResolution::Prevailing
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition: LTO.h:428
llvm::StringMap::end
iterator end()
Definition: StringMap.h:203
llvm::RemarksHotnessThreshold
cl::opt< Optional< uint64_t >, false, remarks::HotnessThresholdParser > RemarksHotnessThreshold("lto-pass-remarks-hotness-threshold", cl::desc("Minimum profile count required for an " "optimization remark to be output." " Use 'auto' to apply the threshold from profile summary."), cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden)
Definition: ThinLTOCodeGenerator.cpp:85
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
llvm::GlobalValue::UnnamedAddr::Global
@ Global
llvm::lto::Config
LTO configuration.
Definition: Config.h:40
llvm::Optional
Definition: APInt.h:33
llvm::lto::Config::Freestanding
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition: Config.h:65
llvm::GlobalValue::DefaultVisibility
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:63
llvm::MapVector< StringRef, BitcodeModule >
llvm::RemarksWithHotness
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
Definition: ThinLTOCodeGenerator.cpp:83
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
llvm::lto::setupLLVMOptimizationRemarks
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, Optional< uint64_t > RemarksHotnessThreshold=0, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1578
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::thinLTOResolvePrevailingInIndex
void thinLTOResolvePrevailingInIndex(const lto::Config &C, ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Resolve linkage for prevailing symbols in the Index.
Definition: LTO.cpp:399
llvm::GlobalValue::setUnnamedAddr
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:212
llvm::GlobalValue::LinkageTypes
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
llvm::ArrayType
Class to represent array types.
Definition: DerivedTypes.h:357
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:216
llvm::gatherImportedSummariesForModule
void gatherImportedSummariesForModule(StringRef ModulePath, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
Definition: FunctionImport.cpp:979
llvm::GlobalValue::isLinkOnceODRLinkage
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:308
llvm::generateParamAccessSummary
void generateParamAccessSummary(ModuleSummaryIndex &Index)
Definition: StackSafetyAnalysis.cpp:1104
llvm::lto::Config::CodeGenOnly
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition: Config.h:68
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
LegacyPassManager.h
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::count
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
PassManagerBuilder.h
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:239
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::timeTraceProfilerFinishThread
void timeTraceProfilerFinishThread()
Finish a time trace profiler running on a worker thread.
Definition: TimeProfiler.cpp:283
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::EmitImportsFiles
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
Definition: FunctionImport.cpp:1003
llvm::lto::Config::CombinedIndexHook
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:249
llvm::lto::setupStatsFile
Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition: LTO.cpp:1603
llvm::lto::getThinLTOOutputFile
std::string getThinLTOOutputFile(const std::string &Path, const std::string &OldPrefix, const std::string &NewPrefix)
Given the original Path to an output file, replace any path prefix matching OldPrefix with NewPrefix.
Definition: LTO.cpp:1318
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:207
llvm::timeTraceProfilerInitialize
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity, StringRef ProcName)
Initialize the time trace profiler.
Definition: TimeProfiler.cpp:262
Context
ManagedStatic< detail::RecordContext > Context
Definition: Record.cpp:96
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:288
llvm::BitcodeModule::getModuleIdentifier
StringRef getModuleIdentifier() const
Definition: BitcodeReader.h:95
llvm::lto::Config::FromPrevailing
@ FromPrevailing
Definition: Config.h:42
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
CommandLine.h
llvm::lto::ThinBackendProc::~ThinBackendProc
virtual ~ThinBackendProc()
Definition: LTO.cpp:1171
llvm::GlobalValueSummary
Function and variable summary information to aid decisions and implementation of importing.
Definition: ModuleSummaryIndex.h:290
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1581
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:246
llvm::MapVector::begin
iterator begin()
Definition: MapVector.h:70
llvm::Triple::isOSBinFormatELF
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:635
TargetMachine.h
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
llvm::GlobalValue::VisibilityTypes
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:62
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:885
llvm::GlobalValue::isAppendingLinkage
static bool isAppendingLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:323
llvm::setupLLVMOptimizationRemarks
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, Optional< uint64_t > RemarksHotnessThreshold=0)
Setup optimization remarks that output to a file.
Definition: LLVMRemarkStreamer.cpp:92
llvm::lto::Config::SampleProfile
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:113
llvm::SHA1
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:26
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
BitcodeWriter.h
Intrinsics.h
llvm::timeTraceProfilerBegin
void timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
Definition: TimeProfiler.cpp:315
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::GlobalObject
Definition: GlobalObject.h:28
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:90
llvm::lto::Config::Options
TargetOptions Options
Definition: Config.h:48
llvm::lto::Config::VisibilityScheme
VisScheme VisibilityScheme
Allows non-imported definitions to get the potentially more constraining visibility from the prevaili...
Definition: Config.h:89
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
TargetLibraryInfo.h
llvm::lto::Config::ELF
@ ELF
Definition: Config.h:43
false
Definition: StackSlotColoring.cpp:142
SplitModule.h
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:109
llvm::computeSyntheticCounts
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
Definition: SummaryBasedOptimizations.cpp:47
llvm::thinLTOInternalizeAndPromoteInIndex
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:459
llvm::make_scope_exit
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:58
llvm::detail::DenseSetImpl::size
size_type size() const
Definition: DenseSet.h:81
llvm::sys::fs::create_directories
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:963
EnableLTOInternalization
cl::opt< bool > EnableLTOInternalization("enable-lto-internalization", cl::init(true), cl::Hidden, cl::desc("Enable global value internalization in LTO"))
Enable global value internalization in LTO.
llvm::WriteIndexToFile
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
Definition: BitcodeWriter.cpp:4666
llvm::StringRef::contains
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:461
llvm::lto::Config::ResolutionFile
std::unique_ptr< raw_ostream > ResolutionFile
If this field is set, LTO will write input file paths and symbol resolutions here in llvm-lto2 comman...
Definition: Config.h:184
llvm::lto::InputFile::symbols
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition: LTO.h:159
llvm::lto::InputFile::create
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:472
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
thinLTOResolvePrevailingGUID
static void thinLTOResolvePrevailingGUID(const Config &C, ValueInfo VI, DenseSet< GlobalValueSummary * > &GlobalInvolvedWithAlias, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Definition: LTO.cpp:322
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:376
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:186
llvm::TargetOptions::RelaxELFRelocations
unsigned RelaxELFRelocations
Definition: TargetOptions.h:254
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::ModuleSymbolTable::symbols
ArrayRef< Symbol > symbols() const
Definition: ModuleSymbolTable.h:46
llvm::Comdat
Definition: Comdat.h:31
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:344
llvm::GlobalValue::getGUID
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:513
llvm::ModuleSymbolTable::addModule
void addModule(Module *M)
Definition: ModuleSymbolTable.cpp:57
llvm::lto::LTO::getMaxTasks
unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition: LTO.cpp:954
SourceMgr.h
llvm::lto::Config::StatsFile
std::string StatsFile
Statistics output file path.
Definition: Config.h:163
llvm::lto::Config::PostInternalizeModuleHook
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:224
llvm::lto::Config::OverrideTriple
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:103
llvm::ThreadPool::getThreadCount
unsigned getThreadCount() const
Definition: ThreadPool.h:68
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::ValueInfo
Struct that holds a reference to a particular GUID in a global value summary.
Definition: ModuleSummaryIndex.h:168
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
llvm::RemarksFormat
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"))
Definition: ThinLTOCodeGenerator.cpp:86
LTOBackend.h
llvm::lto::Config::OptLevel
unsigned OptLevel
Definition: Config.h:57
llvm::OutputFileType::Object
@ Object
llvm::updateVCallVisibilityInIndex
void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
Definition: WholeProgramDevirt.cpp:814
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
ThreadPool.h
llvm::lto::Config::RemarksFilename
std::string RemarksFilename
Optimization remarks file path.
Definition: Config.h:133
llvm::cl::opt< bool >
llvm::GlobalValue::hasAvailableExternallyLinkage
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:435
IRObjectFile.h
llvm::lto::Config::DefaultTriple
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition: Config.h:107
llvm::PrintStatisticsJSON
void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:205
llvm::IRMover
Definition: IRMover.h:25
llvm::lto::backend
Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex)
Runs a regular LTO backend.
Definition: LTOBackend.cpp:507
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition: DiagnosticInfo.h:422
llvm::GlobalValue
Definition: GlobalValue.h:44
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
VI
@ VI
Definition: SIInstrInfo.cpp:7679
llvm::object::BasicSymbolRef::SF_FormatSpecific
@ SF_FormatSpecific
Definition: SymbolicFile.h:115
Threading.h
llvm::SmallSet::count
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:164
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
uint64_t
llvm::pdb::Unknown
@ Unknown
Definition: PDBTypes.h:395
llvm::computeDeadSymbolsWithConstProp
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
Definition: FunctionImport.cpp:966
IPO.h
llvm::lto::Config::DwoDir
std::string DwoDir
The directory to store .dwo files.
Definition: Config.h:119
llvm::GlobalValue::WeakAnyLinkage
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
llvm::PointerUnion::dyn_cast
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwise returns null.
Definition: PointerUnion.h:165
llvm::symbolize::toHex
static std::string toHex(uint64_t V)
Definition: DIPrinter.cpp:276
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DenseMap
Definition: DenseMap.h:714
llvm::lto::LTOLLVMContext
A derived class of LLVMContext that initializes itself according to a given Config object.
Definition: Config.h:284
llvm::IRMover::ValueAdder
std::function< void(GlobalValue &)> ValueAdder
Definition: IRMover.h:64
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::lto::LTO::getRuntimeLibcallSymbols
static ArrayRef< const char * > getRuntimeLibcallSymbols()
Static method that returns a list of libcall symbols that can be generated by LTO but might not be vi...
Definition: LTO.cpp:1154
Analysis.h
StringExtras.h
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:428
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:460
llvm::TypeIdSummary
Definition: ModuleSummaryIndex.h:1024
llvm::EnableStatistics
void EnableStatistics(bool DoPrintOnExit=true)
Enable the collection and printing of statistics.
Definition: Statistic.cpp:133
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
llvm::ModuleSummaryIndex::modulePaths
const StringMap< std::pair< uint64_t, ModuleHash > > & modulePaths() const
Table of modules, containing module hash and id.
Definition: ModuleSummaryIndex.h:1429
llvm::StringMap::begin
iterator begin()
Definition: StringMap.h:202
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::sys::path::parent_path
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:467
thinLTOInternalizeAndPromoteGUID
static void thinLTOInternalizeAndPromoteGUID(ValueInfo VI, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Definition: LTO.cpp:429
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1639
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:451
llvm::updateIndexWPDForExports
void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, ValueInfo)> isExported, std::map< ValueInfo, std::vector< VTableSlotSummary >> &LocalWPDTargetsMap)
Call after cross-module importing to update the recorded single impl devirt target names for any loca...
Definition: WholeProgramDevirt.cpp:839
llvm::lto::Config::MAttrs
std::vector< std::string > MAttrs
Definition: Config.h:49
llvm::ArrayType::get
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:640
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::ModuleSummaryIndex::cfiFunctionDefs
std::set< std::string > & cfiFunctionDefs()
Definition: ModuleSummaryIndex.h:1348
IRMover.h
llvm::StringMapImpl::size
unsigned size() const
Definition: StringMap.h:93
llvm::PrevailingType::Unknown
@ Unknown
llvm::lto::Config::ProfileRemapping
std::string ProfileRemapping
Name remapping file for profile data.
Definition: Config.h:116
llvm::SmallSet::erase
bool erase(const T &V)
Definition: SmallSet.h:207
llvm::GlobalValue::AppendingLinkage
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
Mangler.h
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1562
llvm::SHA1::update
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA1.cpp:213
TargetOptions.h
llvm::GlobalValue::getWeakLinkage
static LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:295
llvm::lto::Config::RelocModel
Optional< Reloc::Model > RelocModel
Definition: Config.h:53
llvm::lto::InputFile::getName
StringRef getName() const
Returns the path to the InputFile.
Definition: LTO.cpp:501
llvm::ArrayRef< uint8_t >
llvm::updateVCallVisibilityInModule
void updateVCallVisibilityInModule(Module &M, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
Definition: WholeProgramDevirt.cpp:794
llvm::lto::Config::RemarksHotnessThreshold
llvm::Optional< uint64_t > RemarksHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition: Config.h:154
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::lto::Config::HasWholeProgramVisibility
bool HasWholeProgramVisibility
Asserts whether we can assume whole program visibility during the LTO link.
Definition: Config.h:78
llvm::lto::thinBackend
Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, MapVector< StringRef, BitcodeModule > *ModuleMap, const std::vector< uint8_t > &CmdArgs=std::vector< uint8_t >())
Runs a ThinLTO backend.
Definition: LTOBackend.cpp:553
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::lto::LTO::~LTO
~LTO()
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::ThreadPool::async
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:51
llvm::lto::Config::TimeTraceEnabled
bool TimeTraceEnabled
Time trace enabled.
Definition: Config.h:169
llvm::GlobalValue::WeakODRLinkage
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
llvm::lto::createWriteIndexesThinBackend
ThinBackend createWriteIndexesThinBackend(std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite)
Definition: LTO.cpp:1397
uint32_t
llvm::lto::LTO::run
Error run(AddStreamFn AddStream, FileCache Cache=nullptr)
Runs the LTO pipeline.
Definition: LTO.cpp:1000
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::lto::Config::AlwaysEmitRegularLTOObj
bool AlwaysEmitRegularLTOObj
Always emit a Regular LTO object even when it is empty because no Regular LTO modules were linked.
Definition: Config.h:83
llvm::lto::Config::OptPipeline
std::string OptPipeline
If this field is set, the set of passes run in the middle-end optimizer will be the one specified by ...
Definition: Config.h:94
llvm::ModuleSummaryIndex::cfiFunctionDecls
std::set< std::string > & cfiFunctionDecls()
Definition: ModuleSummaryIndex.h:1351
llvm::GlobalValue::AvailableExternallyLinkage
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
llvm::SmallSet::insert
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:180
llvm::GlobalValue::getAliaseeObject
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:316
llvm::Module::Error
@ Error
Emits an error if two values disagree, otherwise the resulting value is that of the operands.
Definition: Module.h:120
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:443
llvm::GlobalValue::CommonLinkage
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:58
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:120
llvm::ModuleSymbolTable::getSymbolFlags
uint32_t getSymbolFlags(Symbol S) const
Definition: ModuleSymbolTable.cpp:192
llvm::GlobalValue::isLinkOnceLinkage
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:311
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::lto::LTO::LTO
LTO(Config Conf, ThinBackend Backend=nullptr, unsigned ParallelCodeGenParallelismLevel=1)
Create an LTO object.
Definition: LTO.cpp:523
llvm::BitcodeModule::getLazyModule
Expected< std::unique_ptr< Module > > getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting)
Read the bitcode module and prepare for lazy deserialization of function bodies.
Definition: BitcodeReader.cpp:6828
llvm::TargetOptions::DebuggerTuning
DebuggerKind DebuggerTuning
Which debugger to tune for.
Definition: TargetOptions.h:387
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:838
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
LLVMRemarkStreamer.h
llvm::GlobalValue::isInterposableLinkage
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
Definition: GlobalValue.h:348
llvm::ModuleSymbolTable::CollectAsmSymvers
static void CollectAsmSymvers(const Module &M, function_ref< void(StringRef, StringRef)> AsmSymver)
Parse inline ASM and collect the symvers directives that are defined in the current module.
Definition: ModuleSymbolTable.cpp:170
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end
iterator end()
Definition: DenseMap.h:83
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
isWeakObjectWithRWAccess
static bool isWeakObjectWithRWAccess(GlobalValueSummary *GVS)
Definition: LTO.cpp:421
llvm::irsymtab::Reader::SymbolRef
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition: IRSymtab.h:313
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::thinLTOPropagateFunctionAttrs
bool thinLTOPropagateFunctionAttrs(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Propagate function attributes for function summaries along the index's callgraph during thinlink.
Definition: FunctionAttrs.cpp:436
llvm::timeTraceProfilerEnabled
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:33
llvm::SHA1::result
StringRef result()
Return a reference to the current raw 160-bits SHA1 for the digested data since the last call to init...
Definition: SHA1.cpp:289
llvm::GlobalValue::getComdat
const Comdat * getComdat() const
Definition: Globals.cpp:172
llvm::GlobalValue::getGUID
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:517
llvm::lto::SymbolResolution::FinalDefinitionInLinkageUnit
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition: LTO.h:432
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1522
llvm::lto::Config::AAPipeline
std::string AAPipeline
Definition: Config.h:99
llvm::MapVector::size
size_type size() const
Definition: MapVector.h:61
llvm::TargetOptions::DataSections
unsigned DataSections
Emit data into separate sections.
Definition: TargetOptions.h:260
llvm::lto::IndexWriteCallback
std::function< void(const std::string &)> IndexWriteCallback
This ThinBackend writes individual module indexes to files, instead of running the individual backend...
Definition: LTO.h:216
llvm::lto::Config::PreOptModuleHook
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module,...
Definition: Config.h:217
llvm::lto::SymbolResolution
The resolution for a symbol.
Definition: LTO.h:422
llvm::lto::ThinBackendProc::ModuleToDefinedGVSummaries
const StringMap< GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition: LTO.cpp:1163
llvm::PrevailingType::Yes
@ Yes
llvm::StringMap::count
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:244
llvm::lto::ThinBackendProc::CombinedIndex
ModuleSummaryIndex & CombinedIndex
Definition: LTO.cpp:1162
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:259
llvm::PrevailingType::No
@ No
WholeProgramDevirt.h
libcallRoutineNames
static const char * libcallRoutineNames[]
Definition: LTO.cpp:1148
llvm::lto::SymbolResolution::VisibleToRegularObj
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition: LTO.h:435
SHA1.h
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:474
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::X86AS::GS
@ GS
Definition: X86.h:187
llvm::GlobalValue::DLLStorageClassTypes
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition: GlobalValue.h:69
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:599
llvm::ThreadPool::wait
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
Definition: ThreadPool.cpp:69
llvm::OptimizationRemark
Diagnostic information for applied optimization remarks.
Definition: DiagnosticInfo.h:685
llvm::lto::generateModulesOrdering
std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:1622
llvm::ModuleSymbolTable
Definition: ModuleSymbolTable.h:33
llvm::lto::Config::CGFileType
CodeGenFileType CGFileType
Definition: Config.h:56
llvm::support::endian::write64le
void write64le(void *P, uint64_t V)
Definition: Endian.h:417
llvm::lto::Config::UseNewPM
bool UseNewPM
Use the new pass manager.
Definition: Config.h:61
llvm::GlobalValueSummary::getBaseObject
GlobalValueSummary * getBaseObject()
If this is an alias summary, returns the summary of the aliased object (a global variable or function...
Definition: ModuleSummaryIndex.h:499
LTO.h
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1078
llvm::object::BasicSymbolRef::SF_Global
@ SF_Global
Definition: SymbolicFile.h:109
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
TargetTransformInfo.h
AutoUpgrade.h
ScopeExit.h
llvm::SmallSet::empty
LLVM_NODISCARD bool empty() const
Definition: SmallSet.h:155
llvm::lto::ThinBackendProc::ThinBackendProc
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries)
Definition: LTO.cpp:1166
llvm::ComputeCrossModuleImport
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
Definition: FunctionImport.cpp:656
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
llvm::timeTraceProfilerEnd
void timeTraceProfilerEnd()
Manually end the last time section.
Definition: TimeProfiler.cpp:327
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
llvm::lto::createInProcessThinBackend
ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism)
This ThinBackend runs the individual backend jobs in-process.
Definition: LTO.cpp:1305
llvm::lto::Config::CPU
std::string CPU
Definition: Config.h:47
llvm::lto::ThinBackendProc
This class defines the interface to the ThinLTO backend.
Definition: LTO.cpp:1159
llvm::BitcodeModule
Represents a module in a bitcode file.
Definition: BitcodeReader.h:60
llvm::TargetOptions::FunctionSections
unsigned FunctionSections
Emit functions into separate sections.
Definition: TargetOptions.h:257
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:382
DumpThinCGSCCs
static cl::opt< bool > DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden, cl::desc("Dump the SCCs in the ThinLTO index's callgraph"))
llvm::cl::desc
Definition: CommandLine.h:412
llvm::lto::LTO::add
Error add(std::unique_ptr< InputFile > Obj, ArrayRef< SymbolResolution > Res)
Add an input file to the LTO link, using the provided symbol resolutions.
Definition: LTO.cpp:613
Mod
Module * Mod
Definition: PassBuilderBindings.cpp:54
BitcodeReader.h
llvm::GlobalObject::setAlignment
void setAlignment(MaybeAlign Align)
Definition: Globals.cpp:117
llvm::lto::SymbolResolution::ExportDynamic
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition: LTO.h:439
llvm::StringMap::const_iterator
StringMapConstIterator< ValueTy > const_iterator
Definition: StringMap.h:199
raw_ostream.h
SummaryBasedOptimizations.h
llvm::ConstantAggregateZero::get
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1675
DiagnosticPrinter.h
llvm::GlobalValue::LinkOnceODRLinkage
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
TargetRegistry.h
TimeProfiler.h
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:205
llvm::lto::SymbolResolution::LinkerRedefined
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition: LTO.h:443
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::StringMap::try_emplace
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&... Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:328
llvm::lto::Config::RemarksPasses
std::string RemarksPasses
Optimization remarks pass filter.
Definition: Config.h:136
llvm::Optional::getValue
constexpr const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:282
writeToResolutionFile
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition: LTO.cpp:589
llvm::lto::InputFile
An input file.
Definition: LTO.h:106
SmallSet.h