1 //===- FunctionImportUtils.h - Importing support utilities -----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the FunctionImportGlobalProcessing class which is used
10 // to perform the necessary global value handling for function importing.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/IR/ModuleSummaryIndex.h"
23 /// Class to handle necessary GlobalValue changes required by ThinLTO
24 /// function importing, including linkage changes and any necessary renaming.
25 class FunctionImportGlobalProcessing {
26 /// The Module which we are exporting or importing functions from.
29 /// Module summary index passed in for function importing/exporting handling.
30 const ModuleSummaryIndex &ImportIndex;
32 /// Globals to import from this module, all other functions will be
33 /// imported as declarations instead of definitions.
34 SetVector<GlobalValue *> *GlobalsToImport;
36 /// Set to true if the given ModuleSummaryIndex contains any functions
37 /// from this source module, in which case we must conservatively assume
38 /// that any of its functions may be imported into another module
39 /// as part of a different backend compilation process.
40 bool HasExportedFunctions = false;
42 /// Set to true (only applicatable to ELF -fpic) if dso_local should be
43 /// dropped for a declaration.
45 /// On ELF, the assembler is conservative and assumes a global default
46 /// visibility symbol can be interposable. No direct access relocation is
47 /// allowed, if the definition is not in the translation unit, even if the
48 /// definition is available in the linkage unit. Thus we need to clear
49 /// dso_local to disable direct access.
51 /// This flag should not be set for -fno-pic or -fpie, which would
52 /// unnecessarily disable direct access.
53 bool ClearDSOLocalOnDeclarations;
55 /// Set of llvm.*used values, in order to validate that we don't try
56 /// to promote any non-renamable values.
57 SmallPtrSet<GlobalValue *, 8> Used;
59 /// Keep track of any COMDATs that require renaming (because COMDAT
60 /// leader was promoted and renamed). Maps from original COMDAT to one
62 DenseMap<const Comdat *, Comdat *> RenamedComdats;
64 /// Check if we should promote the given local value to global scope.
65 bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI);
68 /// Check if the given value is a local that can't be renamed (promoted).
69 /// Only used in assertion checking, and disabled under NDEBUG since the Used
70 /// set will not be populated.
71 bool isNonRenamableLocal(const GlobalValue &GV) const;
74 /// Helper methods to check if we are importing from or potentially
75 /// exporting from the current source module.
76 bool isPerformingImport() const { return GlobalsToImport != nullptr; }
77 bool isModuleExporting() const { return HasExportedFunctions; }
79 /// If we are importing from the source module, checks if we should
80 /// import SGV as a definition, otherwise import as a declaration.
81 bool doImportAsDefinition(const GlobalValue *SGV);
83 /// Get the name for a local SGV that should be promoted and renamed to global
84 /// scope in the linked destination module.
85 std::string getPromotedName(const GlobalValue *SGV);
87 /// Process globals so that they can be used in ThinLTO. This includes
88 /// promoting local variables so that they can be reference externally by
89 /// thin lto imported globals and converting strong external globals to
90 /// available_externally.
91 void processGlobalsForThinLTO();
92 void processGlobalForThinLTO(GlobalValue &GV);
94 /// Get the new linkage for SGV that should be used in the linked destination
95 /// module. Specifically, for ThinLTO importing or exporting it may need
96 /// to be adjusted. When \p DoPromote is true then we must adjust the
97 /// linkage for a required promotion of a local to global scope.
98 GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
101 FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index,
102 SetVector<GlobalValue *> *GlobalsToImport,
103 bool ClearDSOLocalOnDeclarations)
104 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
105 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
106 // If we have a ModuleSummaryIndex but no function to import,
107 // then this is the primary module being compiled in a ThinLTO
108 // backend compilation, and we need to see if it has functions that
109 // may be exported to another backend compilation.
110 if (!GlobalsToImport)
111 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
114 // First collect those in the llvm.used set.
115 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
116 // Next collect those in the llvm.compiler.used set.
117 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
124 /// Perform in-place global value handling on the given Module for
125 /// exported local functions renamed and promoted for ThinLTO.
126 bool renameModuleForThinLTO(
127 Module &M, const ModuleSummaryIndex &Index,
128 bool ClearDSOLocalOnDeclarations,
129 SetVector<GlobalValue *> *GlobalsToImport = nullptr);
131 /// Compute synthetic function entry counts.
132 void computeSyntheticCounts(ModuleSummaryIndex &Index);
134 } // End llvm namespace