]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Transforms / Utils / FunctionImportUtils.h
1 //===- FunctionImportUtils.h - Importing support utilities -----*- C++ -*-===//
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 defines the FunctionImportGlobalProcessing class which is used
11 // to perform the necessary global value handling for function importing.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
16 #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
17
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/IR/ModuleSummaryIndex.h"
20
21 namespace llvm {
22 class Module;
23
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.
28   Module &M;
29
30   /// Module summary index passed in for function importing/exporting handling.
31   const ModuleSummaryIndex &ImportIndex;
32
33   /// Globals to import from this module, all other functions will be
34   /// imported as declarations instead of definitions.
35   DenseSet<const GlobalValue *> *GlobalsToImport;
36
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;
42
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;
46
47   /// Check if we should promote the given local value to global scope.
48   bool shouldPromoteLocalToGlobal(const GlobalValue *SGV);
49
50 #ifndef NDEBUG
51   /// Check if the given value is a local that can't be renamed (promoted).
52   /// Only used in assertion checking, and disabled under NDEBUG since the Used
53   /// set will not be populated.
54   bool isNonRenamableLocal(const GlobalValue &GV) const;
55 #endif
56
57   /// Helper methods to check if we are importing from or potentially
58   /// exporting from the current source module.
59   bool isPerformingImport() const { return GlobalsToImport != nullptr; }
60   bool isModuleExporting() const { return HasExportedFunctions; }
61
62   /// If we are importing from the source module, checks if we should
63   /// import SGV as a definition, otherwise import as a declaration.
64   bool doImportAsDefinition(const GlobalValue *SGV);
65
66   /// Get the name for SGV that should be used in the linked destination
67   /// module. Specifically, this handles the case where we need to rename
68   /// a local that is being promoted to global scope, which it will always
69   /// do when \p DoPromote is true (or when importing a local).
70   std::string getName(const GlobalValue *SGV, bool DoPromote);
71
72   /// Process globals so that they can be used in ThinLTO. This includes
73   /// promoting local variables so that they can be reference externally by
74   /// thin lto imported globals and converting strong external globals to
75   /// available_externally.
76   void processGlobalsForThinLTO();
77   void processGlobalForThinLTO(GlobalValue &GV);
78
79   /// Get the new linkage for SGV that should be used in the linked destination
80   /// module. Specifically, for ThinLTO importing or exporting it may need
81   /// to be adjusted. When \p DoPromote is true then we must adjust the
82   /// linkage for a required promotion of a local to global scope.
83   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
84
85 public:
86   FunctionImportGlobalProcessing(
87       Module &M, const ModuleSummaryIndex &Index,
88       DenseSet<const GlobalValue *> *GlobalsToImport = nullptr)
89       : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport) {
90     // If we have a ModuleSummaryIndex but no function to import,
91     // then this is the primary module being compiled in a ThinLTO
92     // backend compilation, and we need to see if it has functions that
93     // may be exported to another backend compilation.
94     if (!GlobalsToImport)
95       HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
96
97 #ifndef NDEBUG
98     // First collect those in the llvm.used set.
99     collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
100     // Next collect those in the llvm.compiler.used set.
101     collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
102 #endif
103   }
104
105   bool run();
106
107   static bool
108   doImportAsDefinition(const GlobalValue *SGV,
109                        DenseSet<const GlobalValue *> *GlobalsToImport);
110 };
111
112 /// Perform in-place global value handling on the given Module for
113 /// exported local functions renamed and promoted for ThinLTO.
114 bool renameModuleForThinLTO(
115     Module &M, const ModuleSummaryIndex &Index,
116     DenseSet<const GlobalValue *> *GlobalsToImport = nullptr);
117
118 } // End llvm namespace
119
120 #endif