1 //===- FunctionImportUtils.h - Importing support utilities -----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the FunctionImportGlobalProcessing class which is used
10 // to perform the necessary global value handling for function importing.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
16 
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/IR/ModuleSummaryIndex.h"
19 
20 namespace llvm {
21 class Module;
22 
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.
27   Module &M;
28 
29   /// Module summary index passed in for function importing/exporting handling.
30   const ModuleSummaryIndex &ImportIndex;
31 
32   /// Globals to import from this module, all other functions will be
33   /// imported as declarations instead of definitions.
34   SetVector<GlobalValue *> *GlobalsToImport;
35 
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;
41 
42   /// Set of llvm.*used values, in order to validate that we don't try
43   /// to promote any non-renamable values.
44   SmallPtrSet<GlobalValue *, 8> Used;
45 
46   /// Keep track of any COMDATs that require renaming (because COMDAT
47   /// leader was promoted and renamed). Maps from original COMDAT to one
48   /// with new name.
49   DenseMap<const Comdat *, Comdat *> RenamedComdats;
50 
51   /// Check if we should promote the given local value to global scope.
52   bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI);
53 
54 #ifndef NDEBUG
55   /// Check if the given value is a local that can't be renamed (promoted).
56   /// Only used in assertion checking, and disabled under NDEBUG since the Used
57   /// set will not be populated.
58   bool isNonRenamableLocal(const GlobalValue &GV) const;
59 #endif
60 
61   /// Helper methods to check if we are importing from or potentially
62   /// exporting from the current source module.
63   bool isPerformingImport() const { return GlobalsToImport != nullptr; }
64   bool isModuleExporting() const { return HasExportedFunctions; }
65 
66   /// If we are importing from the source module, checks if we should
67   /// import SGV as a definition, otherwise import as a declaration.
68   bool doImportAsDefinition(const GlobalValue *SGV);
69 
70   /// Get the name for a local SGV that should be promoted and renamed to global
71   /// scope in the linked destination module.
72   std::string getPromotedName(const GlobalValue *SGV);
73 
74   /// Process globals so that they can be used in ThinLTO. This includes
75   /// promoting local variables so that they can be reference externally by
76   /// thin lto imported globals and converting strong external globals to
77   /// available_externally.
78   void processGlobalsForThinLTO();
79   void processGlobalForThinLTO(GlobalValue &GV);
80 
81   /// Get the new linkage for SGV that should be used in the linked destination
82   /// module. Specifically, for ThinLTO importing or exporting it may need
83   /// to be adjusted. When \p DoPromote is true then we must adjust the
84   /// linkage for a required promotion of a local to global scope.
85   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
86 
87 public:
88   FunctionImportGlobalProcessing(
89       Module &M, const ModuleSummaryIndex &Index,
90       SetVector<GlobalValue *> *GlobalsToImport = nullptr)
91       : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport) {
92     // If we have a ModuleSummaryIndex but no function to import,
93     // then this is the primary module being compiled in a ThinLTO
94     // backend compilation, and we need to see if it has functions that
95     // may be exported to another backend compilation.
96     if (!GlobalsToImport)
97       HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
98 
99 #ifndef NDEBUG
100     // First collect those in the llvm.used set.
101     collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
102     // Next collect those in the llvm.compiler.used set.
103     collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
104 #endif
105   }
106 
107   bool run();
108 };
109 
110 /// Perform in-place global value handling on the given Module for
111 /// exported local functions renamed and promoted for ThinLTO.
112 bool renameModuleForThinLTO(
113     Module &M, const ModuleSummaryIndex &Index,
114     SetVector<GlobalValue *> *GlobalsToImport = nullptr);
115 
116 /// Compute synthetic function entry counts.
117 void computeSyntheticCounts(ModuleSummaryIndex &Index);
118 
119 } // End llvm namespace
120 
121 #endif
122