1 //===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements functions and classes used to support LTO.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/LTO/LTO.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/raw_ostream.h"
22 // Simple helper to load a module from bitcode
23 std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
24 LLVMContext &Context, bool Lazy) {
26 ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
29 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
30 /* ShouldLazyLoadMetadata */ Lazy);
32 ModuleOrErr = parseBitcodeFile(Buffer, Context);
34 if (std::error_code EC = ModuleOrErr.getError()) {
35 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
37 Err.print("ThinLTO", errs());
38 report_fatal_error("Can't load module, abort.");
40 return std::move(ModuleOrErr.get());
43 static void thinLTOResolveWeakForLinkerGUID(
44 GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
45 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
46 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
48 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
50 for (auto &S : GVSummaryList) {
51 if (GlobalInvolvedWithAlias.count(S.get()))
53 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
54 if (!GlobalValue::isWeakForLinker(OriginalLinkage))
56 // We need to emit only one of these. The prevailing module will keep it,
57 // but turned into a weak, while the others will drop it when possible.
58 if (isPrevailing(GUID, S.get())) {
59 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
60 S->setLinkage(GlobalValue::getWeakLinkage(
61 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
63 // Alias can't be turned into available_externally.
64 else if (!isa<AliasSummary>(S.get()) &&
65 (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage) ||
66 GlobalValue::isWeakODRLinkage(OriginalLinkage)))
67 S->setLinkage(GlobalValue::AvailableExternallyLinkage);
68 if (S->linkage() != OriginalLinkage)
69 recordNewLinkage(S->modulePath(), GUID, S->linkage());
73 // Resolve Weak and LinkOnce values in the \p Index.
75 // We'd like to drop these functions if they are no longer referenced in the
76 // current module. However there is a chance that another module is still
77 // referencing them because of the import. We make sure we always emit at least
79 void thinLTOResolveWeakForLinkerInIndex(
80 ModuleSummaryIndex &Index,
81 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
83 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
85 // We won't optimize the globals that are referenced by an alias for now
86 // Ideally we should turn the alias into a global and duplicate the definition
88 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
90 for (auto &S : I.second)
91 if (auto AS = dyn_cast<AliasSummary>(S.get()))
92 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
95 thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias,
96 isPrevailing, recordNewLinkage);
99 static void thinLTOInternalizeAndPromoteGUID(
100 GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
101 function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
102 for (auto &S : GVSummaryList) {
103 if (isExported(S->modulePath(), GUID)) {
104 if (GlobalValue::isLocalLinkage(S->linkage()))
105 S->setLinkage(GlobalValue::ExternalLinkage);
106 } else if (!GlobalValue::isLocalLinkage(S->linkage()))
107 S->setLinkage(GlobalValue::InternalLinkage);
111 // Update the linkages in the given \p Index to mark exported values
112 // as external and non-exported values as internal.
113 void thinLTOInternalizeAndPromoteInIndex(
114 ModuleSummaryIndex &Index,
115 function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
116 for (auto &I : Index)
117 thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);