blob: bc7672f91bfb24a1c067c94eb00a76dfe17dee3a [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(
Mehdi Amini8d051852016-03-19 00:40:31 +000024 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)) {
Teresa Johnson488a8002016-02-10 18:11:31 +000028 if (GA->hasWeakAnyLinkage())
29 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.
37 if (GlobalsToImport->count(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
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
Teresa Johnson26ab5772016-03-15 00:04:37 +000064 // of the module and recorded in the summary index for use when importing
Teresa Johnson488a8002016-02-10 18:11:31 +000065 // from that module.
66 auto *GVar = dyn_cast<GlobalVariable>(SGV);
Peter Collingbourne96efdd62016-06-14 21:01:22 +000067 if (GVar && GVar->isConstant() && GVar->hasGlobalUnnamedAddr())
Teresa Johnson488a8002016-02-10 18:11:31 +000068 return false;
69
Teresa Johnson0515fb82016-11-03 01:07:16 +000070 // 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).
Teresa Johnson4fef68c2016-11-14 19:21:41 +000075 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;
Teresa Johnson0515fb82016-11-03 01:07:16 +000082 }
Mehdi Aminib4e1e822016-04-27 00:32:13 +000083
Teresa Johnson4fef68c2016-11-14 19:21:41 +000084 // 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;
Teresa Johnson488a8002016-02-10 18:11:31 +000096}
97
Teresa Johnson1b9c2be2016-10-29 21:31:48 +000098std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
99 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000100 // 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.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000105 if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
Teresa Johnson26ab5772016-03-15 00:04:37 +0000106 return ModuleSummaryIndex::getGlobalNameForLocal(
Teresa Johnson488a8002016-02-10 18:11:31 +0000107 SGV->getName(),
Mehdi Aminiae280e52016-04-11 23:26:46 +0000108 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
Teresa Johnson488a8002016-02-10 18:11:31 +0000109 return SGV->getName();
110}
111
112GlobalValue::LinkageTypes
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000113FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
114 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000115 // 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()) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000120 if (SGV->hasLocalLinkage() && DoPromote)
Teresa Johnson488a8002016-02-10 18:11:31 +0000121 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.
Mehdi Aminibb3a1d92016-04-20 04:18:11 +0000161 return SGV->getLinkage();
Teresa Johnson488a8002016-02-10 18:11:31 +0000162
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.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000185 if (DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000186 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
210void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000211 bool DoPromote = false;
Teresa Johnson488a8002016-02-10 18:11:31 +0000212 if (GV.hasLocalLinkage() &&
Teresa Johnson38d4df72016-10-29 21:52:23 +0000213 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000214 // Once we change the name or linkage it is difficult to determine
Teresa Johnson38d4df72016-10-29 21:52:23 +0000215 // again whether we should promote since shouldPromoteLocalToGlobal needs
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000216 // 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));
Teresa Johnson488a8002016-02-10 18:11:31 +0000220 if (!GV.hasLocalLinkage())
221 GV.setVisibility(GlobalValue::HiddenVisibility);
Teresa Johnson488a8002016-02-10 18:11:31 +0000222 } else
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000223 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
Teresa Johnson488a8002016-02-10 18:11:31 +0000224
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
239void 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
248bool FunctionImportGlobalProcessing::run() {
249 processGlobalsForThinLTO();
250 return false;
251}
252
Mehdi Amini8d051852016-03-19 00:40:31 +0000253bool llvm::renameModuleForThinLTO(
254 Module &M, const ModuleSummaryIndex &Index,
255 DenseSet<const GlobalValue *> *GlobalsToImport) {
256 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +0000257 return ThinLTOProcessing.run();
258}