101095a5dSDimitry Andric //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
201095a5dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601095a5dSDimitry Andric //
701095a5dSDimitry Andric //===----------------------------------------------------------------------===//
801095a5dSDimitry Andric //
901095a5dSDimitry Andric // This file implements the FunctionImportGlobalProcessing class, used
1001095a5dSDimitry Andric // to perform the necessary global value handling for function importing.
1101095a5dSDimitry Andric //
1201095a5dSDimitry Andric //===----------------------------------------------------------------------===//
1301095a5dSDimitry Andric
1401095a5dSDimitry Andric #include "llvm/Transforms/Utils/FunctionImportUtils.h"
15e3b55780SDimitry Andric #include "llvm/Support/CommandLine.h"
1601095a5dSDimitry Andric using namespace llvm;
1701095a5dSDimitry Andric
18e3b55780SDimitry Andric /// Uses the "source_filename" instead of a Module hash ID for the suffix of
19e3b55780SDimitry Andric /// promoted locals during LTO. NOTE: This requires that the source filename
20e3b55780SDimitry Andric /// has a unique name / path to avoid name collisions.
21e3b55780SDimitry Andric static cl::opt<bool> UseSourceFilenameForPromotedLocals(
22e3b55780SDimitry Andric "use-source-filename-for-promoted-locals", cl::Hidden,
23e3b55780SDimitry Andric cl::desc("Uses the source file name instead of the Module hash. "
24e3b55780SDimitry Andric "This requires that the source filename has a unique name / "
25e3b55780SDimitry Andric "path to avoid name collisions."));
26e3b55780SDimitry Andric
2701095a5dSDimitry Andric /// Checks if we should import SGV as a definition, otherwise import as a
2801095a5dSDimitry Andric /// declaration.
doImportAsDefinition(const GlobalValue * SGV)2901095a5dSDimitry Andric bool FunctionImportGlobalProcessing::doImportAsDefinition(
30706b4fc4SDimitry Andric const GlobalValue *SGV) {
31706b4fc4SDimitry Andric if (!isPerformingImport())
32706b4fc4SDimitry Andric return false;
3301095a5dSDimitry Andric
3401095a5dSDimitry Andric // Only import the globals requested for importing.
35044eb2f6SDimitry Andric if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
3601095a5dSDimitry Andric return false;
37044eb2f6SDimitry Andric
38044eb2f6SDimitry Andric assert(!isa<GlobalAlias>(SGV) &&
39044eb2f6SDimitry Andric "Unexpected global alias in the import list.");
40044eb2f6SDimitry Andric
41044eb2f6SDimitry Andric // Otherwise yes.
42044eb2f6SDimitry Andric return true;
4301095a5dSDimitry Andric }
4401095a5dSDimitry Andric
shouldPromoteLocalToGlobal(const GlobalValue * SGV,ValueInfo VI)45b915e9e0SDimitry Andric bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
46706b4fc4SDimitry Andric const GlobalValue *SGV, ValueInfo VI) {
4701095a5dSDimitry Andric assert(SGV->hasLocalLinkage());
484b4fe385SDimitry Andric
494b4fe385SDimitry Andric // Ifuncs and ifunc alias does not have summary.
504b4fe385SDimitry Andric if (isa<GlobalIFunc>(SGV) ||
514b4fe385SDimitry Andric (isa<GlobalAlias>(SGV) &&
524b4fe385SDimitry Andric isa<GlobalIFunc>(cast<GlobalAlias>(SGV)->getAliaseeObject())))
534b4fe385SDimitry Andric return false;
544b4fe385SDimitry Andric
5501095a5dSDimitry Andric // Both the imported references and the original local variable must
5601095a5dSDimitry Andric // be promoted.
5701095a5dSDimitry Andric if (!isPerformingImport() && !isModuleExporting())
5801095a5dSDimitry Andric return false;
5901095a5dSDimitry Andric
60b915e9e0SDimitry Andric if (isPerformingImport()) {
6171d5a254SDimitry Andric assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
6271d5a254SDimitry Andric !isNonRenamableLocal(*SGV)) &&
637e7b6700SDimitry Andric "Attempting to promote non-renamable local");
64b915e9e0SDimitry Andric // We don't know for sure yet if we are importing this value (as either
65b915e9e0SDimitry Andric // a reference or a def), since we are simply walking all values in the
66b915e9e0SDimitry Andric // module. But by necessity if we end up importing it and it is local,
67b915e9e0SDimitry Andric // it must be promoted, so unconditionally promote all values in the
68b915e9e0SDimitry Andric // importing module.
6901095a5dSDimitry Andric return true;
7001095a5dSDimitry Andric }
7101095a5dSDimitry Andric
72909545a8SDimitry Andric // When exporting, consult the index. We can have more than one local
73909545a8SDimitry Andric // with the same GUID, in the case of same-named locals in different but
74909545a8SDimitry Andric // same-named source files that were compiled in their respective directories
75909545a8SDimitry Andric // (so the source file name and resulting GUID is the same). Find the one
76909545a8SDimitry Andric // in this module.
77909545a8SDimitry Andric auto Summary = ImportIndex.findSummaryInModule(
78706b4fc4SDimitry Andric VI, SGV->getParent()->getModuleIdentifier());
79909545a8SDimitry Andric assert(Summary && "Missing summary for global value when exporting");
80909545a8SDimitry Andric auto Linkage = Summary->linkage();
81b915e9e0SDimitry Andric if (!GlobalValue::isLocalLinkage(Linkage)) {
827e7b6700SDimitry Andric assert(!isNonRenamableLocal(*SGV) &&
837e7b6700SDimitry Andric "Attempting to promote non-renamable local");
84b915e9e0SDimitry Andric return true;
85b915e9e0SDimitry Andric }
86b915e9e0SDimitry Andric
87b915e9e0SDimitry Andric return false;
88b915e9e0SDimitry Andric }
89b915e9e0SDimitry Andric
907e7b6700SDimitry Andric #ifndef NDEBUG
isNonRenamableLocal(const GlobalValue & GV) const917e7b6700SDimitry Andric bool FunctionImportGlobalProcessing::isNonRenamableLocal(
927e7b6700SDimitry Andric const GlobalValue &GV) const {
937e7b6700SDimitry Andric if (!GV.hasLocalLinkage())
947e7b6700SDimitry Andric return false;
957e7b6700SDimitry Andric // This needs to stay in sync with the logic in buildModuleSummaryIndex.
967e7b6700SDimitry Andric if (GV.hasSection())
977e7b6700SDimitry Andric return true;
987e7b6700SDimitry Andric if (Used.count(const_cast<GlobalValue *>(&GV)))
997e7b6700SDimitry Andric return true;
1007e7b6700SDimitry Andric return false;
1017e7b6700SDimitry Andric }
1027e7b6700SDimitry Andric #endif
1037e7b6700SDimitry Andric
104706b4fc4SDimitry Andric std::string
getPromotedName(const GlobalValue * SGV)105706b4fc4SDimitry Andric FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
106706b4fc4SDimitry Andric assert(SGV->hasLocalLinkage());
107e3b55780SDimitry Andric
10801095a5dSDimitry Andric // For locals that must be promoted to global scope, ensure that
10901095a5dSDimitry Andric // the promoted name uniquely identifies the copy in the original module,
110706b4fc4SDimitry Andric // using the ID assigned during combined index creation.
111e3b55780SDimitry Andric if (UseSourceFilenameForPromotedLocals &&
112e3b55780SDimitry Andric !SGV->getParent()->getSourceFileName().empty()) {
113e3b55780SDimitry Andric SmallString<256> Suffix(SGV->getParent()->getSourceFileName());
114e3b55780SDimitry Andric std::replace_if(std::begin(Suffix), std::end(Suffix),
115e3b55780SDimitry Andric [&](char ch) { return !isAlnum(ch); }, '_');
116e3b55780SDimitry Andric return ModuleSummaryIndex::getGlobalNameForLocal(
117e3b55780SDimitry Andric SGV->getName(), Suffix);
118e3b55780SDimitry Andric }
119e3b55780SDimitry Andric
12001095a5dSDimitry Andric return ModuleSummaryIndex::getGlobalNameForLocal(
12101095a5dSDimitry Andric SGV->getName(),
12201095a5dSDimitry Andric ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
12301095a5dSDimitry Andric }
12401095a5dSDimitry Andric
12501095a5dSDimitry Andric GlobalValue::LinkageTypes
getLinkage(const GlobalValue * SGV,bool DoPromote)126b915e9e0SDimitry Andric FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
127b915e9e0SDimitry Andric bool DoPromote) {
12801095a5dSDimitry Andric // Any local variable that is referenced by an exported function needs
12901095a5dSDimitry Andric // to be promoted to global scope. Since we don't currently know which
13001095a5dSDimitry Andric // functions reference which local variables/functions, we must treat
13101095a5dSDimitry Andric // all as potentially exported if this module is exporting anything.
13201095a5dSDimitry Andric if (isModuleExporting()) {
133b915e9e0SDimitry Andric if (SGV->hasLocalLinkage() && DoPromote)
13401095a5dSDimitry Andric return GlobalValue::ExternalLinkage;
13501095a5dSDimitry Andric return SGV->getLinkage();
13601095a5dSDimitry Andric }
13701095a5dSDimitry Andric
13801095a5dSDimitry Andric // Otherwise, if we aren't importing, no linkage change is needed.
13901095a5dSDimitry Andric if (!isPerformingImport())
14001095a5dSDimitry Andric return SGV->getLinkage();
14101095a5dSDimitry Andric
14201095a5dSDimitry Andric switch (SGV->getLinkage()) {
143044eb2f6SDimitry Andric case GlobalValue::LinkOnceODRLinkage:
14401095a5dSDimitry Andric case GlobalValue::ExternalLinkage:
145044eb2f6SDimitry Andric // External and linkonce definitions are converted to available_externally
14601095a5dSDimitry Andric // definitions upon import, so that they are available for inlining
14701095a5dSDimitry Andric // and/or optimization, but are turned into declarations later
14801095a5dSDimitry Andric // during the EliminateAvailableExternally pass.
149e6d15924SDimitry Andric if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
15001095a5dSDimitry Andric return GlobalValue::AvailableExternallyLinkage;
15101095a5dSDimitry Andric // An imported external declaration stays external.
15201095a5dSDimitry Andric return SGV->getLinkage();
15301095a5dSDimitry Andric
15401095a5dSDimitry Andric case GlobalValue::AvailableExternallyLinkage:
15501095a5dSDimitry Andric // An imported available_externally definition converts
15601095a5dSDimitry Andric // to external if imported as a declaration.
15701095a5dSDimitry Andric if (!doImportAsDefinition(SGV))
15801095a5dSDimitry Andric return GlobalValue::ExternalLinkage;
15901095a5dSDimitry Andric // An imported available_externally declaration stays that way.
16001095a5dSDimitry Andric return SGV->getLinkage();
16101095a5dSDimitry Andric
162d8e91e46SDimitry Andric case GlobalValue::LinkOnceAnyLinkage:
16301095a5dSDimitry Andric case GlobalValue::WeakAnyLinkage:
164d8e91e46SDimitry Andric // Can't import linkonce_any/weak_any definitions correctly, or we might
165d8e91e46SDimitry Andric // change the program semantics, since the linker will pick the first
166d8e91e46SDimitry Andric // linkonce_any/weak_any definition and importing would change the order
167d8e91e46SDimitry Andric // they are seen by the linker. The module linking caller needs to enforce
168d8e91e46SDimitry Andric // this.
16901095a5dSDimitry Andric assert(!doImportAsDefinition(SGV));
17001095a5dSDimitry Andric // If imported as a declaration, it becomes external_weak.
17101095a5dSDimitry Andric return SGV->getLinkage();
17201095a5dSDimitry Andric
17301095a5dSDimitry Andric case GlobalValue::WeakODRLinkage:
17401095a5dSDimitry Andric // For weak_odr linkage, there is a guarantee that all copies will be
17501095a5dSDimitry Andric // equivalent, so the issue described above for weak_any does not exist,
17601095a5dSDimitry Andric // and the definition can be imported. It can be treated similarly
17701095a5dSDimitry Andric // to an imported externally visible global value.
178e6d15924SDimitry Andric if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
17901095a5dSDimitry Andric return GlobalValue::AvailableExternallyLinkage;
18001095a5dSDimitry Andric else
18101095a5dSDimitry Andric return GlobalValue::ExternalLinkage;
18201095a5dSDimitry Andric
18301095a5dSDimitry Andric case GlobalValue::AppendingLinkage:
18401095a5dSDimitry Andric // It would be incorrect to import an appending linkage variable,
18501095a5dSDimitry Andric // since it would cause global constructors/destructors to be
18601095a5dSDimitry Andric // executed multiple times. This should have already been handled
18701095a5dSDimitry Andric // by linkIfNeeded, and we will assert in shouldLinkFromSource
18801095a5dSDimitry Andric // if we try to import, so we simply return AppendingLinkage.
18901095a5dSDimitry Andric return GlobalValue::AppendingLinkage;
19001095a5dSDimitry Andric
19101095a5dSDimitry Andric case GlobalValue::InternalLinkage:
19201095a5dSDimitry Andric case GlobalValue::PrivateLinkage:
19301095a5dSDimitry Andric // If we are promoting the local to global scope, it is handled
19401095a5dSDimitry Andric // similarly to a normal externally visible global.
195b915e9e0SDimitry Andric if (DoPromote) {
196e6d15924SDimitry Andric if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
19701095a5dSDimitry Andric return GlobalValue::AvailableExternallyLinkage;
19801095a5dSDimitry Andric else
19901095a5dSDimitry Andric return GlobalValue::ExternalLinkage;
20001095a5dSDimitry Andric }
20101095a5dSDimitry Andric // A non-promoted imported local definition stays local.
20201095a5dSDimitry Andric // The ThinLTO pass will eventually force-import their definitions.
20301095a5dSDimitry Andric return SGV->getLinkage();
20401095a5dSDimitry Andric
20501095a5dSDimitry Andric case GlobalValue::ExternalWeakLinkage:
20601095a5dSDimitry Andric // External weak doesn't apply to definitions, must be a declaration.
20701095a5dSDimitry Andric assert(!doImportAsDefinition(SGV));
20801095a5dSDimitry Andric // Linkage stays external_weak.
20901095a5dSDimitry Andric return SGV->getLinkage();
21001095a5dSDimitry Andric
21101095a5dSDimitry Andric case GlobalValue::CommonLinkage:
21201095a5dSDimitry Andric // Linkage stays common on definitions.
21301095a5dSDimitry Andric // The ThinLTO pass will eventually force-import their definitions.
21401095a5dSDimitry Andric return SGV->getLinkage();
21501095a5dSDimitry Andric }
21601095a5dSDimitry Andric
21701095a5dSDimitry Andric llvm_unreachable("unknown linkage type");
21801095a5dSDimitry Andric }
21901095a5dSDimitry Andric
processGlobalForThinLTO(GlobalValue & GV)22001095a5dSDimitry Andric void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
221044eb2f6SDimitry Andric
222d8e91e46SDimitry Andric ValueInfo VI;
223d8e91e46SDimitry Andric if (GV.hasName()) {
224d8e91e46SDimitry Andric VI = ImportIndex.getValueInfo(GV.getGUID());
225d8e91e46SDimitry Andric // Set synthetic function entry counts.
226d8e91e46SDimitry Andric if (VI && ImportIndex.hasSyntheticEntryCounts()) {
227d8e91e46SDimitry Andric if (Function *F = dyn_cast<Function>(&GV)) {
228d8e91e46SDimitry Andric if (!F->isDeclaration()) {
229e3b55780SDimitry Andric for (const auto &S : VI.getSummaryList()) {
2301d5ae102SDimitry Andric auto *FS = cast<FunctionSummary>(S->getBaseObject());
231d8e91e46SDimitry Andric if (FS->modulePath() == M.getModuleIdentifier()) {
232d8e91e46SDimitry Andric F->setEntryCount(Function::ProfileCount(FS->entryCount(),
233d8e91e46SDimitry Andric Function::PCT_Synthetic));
234d8e91e46SDimitry Andric break;
235d8e91e46SDimitry Andric }
236d8e91e46SDimitry Andric }
237d8e91e46SDimitry Andric }
238d8e91e46SDimitry Andric }
239d8e91e46SDimitry Andric }
240044eb2f6SDimitry Andric }
241044eb2f6SDimitry Andric
242706b4fc4SDimitry Andric // We should always have a ValueInfo (i.e. GV in index) for definitions when
243706b4fc4SDimitry Andric // we are exporting, and also when importing that value.
244706b4fc4SDimitry Andric assert(VI || GV.isDeclaration() ||
245706b4fc4SDimitry Andric (isPerformingImport() && !doImportAsDefinition(&GV)));
246706b4fc4SDimitry Andric
247e6d15924SDimitry Andric // Mark read/write-only variables which can be imported with specific
248e6d15924SDimitry Andric // attribute. We can't internalize them now because IRMover will fail
249e6d15924SDimitry Andric // to link variable definitions to their external declarations during
250e6d15924SDimitry Andric // ThinLTO import. We'll internalize read-only variables later, after
251e6d15924SDimitry Andric // import is finished. See internalizeGVsAfterImport.
252d8e91e46SDimitry Andric //
253d8e91e46SDimitry Andric // If global value dead stripping is not enabled in summary then
254d8e91e46SDimitry Andric // propagateConstants hasn't been run. We can't internalize GV
255d8e91e46SDimitry Andric // in such case.
256706b4fc4SDimitry Andric if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
257706b4fc4SDimitry Andric if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
258706b4fc4SDimitry Andric // We can have more than one local with the same GUID, in the case of
259706b4fc4SDimitry Andric // same-named locals in different but same-named source files that were
260706b4fc4SDimitry Andric // compiled in their respective directories (so the source file name
261706b4fc4SDimitry Andric // and resulting GUID is the same). Find the one in this module.
262706b4fc4SDimitry Andric // Handle the case where there is no summary found in this module. That
263706b4fc4SDimitry Andric // can happen in the distributed ThinLTO backend, because the index only
264706b4fc4SDimitry Andric // contains summaries from the source modules if they are being imported.
265706b4fc4SDimitry Andric // We might have a non-null VI and get here even in that case if the name
266706b4fc4SDimitry Andric // matches one in this module (e.g. weak or appending linkage).
267706b4fc4SDimitry Andric auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
268706b4fc4SDimitry Andric ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
269706b4fc4SDimitry Andric if (GVS &&
270706b4fc4SDimitry Andric (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
271706b4fc4SDimitry Andric V->addAttribute("thinlto-internalize");
272706b4fc4SDimitry Andric // Objects referenced by writeonly GV initializer should not be
273706b4fc4SDimitry Andric // promoted, because there is no any kind of read access to them
274706b4fc4SDimitry Andric // on behalf of this writeonly GV. To avoid promotion we convert
275706b4fc4SDimitry Andric // GV initializer to 'zeroinitializer'. This effectively drops
276706b4fc4SDimitry Andric // references in IR module (not in combined index), so we can
277706b4fc4SDimitry Andric // ignore them when computing import. We do not export references
278706b4fc4SDimitry Andric // of writeonly object. See computeImportForReferencedGlobals
279706b4fc4SDimitry Andric if (ImportIndex.isWriteOnly(GVS))
280706b4fc4SDimitry Andric V->setInitializer(Constant::getNullValue(V->getValueType()));
281706b4fc4SDimitry Andric }
282706b4fc4SDimitry Andric }
283d8e91e46SDimitry Andric }
284d8e91e46SDimitry Andric
285706b4fc4SDimitry Andric if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
286e6d15924SDimitry Andric // Save the original name string before we rename GV below.
287e6d15924SDimitry Andric auto Name = GV.getName().str();
288706b4fc4SDimitry Andric GV.setName(getPromotedName(&GV));
289706b4fc4SDimitry Andric GV.setLinkage(getLinkage(&GV, /* DoPromote */ true));
290706b4fc4SDimitry Andric assert(!GV.hasLocalLinkage());
29101095a5dSDimitry Andric GV.setVisibility(GlobalValue::HiddenVisibility);
292e6d15924SDimitry Andric
293e6d15924SDimitry Andric // If we are renaming a COMDAT leader, ensure that we record the COMDAT
294e6d15924SDimitry Andric // for later renaming as well. This is required for COFF.
295e6d15924SDimitry Andric if (const auto *C = GV.getComdat())
296e6d15924SDimitry Andric if (C->getName() == Name)
297e6d15924SDimitry Andric RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
29801095a5dSDimitry Andric } else
299b915e9e0SDimitry Andric GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
30001095a5dSDimitry Andric
301cfca06d7SDimitry Andric // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
302cfca06d7SDimitry Andric // converted to a declaration, to disable direct access. Don't do this if GV
303cfca06d7SDimitry Andric // is implicitly dso_local due to a non-default visibility.
304344a3780SDimitry Andric if (ClearDSOLocalOnDeclarations &&
305344a3780SDimitry Andric (GV.isDeclarationForLinker() ||
306344a3780SDimitry Andric (isPerformingImport() && !doImportAsDefinition(&GV))) &&
307cfca06d7SDimitry Andric !GV.isImplicitDSOLocal()) {
308cfca06d7SDimitry Andric GV.setDSOLocal(false);
309344a3780SDimitry Andric } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
310cfca06d7SDimitry Andric // If all summaries are dso_local, symbol gets resolved to a known local
311cfca06d7SDimitry Andric // definition.
312cfca06d7SDimitry Andric GV.setDSOLocal(true);
313cfca06d7SDimitry Andric if (GV.hasDLLImportStorageClass())
314cfca06d7SDimitry Andric GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
315cfca06d7SDimitry Andric }
316cfca06d7SDimitry Andric
31701095a5dSDimitry Andric // Remove functions imported as available externally defs from comdats,
31801095a5dSDimitry Andric // as this is a declaration for the linker, and will be dropped eventually.
31901095a5dSDimitry Andric // It is illegal for comdats to contain declarations.
320d8e91e46SDimitry Andric auto *GO = dyn_cast<GlobalObject>(&GV);
32101095a5dSDimitry Andric if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
32201095a5dSDimitry Andric // The IRMover should not have placed any imported declarations in
32301095a5dSDimitry Andric // a comdat, so the only declaration that should be in a comdat
32401095a5dSDimitry Andric // at this point would be a definition imported as available_externally.
32501095a5dSDimitry Andric assert(GO->hasAvailableExternallyLinkage() &&
32601095a5dSDimitry Andric "Expected comdat on definition (possibly available external)");
32701095a5dSDimitry Andric GO->setComdat(nullptr);
32801095a5dSDimitry Andric }
32901095a5dSDimitry Andric }
33001095a5dSDimitry Andric
processGlobalsForThinLTO()33101095a5dSDimitry Andric void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
33201095a5dSDimitry Andric for (GlobalVariable &GV : M.globals())
33301095a5dSDimitry Andric processGlobalForThinLTO(GV);
33401095a5dSDimitry Andric for (Function &SF : M)
33501095a5dSDimitry Andric processGlobalForThinLTO(SF);
33601095a5dSDimitry Andric for (GlobalAlias &GA : M.aliases())
33701095a5dSDimitry Andric processGlobalForThinLTO(GA);
338e6d15924SDimitry Andric
339e6d15924SDimitry Andric // Replace any COMDATS that required renaming (because the COMDAT leader was
340e6d15924SDimitry Andric // promoted and renamed).
341e6d15924SDimitry Andric if (!RenamedComdats.empty())
342e6d15924SDimitry Andric for (auto &GO : M.global_objects())
343e6d15924SDimitry Andric if (auto *C = GO.getComdat()) {
344e6d15924SDimitry Andric auto Replacement = RenamedComdats.find(C);
345e6d15924SDimitry Andric if (Replacement != RenamedComdats.end())
346e6d15924SDimitry Andric GO.setComdat(Replacement->second);
347e6d15924SDimitry Andric }
34801095a5dSDimitry Andric }
34901095a5dSDimitry Andric
run()35001095a5dSDimitry Andric bool FunctionImportGlobalProcessing::run() {
35101095a5dSDimitry Andric processGlobalsForThinLTO();
35201095a5dSDimitry Andric return false;
35301095a5dSDimitry Andric }
35401095a5dSDimitry Andric
renameModuleForThinLTO(Module & M,const ModuleSummaryIndex & Index,bool ClearDSOLocalOnDeclarations,SetVector<GlobalValue * > * GlobalsToImport)35571d5a254SDimitry Andric bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
356cfca06d7SDimitry Andric bool ClearDSOLocalOnDeclarations,
35771d5a254SDimitry Andric SetVector<GlobalValue *> *GlobalsToImport) {
358cfca06d7SDimitry Andric FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
359cfca06d7SDimitry Andric ClearDSOLocalOnDeclarations);
36001095a5dSDimitry Andric return ThinLTOProcessing.run();
36101095a5dSDimitry Andric }
362