blob: b00f4b14068a2f152f2e7b718439528d95678d57 [file] [log] [blame]
Teresa Johnson488a8002016-02-10 18:11:31 +00001//===- 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
Mehdi Amini3b132e32016-05-06 08:25:33 +000015#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Teresa Johnson488a8002016-02-10 18:11:31 +000016#include "llvm/Transforms/Utils/FunctionImportUtils.h"
Teresa Johnsondf5ef872016-04-27 14:19:38 +000017#include "llvm/IR/InstIterator.h"
18#include "llvm/IR/Instructions.h"
Teresa Johnson488a8002016-02-10 18:11:31 +000019using namespace llvm;
20
21/// Checks if we should import SGV as a definition, otherwise import as a
22/// declaration.
23bool FunctionImportGlobalProcessing::doImportAsDefinition(
Peter Collingbourne6d8f8172017-02-03 16:56:27 +000024 const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
Mehdi Amini8d051852016-03-19 00:40:31 +000025
26 // For alias, we tie the definition to the base object. Extract it and recurse
27 if (auto *GA = dyn_cast<GlobalAlias>(SGV)) {
Peter Collingbourne6d8f8172017-02-03 16:56:27 +000028 if (GA->isInterposable())
Teresa Johnson488a8002016-02-10 18:11:31 +000029 return false;
30 const GlobalObject *GO = GA->getBaseObject();
31 if (!GO->hasLinkOnceODRLinkage())
32 return false;
33 return FunctionImportGlobalProcessing::doImportAsDefinition(
Mehdi Amini8d051852016-03-19 00:40:31 +000034 GO, GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +000035 }
Mehdi Amini8d051852016-03-19 00:40:31 +000036 // Only import the globals requested for importing.
Peter Collingbourne6d8f8172017-02-03 16:56:27 +000037 if (GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
Teresa Johnson488a8002016-02-10 18:11:31 +000038 return true;
39 // Otherwise no.
40 return false;
41}
42
43bool FunctionImportGlobalProcessing::doImportAsDefinition(
44 const GlobalValue *SGV) {
45 if (!isPerformingImport())
46 return false;
Mehdi Amini8d051852016-03-19 00:40:31 +000047 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
48 GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +000049}
50
Teresa Johnson38d4df72016-10-29 21:52:23 +000051bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
Teresa Johnson488a8002016-02-10 18:11:31 +000052 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
Teresa Johnson4fef68c2016-11-14 19:21:41 +000059 if (isPerformingImport()) {
Peter Collingbourne6d8f8172017-02-03 16:56:27 +000060 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
61 !isNonRenamableLocal(*SGV)) &&
Teresa Johnson2b603842017-01-05 15:10:10 +000062 "Attempting to promote non-renamable local");
Teresa Johnson4fef68c2016-11-14 19:21:41 +000063 // We don't know for sure yet if we are importing this value (as either
64 // a reference or a def), since we are simply walking all values in the
65 // module. But by necessity if we end up importing it and it is local,
66 // it must be promoted, so unconditionally promote all values in the
67 // importing module.
68 return true;
Teresa Johnson0515fb82016-11-03 01:07:16 +000069 }
Mehdi Aminib4e1e822016-04-27 00:32:13 +000070
Teresa Johnson9006d522017-01-06 23:38:41 +000071 // When exporting, consult the index. We can have more than one local
72 // with the same GUID, in the case of same-named locals in different but
73 // same-named source files that were compiled in their respective directories
74 // (so the source file name and resulting GUID is the same). Find the one
75 // in this module.
76 auto Summary = ImportIndex.findSummaryInModule(
77 SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
78 assert(Summary && "Missing summary for global value when exporting");
79 auto Linkage = Summary->linkage();
Teresa Johnson4fef68c2016-11-14 19:21:41 +000080 if (!GlobalValue::isLocalLinkage(Linkage)) {
Teresa Johnson519465b2017-01-05 14:32:16 +000081 assert(!isNonRenamableLocal(*SGV) &&
82 "Attempting to promote non-renamable local");
Teresa Johnson4fef68c2016-11-14 19:21:41 +000083 return true;
84 }
85
86 return false;
Teresa Johnson488a8002016-02-10 18:11:31 +000087}
88
Teresa Johnson519465b2017-01-05 14:32:16 +000089#ifndef NDEBUG
90bool FunctionImportGlobalProcessing::isNonRenamableLocal(
91 const GlobalValue &GV) const {
92 if (!GV.hasLocalLinkage())
93 return false;
94 // This needs to stay in sync with the logic in buildModuleSummaryIndex.
95 if (GV.hasSection())
96 return true;
97 if (Used.count(const_cast<GlobalValue *>(&GV)))
98 return true;
99 return false;
100}
101#endif
102
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000103std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
104 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000105 // For locals that must be promoted to global scope, ensure that
106 // the promoted name uniquely identifies the copy in the original module,
107 // using the ID assigned during combined index creation. When importing,
108 // we rename all locals (not just those that are promoted) in order to
109 // avoid naming conflicts between locals imported from different modules.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000110 if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
Teresa Johnson26ab5772016-03-15 00:04:37 +0000111 return ModuleSummaryIndex::getGlobalNameForLocal(
Teresa Johnson488a8002016-02-10 18:11:31 +0000112 SGV->getName(),
Mehdi Aminiae280e52016-04-11 23:26:46 +0000113 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
Teresa Johnson488a8002016-02-10 18:11:31 +0000114 return SGV->getName();
115}
116
117GlobalValue::LinkageTypes
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000118FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
119 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000120 // Any local variable that is referenced by an exported function needs
121 // to be promoted to global scope. Since we don't currently know which
122 // functions reference which local variables/functions, we must treat
123 // all as potentially exported if this module is exporting anything.
124 if (isModuleExporting()) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000125 if (SGV->hasLocalLinkage() && DoPromote)
Teresa Johnson488a8002016-02-10 18:11:31 +0000126 return GlobalValue::ExternalLinkage;
127 return SGV->getLinkage();
128 }
129
130 // Otherwise, if we aren't importing, no linkage change is needed.
131 if (!isPerformingImport())
132 return SGV->getLinkage();
133
134 switch (SGV->getLinkage()) {
135 case GlobalValue::ExternalLinkage:
136 // External defnitions are converted to available_externally
137 // definitions upon import, so that they are available for inlining
138 // and/or optimization, but are turned into declarations later
139 // during the EliminateAvailableExternally pass.
140 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
141 return GlobalValue::AvailableExternallyLinkage;
142 // An imported external declaration stays external.
143 return SGV->getLinkage();
144
145 case GlobalValue::AvailableExternallyLinkage:
146 // An imported available_externally definition converts
147 // to external if imported as a declaration.
148 if (!doImportAsDefinition(SGV))
149 return GlobalValue::ExternalLinkage;
150 // An imported available_externally declaration stays that way.
151 return SGV->getLinkage();
152
153 case GlobalValue::LinkOnceAnyLinkage:
154 case GlobalValue::LinkOnceODRLinkage:
155 // These both stay the same when importing the definition.
156 // The ThinLTO pass will eventually force-import their definitions.
157 return SGV->getLinkage();
158
159 case GlobalValue::WeakAnyLinkage:
160 // Can't import weak_any definitions correctly, or we might change the
161 // program semantics, since the linker will pick the first weak_any
162 // definition and importing would change the order they are seen by the
163 // linker. The module linking caller needs to enforce this.
164 assert(!doImportAsDefinition(SGV));
165 // If imported as a declaration, it becomes external_weak.
Mehdi Aminibb3a1d92016-04-20 04:18:11 +0000166 return SGV->getLinkage();
Teresa Johnson488a8002016-02-10 18:11:31 +0000167
168 case GlobalValue::WeakODRLinkage:
169 // For weak_odr linkage, there is a guarantee that all copies will be
170 // equivalent, so the issue described above for weak_any does not exist,
171 // and the definition can be imported. It can be treated similarly
172 // to an imported externally visible global value.
173 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
174 return GlobalValue::AvailableExternallyLinkage;
175 else
176 return GlobalValue::ExternalLinkage;
177
178 case GlobalValue::AppendingLinkage:
179 // It would be incorrect to import an appending linkage variable,
180 // since it would cause global constructors/destructors to be
181 // executed multiple times. This should have already been handled
182 // by linkIfNeeded, and we will assert in shouldLinkFromSource
183 // if we try to import, so we simply return AppendingLinkage.
184 return GlobalValue::AppendingLinkage;
185
186 case GlobalValue::InternalLinkage:
187 case GlobalValue::PrivateLinkage:
188 // If we are promoting the local to global scope, it is handled
189 // similarly to a normal externally visible global.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000190 if (DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000191 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
192 return GlobalValue::AvailableExternallyLinkage;
193 else
194 return GlobalValue::ExternalLinkage;
195 }
196 // A non-promoted imported local definition stays local.
197 // The ThinLTO pass will eventually force-import their definitions.
198 return SGV->getLinkage();
199
200 case GlobalValue::ExternalWeakLinkage:
201 // External weak doesn't apply to definitions, must be a declaration.
202 assert(!doImportAsDefinition(SGV));
203 // Linkage stays external_weak.
204 return SGV->getLinkage();
205
206 case GlobalValue::CommonLinkage:
207 // Linkage stays common on definitions.
208 // The ThinLTO pass will eventually force-import their definitions.
209 return SGV->getLinkage();
210 }
211
212 llvm_unreachable("unknown linkage type");
213}
214
215void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000216 bool DoPromote = false;
Teresa Johnson488a8002016-02-10 18:11:31 +0000217 if (GV.hasLocalLinkage() &&
Teresa Johnson38d4df72016-10-29 21:52:23 +0000218 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000219 // Once we change the name or linkage it is difficult to determine
Teresa Johnson38d4df72016-10-29 21:52:23 +0000220 // again whether we should promote since shouldPromoteLocalToGlobal needs
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000221 // to locate the summary (based on GUID from name and linkage). Therefore,
222 // use DoPromote result saved above.
223 GV.setName(getName(&GV, DoPromote));
224 GV.setLinkage(getLinkage(&GV, DoPromote));
Teresa Johnson488a8002016-02-10 18:11:31 +0000225 if (!GV.hasLocalLinkage())
226 GV.setVisibility(GlobalValue::HiddenVisibility);
Teresa Johnson488a8002016-02-10 18:11:31 +0000227 } else
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000228 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
Teresa Johnson488a8002016-02-10 18:11:31 +0000229
230 // Remove functions imported as available externally defs from comdats,
231 // as this is a declaration for the linker, and will be dropped eventually.
232 // It is illegal for comdats to contain declarations.
233 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
234 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
235 // The IRMover should not have placed any imported declarations in
236 // a comdat, so the only declaration that should be in a comdat
237 // at this point would be a definition imported as available_externally.
238 assert(GO->hasAvailableExternallyLinkage() &&
239 "Expected comdat on definition (possibly available external)");
240 GO->setComdat(nullptr);
241 }
242}
243
244void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
245 for (GlobalVariable &GV : M.globals())
246 processGlobalForThinLTO(GV);
247 for (Function &SF : M)
248 processGlobalForThinLTO(SF);
249 for (GlobalAlias &GA : M.aliases())
250 processGlobalForThinLTO(GA);
251}
252
253bool FunctionImportGlobalProcessing::run() {
254 processGlobalsForThinLTO();
255 return false;
256}
257
Peter Collingbourne6d8f8172017-02-03 16:56:27 +0000258bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
259 SetVector<GlobalValue *> *GlobalsToImport) {
Mehdi Amini8d051852016-03-19 00:40:31 +0000260 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +0000261 return ThinLTOProcessing.run();
262}