1 //===- FunctionImportUtils.h - Importing support utilities -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the FunctionImportGlobalProcessing class which is used
11 // to perform the necessary global value handling for function importing.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
16 #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/IR/ModuleSummaryIndex.h"
24 /// Class to handle necessary GlobalValue changes required by ThinLTO
25 /// function importing, including linkage changes and any necessary renaming.
26 class FunctionImportGlobalProcessing {
27 /// The Module which we are exporting or importing functions from.
30 /// Module summary index passed in for function importing/exporting handling.
31 const ModuleSummaryIndex &ImportIndex;
33 /// Globals to import from this module, all other functions will be
34 /// imported as declarations instead of definitions.
35 SetVector<GlobalValue *> *GlobalsToImport;
37 /// Set to true if the given ModuleSummaryIndex contains any functions
38 /// from this source module, in which case we must conservatively assume
39 /// that any of its functions may be imported into another module
40 /// as part of a different backend compilation process.
41 bool HasExportedFunctions = false;
43 /// Set of llvm.*used values, in order to validate that we don't try
44 /// to promote any non-renamable values.
45 SmallPtrSet<GlobalValue *, 8> Used;
47 /// Keep track of any COMDATs that require renaming (because COMDAT
48 /// leader was promoted and renamed). Maps from original COMDAT to one
50 DenseMap<const Comdat *, Comdat *> RenamedComdats;
52 /// Check if we should promote the given local value to global scope.
53 bool shouldPromoteLocalToGlobal(const GlobalValue *SGV);
56 /// Check if the given value is a local that can't be renamed (promoted).
57 /// Only used in assertion checking, and disabled under NDEBUG since the Used
58 /// set will not be populated.
59 bool isNonRenamableLocal(const GlobalValue &GV) const;
62 /// Helper methods to check if we are importing from or potentially
63 /// exporting from the current source module.
64 bool isPerformingImport() const { return GlobalsToImport != nullptr; }
65 bool isModuleExporting() const { return HasExportedFunctions; }
67 /// If we are importing from the source module, checks if we should
68 /// import SGV as a definition, otherwise import as a declaration.
69 bool doImportAsDefinition(const GlobalValue *SGV);
71 /// Get the name for SGV that should be used in the linked destination
72 /// module. Specifically, this handles the case where we need to rename
73 /// a local that is being promoted to global scope, which it will always
74 /// do when \p DoPromote is true (or when importing a local).
75 std::string getName(const GlobalValue *SGV, bool DoPromote);
77 /// Process globals so that they can be used in ThinLTO. This includes
78 /// promoting local variables so that they can be reference externally by
79 /// thin lto imported globals and converting strong external globals to
80 /// available_externally.
81 void processGlobalsForThinLTO();
82 void processGlobalForThinLTO(GlobalValue &GV);
84 /// Get the new linkage for SGV that should be used in the linked destination
85 /// module. Specifically, for ThinLTO importing or exporting it may need
86 /// to be adjusted. When \p DoPromote is true then we must adjust the
87 /// linkage for a required promotion of a local to global scope.
88 GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
91 FunctionImportGlobalProcessing(
92 Module &M, const ModuleSummaryIndex &Index,
93 SetVector<GlobalValue *> *GlobalsToImport = nullptr)
94 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport) {
95 // If we have a ModuleSummaryIndex but no function to import,
96 // then this is the primary module being compiled in a ThinLTO
97 // backend compilation, and we need to see if it has functions that
98 // may be exported to another backend compilation.
100 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
103 // First collect those in the llvm.used set.
104 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
105 // Next collect those in the llvm.compiler.used set.
106 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
112 static bool doImportAsDefinition(const GlobalValue *SGV,
113 SetVector<GlobalValue *> *GlobalsToImport);
116 /// Perform in-place global value handling on the given Module for
117 /// exported local functions renamed and promoted for ThinLTO.
118 bool renameModuleForThinLTO(
119 Module &M, const ModuleSummaryIndex &Index,
120 SetVector<GlobalValue *> *GlobalsToImport = nullptr);
122 /// Compute synthetic function entry counts.
123 void computeSyntheticCounts(ModuleSummaryIndex &Index);
125 } // End llvm namespace