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