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);
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 SGV that should be used in the linked destination
71   /// module. Specifically, this handles the case where we need to rename
72   /// a local that is being promoted to global scope, which it will always
73   /// do when \p DoPromote is true (or when importing a local).
74   std::string getName(const GlobalValue *SGV, bool DoPromote);
75 
76   /// Process globals so that they can be used in ThinLTO. This includes
77   /// promoting local variables so that they can be reference externally by
78   /// thin lto imported globals and converting strong external globals to
79   /// available_externally.
80   void processGlobalsForThinLTO();
81   void processGlobalForThinLTO(GlobalValue &GV);
82 
83   /// Get the new linkage for SGV that should be used in the linked destination
84   /// module. Specifically, for ThinLTO importing or exporting it may need
85   /// to be adjusted. When \p DoPromote is true then we must adjust the
86   /// linkage for a required promotion of a local to global scope.
87   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
88 
89 public:
90   FunctionImportGlobalProcessing(
91       Module &M, const ModuleSummaryIndex &Index,
92       SetVector<GlobalValue *> *GlobalsToImport = nullptr)
93       : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport) {
94     // If we have a ModuleSummaryIndex but no function to import,
95     // then this is the primary module being compiled in a ThinLTO
96     // backend compilation, and we need to see if it has functions that
97     // may be exported to another backend compilation.
98     if (!GlobalsToImport)
99       HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
100 
101 #ifndef NDEBUG
102     // First collect those in the llvm.used set.
103     collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
104     // Next collect those in the llvm.compiler.used set.
105     collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
106 #endif
107   }
108 
109   bool run();
110 
111   static bool doImportAsDefinition(const GlobalValue *SGV,
112                                    SetVector<GlobalValue *> *GlobalsToImport);
113 };
114 
115 /// Perform in-place global value handling on the given Module for
116 /// exported local functions renamed and promoted for ThinLTO.
117 bool renameModuleForThinLTO(
118     Module &M, const ModuleSummaryIndex &Index,
119     SetVector<GlobalValue *> *GlobalsToImport = nullptr);
120 
121 /// Compute synthetic function entry counts.
122 void computeSyntheticCounts(ModuleSummaryIndex &Index);
123 
124 } // End llvm namespace
125 
126 #endif
127