blob: d1710feba9d8c09059fe73cdc748874e3f39ca22 [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).
75 auto Summaries = ImportIndex.findGlobalValueSummaryList(SGV->getGUID());
76 if (Summaries == ImportIndex.end())
77 // Assert that this is an import - we should always have access to the
78 // summary when exporting.
79 assert(isPerformingImport() &&
80 "Missing summary for global value when exporting");
81 else {
82 assert(Summaries->second.size() == 1 && "Local has more than one summary");
83 if (Summaries->second.front()->noRename()) {
84 assert((isModuleExporting() || !GlobalsToImport->count(SGV)) &&
85 "Imported a non-renamable local value");
86 return false;
87 }
88 }
Mehdi Aminib4e1e822016-04-27 00:32:13 +000089
Teresa Johnson488a8002016-02-10 18:11:31 +000090 // Eventually we only need to promote functions in the exporting module that
91 // are referenced by a potentially exported function (i.e. one that is in the
Teresa Johnson26ab5772016-03-15 00:04:37 +000092 // summary index).
Teresa Johnson488a8002016-02-10 18:11:31 +000093 return true;
94}
95
Teresa Johnson1b9c2be2016-10-29 21:31:48 +000096std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
97 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +000098 // For locals that must be promoted to global scope, ensure that
99 // the promoted name uniquely identifies the copy in the original module,
100 // using the ID assigned during combined index creation. When importing,
101 // we rename all locals (not just those that are promoted) in order to
102 // avoid naming conflicts between locals imported from different modules.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000103 if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
Teresa Johnson26ab5772016-03-15 00:04:37 +0000104 return ModuleSummaryIndex::getGlobalNameForLocal(
Teresa Johnson488a8002016-02-10 18:11:31 +0000105 SGV->getName(),
Mehdi Aminiae280e52016-04-11 23:26:46 +0000106 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
Teresa Johnson488a8002016-02-10 18:11:31 +0000107 return SGV->getName();
108}
109
110GlobalValue::LinkageTypes
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000111FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
112 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000113 // Any local variable that is referenced by an exported function needs
114 // to be promoted to global scope. Since we don't currently know which
115 // functions reference which local variables/functions, we must treat
116 // all as potentially exported if this module is exporting anything.
117 if (isModuleExporting()) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000118 if (SGV->hasLocalLinkage() && DoPromote)
Teresa Johnson488a8002016-02-10 18:11:31 +0000119 return GlobalValue::ExternalLinkage;
120 return SGV->getLinkage();
121 }
122
123 // Otherwise, if we aren't importing, no linkage change is needed.
124 if (!isPerformingImport())
125 return SGV->getLinkage();
126
127 switch (SGV->getLinkage()) {
128 case GlobalValue::ExternalLinkage:
129 // External defnitions are converted to available_externally
130 // definitions upon import, so that they are available for inlining
131 // and/or optimization, but are turned into declarations later
132 // during the EliminateAvailableExternally pass.
133 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
134 return GlobalValue::AvailableExternallyLinkage;
135 // An imported external declaration stays external.
136 return SGV->getLinkage();
137
138 case GlobalValue::AvailableExternallyLinkage:
139 // An imported available_externally definition converts
140 // to external if imported as a declaration.
141 if (!doImportAsDefinition(SGV))
142 return GlobalValue::ExternalLinkage;
143 // An imported available_externally declaration stays that way.
144 return SGV->getLinkage();
145
146 case GlobalValue::LinkOnceAnyLinkage:
147 case GlobalValue::LinkOnceODRLinkage:
148 // These both stay the same when importing the definition.
149 // The ThinLTO pass will eventually force-import their definitions.
150 return SGV->getLinkage();
151
152 case GlobalValue::WeakAnyLinkage:
153 // Can't import weak_any definitions correctly, or we might change the
154 // program semantics, since the linker will pick the first weak_any
155 // definition and importing would change the order they are seen by the
156 // linker. The module linking caller needs to enforce this.
157 assert(!doImportAsDefinition(SGV));
158 // If imported as a declaration, it becomes external_weak.
Mehdi Aminibb3a1d92016-04-20 04:18:11 +0000159 return SGV->getLinkage();
Teresa Johnson488a8002016-02-10 18:11:31 +0000160
161 case GlobalValue::WeakODRLinkage:
162 // For weak_odr linkage, there is a guarantee that all copies will be
163 // equivalent, so the issue described above for weak_any does not exist,
164 // and the definition can be imported. It can be treated similarly
165 // to an imported externally visible global value.
166 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
167 return GlobalValue::AvailableExternallyLinkage;
168 else
169 return GlobalValue::ExternalLinkage;
170
171 case GlobalValue::AppendingLinkage:
172 // It would be incorrect to import an appending linkage variable,
173 // since it would cause global constructors/destructors to be
174 // executed multiple times. This should have already been handled
175 // by linkIfNeeded, and we will assert in shouldLinkFromSource
176 // if we try to import, so we simply return AppendingLinkage.
177 return GlobalValue::AppendingLinkage;
178
179 case GlobalValue::InternalLinkage:
180 case GlobalValue::PrivateLinkage:
181 // If we are promoting the local to global scope, it is handled
182 // similarly to a normal externally visible global.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000183 if (DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000184 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
185 return GlobalValue::AvailableExternallyLinkage;
186 else
187 return GlobalValue::ExternalLinkage;
188 }
189 // A non-promoted imported local definition stays local.
190 // The ThinLTO pass will eventually force-import their definitions.
191 return SGV->getLinkage();
192
193 case GlobalValue::ExternalWeakLinkage:
194 // External weak doesn't apply to definitions, must be a declaration.
195 assert(!doImportAsDefinition(SGV));
196 // Linkage stays external_weak.
197 return SGV->getLinkage();
198
199 case GlobalValue::CommonLinkage:
200 // Linkage stays common on definitions.
201 // The ThinLTO pass will eventually force-import their definitions.
202 return SGV->getLinkage();
203 }
204
205 llvm_unreachable("unknown linkage type");
206}
207
208void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000209 bool DoPromote = false;
Teresa Johnson488a8002016-02-10 18:11:31 +0000210 if (GV.hasLocalLinkage() &&
Teresa Johnson38d4df72016-10-29 21:52:23 +0000211 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000212 // Once we change the name or linkage it is difficult to determine
Teresa Johnson38d4df72016-10-29 21:52:23 +0000213 // again whether we should promote since shouldPromoteLocalToGlobal needs
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000214 // to locate the summary (based on GUID from name and linkage). Therefore,
215 // use DoPromote result saved above.
216 GV.setName(getName(&GV, DoPromote));
217 GV.setLinkage(getLinkage(&GV, DoPromote));
Teresa Johnson488a8002016-02-10 18:11:31 +0000218 if (!GV.hasLocalLinkage())
219 GV.setVisibility(GlobalValue::HiddenVisibility);
Teresa Johnson488a8002016-02-10 18:11:31 +0000220 } else
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000221 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
Teresa Johnson488a8002016-02-10 18:11:31 +0000222
223 // Remove functions imported as available externally defs from comdats,
224 // as this is a declaration for the linker, and will be dropped eventually.
225 // It is illegal for comdats to contain declarations.
226 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
227 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
228 // The IRMover should not have placed any imported declarations in
229 // a comdat, so the only declaration that should be in a comdat
230 // at this point would be a definition imported as available_externally.
231 assert(GO->hasAvailableExternallyLinkage() &&
232 "Expected comdat on definition (possibly available external)");
233 GO->setComdat(nullptr);
234 }
235}
236
237void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
238 for (GlobalVariable &GV : M.globals())
239 processGlobalForThinLTO(GV);
240 for (Function &SF : M)
241 processGlobalForThinLTO(SF);
242 for (GlobalAlias &GA : M.aliases())
243 processGlobalForThinLTO(GA);
244}
245
246bool FunctionImportGlobalProcessing::run() {
247 processGlobalsForThinLTO();
248 return false;
249}
250
Mehdi Amini8d051852016-03-19 00:40:31 +0000251bool llvm::renameModuleForThinLTO(
252 Module &M, const ModuleSummaryIndex &Index,
253 DenseSet<const GlobalValue *> *GlobalsToImport) {
254 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +0000255 return ThinLTOProcessing.run();
256}