1 //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the FunctionImportGlobalProcessing class, used
11 // to perform the necessary global value handling for function importing.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
16 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
17 #include "llvm/IR/InstIterator.h"
18 #include "llvm/IR/Instructions.h"
19 using namespace llvm;
20 
21 /// Checks if we should import SGV as a definition, otherwise import as a
22 /// declaration.
23 bool FunctionImportGlobalProcessing::doImportAsDefinition(
24     const GlobalValue *SGV, DenseSet<const GlobalValue *> *GlobalsToImport) {
25 
26   // For alias, we tie the definition to the base object. Extract it and recurse
27   if (auto *GA = dyn_cast<GlobalAlias>(SGV)) {
28     if (GA->hasWeakAnyLinkage())
29       return false;
30     const GlobalObject *GO = GA->getBaseObject();
31     if (!GO->hasLinkOnceODRLinkage())
32       return false;
33     return FunctionImportGlobalProcessing::doImportAsDefinition(
34         GO, GlobalsToImport);
35   }
36   // Only import the globals requested for importing.
37   if (GlobalsToImport->count(SGV))
38     return true;
39   // Otherwise no.
40   return false;
41 }
42 
43 bool FunctionImportGlobalProcessing::doImportAsDefinition(
44     const GlobalValue *SGV) {
45   if (!isPerformingImport())
46     return false;
47   return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
48                                                               GlobalsToImport);
49 }
50 
51 bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
52     const GlobalValue *SGV) {
53   assert(SGV->hasLocalLinkage());
54   // Both the imported references and the original local variable must
55   // be promoted.
56   if (!isPerformingImport() && !isModuleExporting())
57     return false;
58 
59   // Local const variables never need to be promoted unless they are address
60   // taken. The imported uses can simply use the clone created in this module.
61   // For now we are conservative in determining which variables are not
62   // address taken by checking the unnamed addr flag. To be more aggressive,
63   // the address taken information must be checked earlier during parsing
64   // of the module and recorded in the summary index for use when importing
65   // from that module.
66   auto *GVar = dyn_cast<GlobalVariable>(SGV);
67   if (GVar && GVar->isConstant() && GVar->hasGlobalUnnamedAddr())
68     return false;
69 
70   // If we are exporting, we need to see whether this value is marked
71   // as NoRename in the summary. If we are importing, we may not have
72   // a summary in the distributed backend case (only summaries for values
73   // importes as defs, not references, are included in the index passed
74   // to the distributed backends).
75   if (isPerformingImport()) {
76     // We don't know for sure yet if we are importing this value (as either
77     // a reference or a def), since we are simply walking all values in the
78     // module. But by necessity if we end up importing it and it is local,
79     // it must be promoted, so unconditionally promote all values in the
80     // importing module.
81     return true;
82   }
83 
84   // When exporting, consult the index.
85   auto Summaries = ImportIndex.findGlobalValueSummaryList(SGV->getGUID());
86   assert(Summaries != ImportIndex.end() &&
87          "Missing summary for global value when exporting");
88   assert(Summaries->second.size() == 1 && "Local has more than one summary");
89   auto Linkage = Summaries->second.front()->linkage();
90   if (!GlobalValue::isLocalLinkage(Linkage)) {
91     assert(!Summaries->second.front()->noRename());
92     return true;
93   }
94 
95   return false;
96 }
97 
98 std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
99                                                     bool DoPromote) {
100   // For locals that must be promoted to global scope, ensure that
101   // the promoted name uniquely identifies the copy in the original module,
102   // using the ID assigned during combined index creation. When importing,
103   // we rename all locals (not just those that are promoted) in order to
104   // avoid naming conflicts between locals imported from different modules.
105   if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
106     return ModuleSummaryIndex::getGlobalNameForLocal(
107         SGV->getName(),
108         ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
109   return SGV->getName();
110 }
111 
112 GlobalValue::LinkageTypes
113 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
114                                            bool DoPromote) {
115   // Any local variable that is referenced by an exported function needs
116   // to be promoted to global scope. Since we don't currently know which
117   // functions reference which local variables/functions, we must treat
118   // all as potentially exported if this module is exporting anything.
119   if (isModuleExporting()) {
120     if (SGV->hasLocalLinkage() && DoPromote)
121       return GlobalValue::ExternalLinkage;
122     return SGV->getLinkage();
123   }
124 
125   // Otherwise, if we aren't importing, no linkage change is needed.
126   if (!isPerformingImport())
127     return SGV->getLinkage();
128 
129   switch (SGV->getLinkage()) {
130   case GlobalValue::ExternalLinkage:
131     // External defnitions are converted to available_externally
132     // definitions upon import, so that they are available for inlining
133     // and/or optimization, but are turned into declarations later
134     // during the EliminateAvailableExternally pass.
135     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
136       return GlobalValue::AvailableExternallyLinkage;
137     // An imported external declaration stays external.
138     return SGV->getLinkage();
139 
140   case GlobalValue::AvailableExternallyLinkage:
141     // An imported available_externally definition converts
142     // to external if imported as a declaration.
143     if (!doImportAsDefinition(SGV))
144       return GlobalValue::ExternalLinkage;
145     // An imported available_externally declaration stays that way.
146     return SGV->getLinkage();
147 
148   case GlobalValue::LinkOnceAnyLinkage:
149   case GlobalValue::LinkOnceODRLinkage:
150     // These both stay the same when importing the definition.
151     // The ThinLTO pass will eventually force-import their definitions.
152     return SGV->getLinkage();
153 
154   case GlobalValue::WeakAnyLinkage:
155     // Can't import weak_any definitions correctly, or we might change the
156     // program semantics, since the linker will pick the first weak_any
157     // definition and importing would change the order they are seen by the
158     // linker. The module linking caller needs to enforce this.
159     assert(!doImportAsDefinition(SGV));
160     // If imported as a declaration, it becomes external_weak.
161     return SGV->getLinkage();
162 
163   case GlobalValue::WeakODRLinkage:
164     // For weak_odr linkage, there is a guarantee that all copies will be
165     // equivalent, so the issue described above for weak_any does not exist,
166     // and the definition can be imported. It can be treated similarly
167     // to an imported externally visible global value.
168     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
169       return GlobalValue::AvailableExternallyLinkage;
170     else
171       return GlobalValue::ExternalLinkage;
172 
173   case GlobalValue::AppendingLinkage:
174     // It would be incorrect to import an appending linkage variable,
175     // since it would cause global constructors/destructors to be
176     // executed multiple times. This should have already been handled
177     // by linkIfNeeded, and we will assert in shouldLinkFromSource
178     // if we try to import, so we simply return AppendingLinkage.
179     return GlobalValue::AppendingLinkage;
180 
181   case GlobalValue::InternalLinkage:
182   case GlobalValue::PrivateLinkage:
183     // If we are promoting the local to global scope, it is handled
184     // similarly to a normal externally visible global.
185     if (DoPromote) {
186       if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
187         return GlobalValue::AvailableExternallyLinkage;
188       else
189         return GlobalValue::ExternalLinkage;
190     }
191     // A non-promoted imported local definition stays local.
192     // The ThinLTO pass will eventually force-import their definitions.
193     return SGV->getLinkage();
194 
195   case GlobalValue::ExternalWeakLinkage:
196     // External weak doesn't apply to definitions, must be a declaration.
197     assert(!doImportAsDefinition(SGV));
198     // Linkage stays external_weak.
199     return SGV->getLinkage();
200 
201   case GlobalValue::CommonLinkage:
202     // Linkage stays common on definitions.
203     // The ThinLTO pass will eventually force-import their definitions.
204     return SGV->getLinkage();
205   }
206 
207   llvm_unreachable("unknown linkage type");
208 }
209 
210 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
211   bool DoPromote = false;
212   if (GV.hasLocalLinkage() &&
213       ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
214     // Once we change the name or linkage it is difficult to determine
215     // again whether we should promote since shouldPromoteLocalToGlobal needs
216     // to locate the summary (based on GUID from name and linkage). Therefore,
217     // use DoPromote result saved above.
218     GV.setName(getName(&GV, DoPromote));
219     GV.setLinkage(getLinkage(&GV, DoPromote));
220     if (!GV.hasLocalLinkage())
221       GV.setVisibility(GlobalValue::HiddenVisibility);
222   } else
223     GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
224 
225   // Remove functions imported as available externally defs from comdats,
226   // as this is a declaration for the linker, and will be dropped eventually.
227   // It is illegal for comdats to contain declarations.
228   auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
229   if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
230     // The IRMover should not have placed any imported declarations in
231     // a comdat, so the only declaration that should be in a comdat
232     // at this point would be a definition imported as available_externally.
233     assert(GO->hasAvailableExternallyLinkage() &&
234            "Expected comdat on definition (possibly available external)");
235     GO->setComdat(nullptr);
236   }
237 }
238 
239 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
240   for (GlobalVariable &GV : M.globals())
241     processGlobalForThinLTO(GV);
242   for (Function &SF : M)
243     processGlobalForThinLTO(SF);
244   for (GlobalAlias &GA : M.aliases())
245     processGlobalForThinLTO(GA);
246 }
247 
248 bool FunctionImportGlobalProcessing::run() {
249   processGlobalsForThinLTO();
250   return false;
251 }
252 
253 bool llvm::renameModuleForThinLTO(
254     Module &M, const ModuleSummaryIndex &Index,
255     DenseSet<const GlobalValue *> *GlobalsToImport) {
256   FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
257   return ThinLTOProcessing.run();
258 }
259