1488a800aSTeresa Johnson //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
2488a800aSTeresa Johnson //
3488a800aSTeresa Johnson //                     The LLVM Compiler Infrastructure
4488a800aSTeresa Johnson //
5488a800aSTeresa Johnson // This file is distributed under the University of Illinois Open Source
6488a800aSTeresa Johnson // License. See LICENSE.TXT for details.
7488a800aSTeresa Johnson //
8488a800aSTeresa Johnson //===----------------------------------------------------------------------===//
9488a800aSTeresa Johnson //
10488a800aSTeresa Johnson // This file implements the FunctionImportGlobalProcessing class, used
11488a800aSTeresa Johnson // to perform the necessary global value handling for function importing.
12488a800aSTeresa Johnson //
13488a800aSTeresa Johnson //===----------------------------------------------------------------------===//
14488a800aSTeresa Johnson 
15488a800aSTeresa Johnson #include "llvm/Transforms/Utils/FunctionImportUtils.h"
16df5ef871STeresa Johnson #include "llvm/IR/InstIterator.h"
17488a800aSTeresa Johnson using namespace llvm;
18488a800aSTeresa Johnson 
19488a800aSTeresa Johnson /// Checks if we should import SGV as a definition, otherwise import as a
20488a800aSTeresa Johnson /// declaration.
21488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doImportAsDefinition(
226d8f817fSPeter Collingbourne     const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
238d05185aSMehdi Amini 
248d05185aSMehdi Amini   // Only import the globals requested for importing.
2572c0b1ccSDavid Blaikie   if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
2672c0b1ccSDavid Blaikie     return false;
272f0cc477SDavid Blaikie 
282f0cc477SDavid Blaikie   assert(!isa<GlobalAlias>(SGV) &&
292f0cc477SDavid Blaikie          "Unexpected global alias in the import list.");
302f0cc477SDavid Blaikie 
3172c0b1ccSDavid Blaikie   // Otherwise yes.
3272c0b1ccSDavid Blaikie   return true;
33488a800aSTeresa Johnson }
34488a800aSTeresa Johnson 
35488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doImportAsDefinition(
36488a800aSTeresa Johnson     const GlobalValue *SGV) {
37488a800aSTeresa Johnson   if (!isPerformingImport())
38488a800aSTeresa Johnson     return false;
398d05185aSMehdi Amini   return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
408d05185aSMehdi Amini                                                               GlobalsToImport);
41488a800aSTeresa Johnson }
42488a800aSTeresa Johnson 
4338d4df71STeresa Johnson bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
44488a800aSTeresa Johnson     const GlobalValue *SGV) {
45488a800aSTeresa Johnson   assert(SGV->hasLocalLinkage());
46488a800aSTeresa Johnson   // Both the imported references and the original local variable must
47488a800aSTeresa Johnson   // be promoted.
48488a800aSTeresa Johnson   if (!isPerformingImport() && !isModuleExporting())
49488a800aSTeresa Johnson     return false;
50488a800aSTeresa Johnson 
514fef68cbSTeresa Johnson   if (isPerformingImport()) {
526d8f817fSPeter Collingbourne     assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
536d8f817fSPeter Collingbourne             !isNonRenamableLocal(*SGV)) &&
54519465b9STeresa Johnson            "Attempting to promote non-renamable local");
554fef68cbSTeresa Johnson     // We don't know for sure yet if we are importing this value (as either
564fef68cbSTeresa Johnson     // a reference or a def), since we are simply walking all values in the
574fef68cbSTeresa Johnson     // module. But by necessity if we end up importing it and it is local,
584fef68cbSTeresa Johnson     // it must be promoted, so unconditionally promote all values in the
594fef68cbSTeresa Johnson     // importing module.
604fef68cbSTeresa Johnson     return true;
610515fb8dSTeresa Johnson   }
62b4e1e829SMehdi Amini 
639006d526STeresa Johnson   // When exporting, consult the index. We can have more than one local
649006d526STeresa Johnson   // with the same GUID, in the case of same-named locals in different but
659006d526STeresa Johnson   // same-named source files that were compiled in their respective directories
669006d526STeresa Johnson   // (so the source file name and resulting GUID is the same). Find the one
679006d526STeresa Johnson   // in this module.
689006d526STeresa Johnson   auto Summary = ImportIndex.findSummaryInModule(
699006d526STeresa Johnson       SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
709006d526STeresa Johnson   assert(Summary && "Missing summary for global value when exporting");
719006d526STeresa Johnson   auto Linkage = Summary->linkage();
724fef68cbSTeresa Johnson   if (!GlobalValue::isLocalLinkage(Linkage)) {
73519465b9STeresa Johnson     assert(!isNonRenamableLocal(*SGV) &&
74519465b9STeresa Johnson            "Attempting to promote non-renamable local");
75488a800aSTeresa Johnson     return true;
76488a800aSTeresa Johnson   }
77488a800aSTeresa Johnson 
784fef68cbSTeresa Johnson   return false;
794fef68cbSTeresa Johnson }
804fef68cbSTeresa Johnson 
81519465b9STeresa Johnson #ifndef NDEBUG
82519465b9STeresa Johnson bool FunctionImportGlobalProcessing::isNonRenamableLocal(
83519465b9STeresa Johnson     const GlobalValue &GV) const {
84519465b9STeresa Johnson   if (!GV.hasLocalLinkage())
85519465b9STeresa Johnson     return false;
86519465b9STeresa Johnson   // This needs to stay in sync with the logic in buildModuleSummaryIndex.
87519465b9STeresa Johnson   if (GV.hasSection())
88519465b9STeresa Johnson     return true;
89519465b9STeresa Johnson   if (Used.count(const_cast<GlobalValue *>(&GV)))
90519465b9STeresa Johnson     return true;
91519465b9STeresa Johnson   return false;
92519465b9STeresa Johnson }
93519465b9STeresa Johnson #endif
94519465b9STeresa Johnson 
951b9c2be8STeresa Johnson std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
961b9c2be8STeresa Johnson                                                     bool DoPromote) {
97488a800aSTeresa Johnson   // For locals that must be promoted to global scope, ensure that
98488a800aSTeresa Johnson   // the promoted name uniquely identifies the copy in the original module,
99488a800aSTeresa Johnson   // using the ID assigned during combined index creation. When importing,
100488a800aSTeresa Johnson   // we rename all locals (not just those that are promoted) in order to
101488a800aSTeresa Johnson   // avoid naming conflicts between locals imported from different modules.
1021b9c2be8STeresa Johnson   if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
10326ab5772STeresa Johnson     return ModuleSummaryIndex::getGlobalNameForLocal(
104488a800aSTeresa Johnson         SGV->getName(),
105ae280e54SMehdi Amini         ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
106488a800aSTeresa Johnson   return SGV->getName();
107488a800aSTeresa Johnson }
108488a800aSTeresa Johnson 
109488a800aSTeresa Johnson GlobalValue::LinkageTypes
1101b9c2be8STeresa Johnson FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
1111b9c2be8STeresa Johnson                                            bool DoPromote) {
112488a800aSTeresa Johnson   // Any local variable that is referenced by an exported function needs
113488a800aSTeresa Johnson   // to be promoted to global scope. Since we don't currently know which
114488a800aSTeresa Johnson   // functions reference which local variables/functions, we must treat
115488a800aSTeresa Johnson   // all as potentially exported if this module is exporting anything.
116488a800aSTeresa Johnson   if (isModuleExporting()) {
1171b9c2be8STeresa Johnson     if (SGV->hasLocalLinkage() && DoPromote)
118488a800aSTeresa Johnson       return GlobalValue::ExternalLinkage;
119488a800aSTeresa Johnson     return SGV->getLinkage();
120488a800aSTeresa Johnson   }
121488a800aSTeresa Johnson 
122488a800aSTeresa Johnson   // Otherwise, if we aren't importing, no linkage change is needed.
123488a800aSTeresa Johnson   if (!isPerformingImport())
124488a800aSTeresa Johnson     return SGV->getLinkage();
125488a800aSTeresa Johnson 
126488a800aSTeresa Johnson   switch (SGV->getLinkage()) {
1272f0cc477SDavid Blaikie   case GlobalValue::LinkOnceAnyLinkage:
1282f0cc477SDavid Blaikie   case GlobalValue::LinkOnceODRLinkage:
129488a800aSTeresa Johnson   case GlobalValue::ExternalLinkage:
1302f0cc477SDavid Blaikie     // External and linkonce definitions are converted to available_externally
131488a800aSTeresa Johnson     // definitions upon import, so that they are available for inlining
132488a800aSTeresa Johnson     // and/or optimization, but are turned into declarations later
133488a800aSTeresa Johnson     // during the EliminateAvailableExternally pass.
134488a800aSTeresa Johnson     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
135488a800aSTeresa Johnson       return GlobalValue::AvailableExternallyLinkage;
136488a800aSTeresa Johnson     // An imported external declaration stays external.
137488a800aSTeresa Johnson     return SGV->getLinkage();
138488a800aSTeresa Johnson 
139488a800aSTeresa Johnson   case GlobalValue::AvailableExternallyLinkage:
140488a800aSTeresa Johnson     // An imported available_externally definition converts
141488a800aSTeresa Johnson     // to external if imported as a declaration.
142488a800aSTeresa Johnson     if (!doImportAsDefinition(SGV))
143488a800aSTeresa Johnson       return GlobalValue::ExternalLinkage;
144488a800aSTeresa Johnson     // An imported available_externally declaration stays that way.
145488a800aSTeresa Johnson     return SGV->getLinkage();
146488a800aSTeresa Johnson 
147488a800aSTeresa Johnson   case GlobalValue::WeakAnyLinkage:
148488a800aSTeresa Johnson     // Can't import weak_any definitions correctly, or we might change the
149488a800aSTeresa Johnson     // program semantics, since the linker will pick the first weak_any
150488a800aSTeresa Johnson     // definition and importing would change the order they are seen by the
151488a800aSTeresa Johnson     // linker. The module linking caller needs to enforce this.
152488a800aSTeresa Johnson     assert(!doImportAsDefinition(SGV));
153488a800aSTeresa Johnson     // If imported as a declaration, it becomes external_weak.
154bb3a1d92SMehdi Amini     return SGV->getLinkage();
155488a800aSTeresa Johnson 
156488a800aSTeresa Johnson   case GlobalValue::WeakODRLinkage:
157488a800aSTeresa Johnson     // For weak_odr linkage, there is a guarantee that all copies will be
158488a800aSTeresa Johnson     // equivalent, so the issue described above for weak_any does not exist,
159488a800aSTeresa Johnson     // and the definition can be imported. It can be treated similarly
160488a800aSTeresa Johnson     // to an imported externally visible global value.
161488a800aSTeresa Johnson     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
162488a800aSTeresa Johnson       return GlobalValue::AvailableExternallyLinkage;
163488a800aSTeresa Johnson     else
164488a800aSTeresa Johnson       return GlobalValue::ExternalLinkage;
165488a800aSTeresa Johnson 
166488a800aSTeresa Johnson   case GlobalValue::AppendingLinkage:
167488a800aSTeresa Johnson     // It would be incorrect to import an appending linkage variable,
168488a800aSTeresa Johnson     // since it would cause global constructors/destructors to be
169488a800aSTeresa Johnson     // executed multiple times. This should have already been handled
170488a800aSTeresa Johnson     // by linkIfNeeded, and we will assert in shouldLinkFromSource
171488a800aSTeresa Johnson     // if we try to import, so we simply return AppendingLinkage.
172488a800aSTeresa Johnson     return GlobalValue::AppendingLinkage;
173488a800aSTeresa Johnson 
174488a800aSTeresa Johnson   case GlobalValue::InternalLinkage:
175488a800aSTeresa Johnson   case GlobalValue::PrivateLinkage:
176488a800aSTeresa Johnson     // If we are promoting the local to global scope, it is handled
177488a800aSTeresa Johnson     // similarly to a normal externally visible global.
1781b9c2be8STeresa Johnson     if (DoPromote) {
179488a800aSTeresa Johnson       if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
180488a800aSTeresa Johnson         return GlobalValue::AvailableExternallyLinkage;
181488a800aSTeresa Johnson       else
182488a800aSTeresa Johnson         return GlobalValue::ExternalLinkage;
183488a800aSTeresa Johnson     }
184488a800aSTeresa Johnson     // A non-promoted imported local definition stays local.
185488a800aSTeresa Johnson     // The ThinLTO pass will eventually force-import their definitions.
186488a800aSTeresa Johnson     return SGV->getLinkage();
187488a800aSTeresa Johnson 
188488a800aSTeresa Johnson   case GlobalValue::ExternalWeakLinkage:
189488a800aSTeresa Johnson     // External weak doesn't apply to definitions, must be a declaration.
190488a800aSTeresa Johnson     assert(!doImportAsDefinition(SGV));
191488a800aSTeresa Johnson     // Linkage stays external_weak.
192488a800aSTeresa Johnson     return SGV->getLinkage();
193488a800aSTeresa Johnson 
194488a800aSTeresa Johnson   case GlobalValue::CommonLinkage:
195488a800aSTeresa Johnson     // Linkage stays common on definitions.
196488a800aSTeresa Johnson     // The ThinLTO pass will eventually force-import their definitions.
197488a800aSTeresa Johnson     return SGV->getLinkage();
198488a800aSTeresa Johnson   }
199488a800aSTeresa Johnson 
200488a800aSTeresa Johnson   llvm_unreachable("unknown linkage type");
201488a800aSTeresa Johnson }
202488a800aSTeresa Johnson 
203488a800aSTeresa Johnson void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
2044595a915SSean Fertile 
2054595a915SSean Fertile   // Check the summaries to see if the symbol gets resolved to a known local
2064595a915SSean Fertile   // definition.
207*bf46e741SEugene Leviant   ValueInfo VI;
2084595a915SSean Fertile   if (GV.hasName()) {
209*bf46e741SEugene Leviant     VI = ImportIndex.getValueInfo(GV.getGUID());
210f5220fb6SRafael Espindola     if (VI && VI.isDSOLocal()) {
2114595a915SSean Fertile       GV.setDSOLocal(true);
212f5220fb6SRafael Espindola       if (GV.hasDLLImportStorageClass())
213f5220fb6SRafael Espindola         GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
214f5220fb6SRafael Espindola     }
2154595a915SSean Fertile   }
2164595a915SSean Fertile 
217*bf46e741SEugene Leviant   // Mark read-only variables which can be imported with specific attribute.
218*bf46e741SEugene Leviant   // We can't internalize them now because IRMover will fail to link variable
219*bf46e741SEugene Leviant   // definitions to their external declarations during ThinLTO import. We'll
220*bf46e741SEugene Leviant   // internalize read-only variables later, after import is finished.
221*bf46e741SEugene Leviant   // See internalizeImmutableGVs.
222*bf46e741SEugene Leviant   //
223*bf46e741SEugene Leviant   // If global value dead stripping is not enabled in summary then
224*bf46e741SEugene Leviant   // propagateConstants hasn't been run. We can't internalize GV
225*bf46e741SEugene Leviant   // in such case.
226*bf46e741SEugene Leviant   if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) {
227*bf46e741SEugene Leviant     const auto &SL = VI.getSummaryList();
228*bf46e741SEugene Leviant     auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get());
229*bf46e741SEugene Leviant     if (GVS && GVS->isReadOnly())
230*bf46e741SEugene Leviant       cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
231*bf46e741SEugene Leviant   }
232*bf46e741SEugene Leviant 
2331b9c2be8STeresa Johnson   bool DoPromote = false;
234488a800aSTeresa Johnson   if (GV.hasLocalLinkage() &&
23538d4df71STeresa Johnson       ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
2361b9c2be8STeresa Johnson     // Once we change the name or linkage it is difficult to determine
23738d4df71STeresa Johnson     // again whether we should promote since shouldPromoteLocalToGlobal needs
2381b9c2be8STeresa Johnson     // to locate the summary (based on GUID from name and linkage). Therefore,
2391b9c2be8STeresa Johnson     // use DoPromote result saved above.
2401b9c2be8STeresa Johnson     GV.setName(getName(&GV, DoPromote));
2411b9c2be8STeresa Johnson     GV.setLinkage(getLinkage(&GV, DoPromote));
242488a800aSTeresa Johnson     if (!GV.hasLocalLinkage())
243488a800aSTeresa Johnson       GV.setVisibility(GlobalValue::HiddenVisibility);
244488a800aSTeresa Johnson   } else
2451b9c2be8STeresa Johnson     GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
246488a800aSTeresa Johnson 
247488a800aSTeresa Johnson   // Remove functions imported as available externally defs from comdats,
248488a800aSTeresa Johnson   // as this is a declaration for the linker, and will be dropped eventually.
249488a800aSTeresa Johnson   // It is illegal for comdats to contain declarations.
250*bf46e741SEugene Leviant   auto *GO = dyn_cast<GlobalObject>(&GV);
251488a800aSTeresa Johnson   if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
252488a800aSTeresa Johnson     // The IRMover should not have placed any imported declarations in
253488a800aSTeresa Johnson     // a comdat, so the only declaration that should be in a comdat
254488a800aSTeresa Johnson     // at this point would be a definition imported as available_externally.
255488a800aSTeresa Johnson     assert(GO->hasAvailableExternallyLinkage() &&
256488a800aSTeresa Johnson            "Expected comdat on definition (possibly available external)");
257488a800aSTeresa Johnson     GO->setComdat(nullptr);
258488a800aSTeresa Johnson   }
259488a800aSTeresa Johnson }
260488a800aSTeresa Johnson 
261488a800aSTeresa Johnson void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
262488a800aSTeresa Johnson   for (GlobalVariable &GV : M.globals())
263488a800aSTeresa Johnson     processGlobalForThinLTO(GV);
264488a800aSTeresa Johnson   for (Function &SF : M)
265488a800aSTeresa Johnson     processGlobalForThinLTO(SF);
266488a800aSTeresa Johnson   for (GlobalAlias &GA : M.aliases())
267488a800aSTeresa Johnson     processGlobalForThinLTO(GA);
268488a800aSTeresa Johnson }
269488a800aSTeresa Johnson 
270488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::run() {
271488a800aSTeresa Johnson   processGlobalsForThinLTO();
272488a800aSTeresa Johnson   return false;
273488a800aSTeresa Johnson }
274488a800aSTeresa Johnson 
2756d8f817fSPeter Collingbourne bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
2766d8f817fSPeter Collingbourne                                   SetVector<GlobalValue *> *GlobalsToImport) {
2778d05185aSMehdi Amini   FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
278488a800aSTeresa Johnson   return ThinLTOProcessing.run();
279488a800aSTeresa Johnson }
280