]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp
MFV r309587:
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Utils / FunctionImportUtils.cpp
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::doPromoteLocalToGlobal(
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 (GVar && GVar->hasSection())
71     // Some sections like "__DATA,__cfstring" are "magic" and promotion is not
72     // allowed. Just disable promotion on any GVar with sections right now.
73     return false;
74
75   // Eventually we only need to promote functions in the exporting module that
76   // are referenced by a potentially exported function (i.e. one that is in the
77   // summary index).
78   return true;
79 }
80
81 std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV) {
82   // For locals that must be promoted to global scope, ensure that
83   // the promoted name uniquely identifies the copy in the original module,
84   // using the ID assigned during combined index creation. When importing,
85   // we rename all locals (not just those that are promoted) in order to
86   // avoid naming conflicts between locals imported from different modules.
87   if (SGV->hasLocalLinkage() &&
88       (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
89     return ModuleSummaryIndex::getGlobalNameForLocal(
90         SGV->getName(),
91         ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
92   return SGV->getName();
93 }
94
95 GlobalValue::LinkageTypes
96 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV) {
97   // Any local variable that is referenced by an exported function needs
98   // to be promoted to global scope. Since we don't currently know which
99   // functions reference which local variables/functions, we must treat
100   // all as potentially exported if this module is exporting anything.
101   if (isModuleExporting()) {
102     if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
103       return GlobalValue::ExternalLinkage;
104     return SGV->getLinkage();
105   }
106
107   // Otherwise, if we aren't importing, no linkage change is needed.
108   if (!isPerformingImport())
109     return SGV->getLinkage();
110
111   switch (SGV->getLinkage()) {
112   case GlobalValue::ExternalLinkage:
113     // External defnitions are converted to available_externally
114     // definitions upon import, so that they are available for inlining
115     // and/or optimization, but are turned into declarations later
116     // during the EliminateAvailableExternally pass.
117     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
118       return GlobalValue::AvailableExternallyLinkage;
119     // An imported external declaration stays external.
120     return SGV->getLinkage();
121
122   case GlobalValue::AvailableExternallyLinkage:
123     // An imported available_externally definition converts
124     // to external if imported as a declaration.
125     if (!doImportAsDefinition(SGV))
126       return GlobalValue::ExternalLinkage;
127     // An imported available_externally declaration stays that way.
128     return SGV->getLinkage();
129
130   case GlobalValue::LinkOnceAnyLinkage:
131   case GlobalValue::LinkOnceODRLinkage:
132     // These both stay the same when importing the definition.
133     // The ThinLTO pass will eventually force-import their definitions.
134     return SGV->getLinkage();
135
136   case GlobalValue::WeakAnyLinkage:
137     // Can't import weak_any definitions correctly, or we might change the
138     // program semantics, since the linker will pick the first weak_any
139     // definition and importing would change the order they are seen by the
140     // linker. The module linking caller needs to enforce this.
141     assert(!doImportAsDefinition(SGV));
142     // If imported as a declaration, it becomes external_weak.
143     return SGV->getLinkage();
144
145   case GlobalValue::WeakODRLinkage:
146     // For weak_odr linkage, there is a guarantee that all copies will be
147     // equivalent, so the issue described above for weak_any does not exist,
148     // and the definition can be imported. It can be treated similarly
149     // to an imported externally visible global value.
150     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
151       return GlobalValue::AvailableExternallyLinkage;
152     else
153       return GlobalValue::ExternalLinkage;
154
155   case GlobalValue::AppendingLinkage:
156     // It would be incorrect to import an appending linkage variable,
157     // since it would cause global constructors/destructors to be
158     // executed multiple times. This should have already been handled
159     // by linkIfNeeded, and we will assert in shouldLinkFromSource
160     // if we try to import, so we simply return AppendingLinkage.
161     return GlobalValue::AppendingLinkage;
162
163   case GlobalValue::InternalLinkage:
164   case GlobalValue::PrivateLinkage:
165     // If we are promoting the local to global scope, it is handled
166     // similarly to a normal externally visible global.
167     if (doPromoteLocalToGlobal(SGV)) {
168       if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
169         return GlobalValue::AvailableExternallyLinkage;
170       else
171         return GlobalValue::ExternalLinkage;
172     }
173     // A non-promoted imported local definition stays local.
174     // The ThinLTO pass will eventually force-import their definitions.
175     return SGV->getLinkage();
176
177   case GlobalValue::ExternalWeakLinkage:
178     // External weak doesn't apply to definitions, must be a declaration.
179     assert(!doImportAsDefinition(SGV));
180     // Linkage stays external_weak.
181     return SGV->getLinkage();
182
183   case GlobalValue::CommonLinkage:
184     // Linkage stays common on definitions.
185     // The ThinLTO pass will eventually force-import their definitions.
186     return SGV->getLinkage();
187   }
188
189   llvm_unreachable("unknown linkage type");
190 }
191
192 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
193   if (GV.hasLocalLinkage() &&
194       (doPromoteLocalToGlobal(&GV) || isPerformingImport())) {
195     GV.setName(getName(&GV));
196     GV.setLinkage(getLinkage(&GV));
197     if (!GV.hasLocalLinkage())
198       GV.setVisibility(GlobalValue::HiddenVisibility);
199   } else
200     GV.setLinkage(getLinkage(&GV));
201
202   // Remove functions imported as available externally defs from comdats,
203   // as this is a declaration for the linker, and will be dropped eventually.
204   // It is illegal for comdats to contain declarations.
205   auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
206   if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
207     // The IRMover should not have placed any imported declarations in
208     // a comdat, so the only declaration that should be in a comdat
209     // at this point would be a definition imported as available_externally.
210     assert(GO->hasAvailableExternallyLinkage() &&
211            "Expected comdat on definition (possibly available external)");
212     GO->setComdat(nullptr);
213   }
214 }
215
216 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
217   if (!moduleCanBeRenamedForThinLTO(M)) {
218     // We would have blocked importing from this module by suppressing index
219     // generation. We still may be able to import into this module though.
220     assert(!isPerformingImport() &&
221            "Should have blocked importing from module with local used in ASM");
222     return;
223   }
224
225   for (GlobalVariable &GV : M.globals())
226     processGlobalForThinLTO(GV);
227   for (Function &SF : M)
228     processGlobalForThinLTO(SF);
229   for (GlobalAlias &GA : M.aliases())
230     processGlobalForThinLTO(GA);
231 }
232
233 bool FunctionImportGlobalProcessing::run() {
234   processGlobalsForThinLTO();
235   return false;
236 }
237
238 bool llvm::renameModuleForThinLTO(
239     Module &M, const ModuleSummaryIndex &Index,
240     DenseSet<const GlobalValue *> *GlobalsToImport) {
241   FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
242   return ThinLTOProcessing.run();
243 }