LLVM  8.0.1
FunctionImportUtils.h
Go to the documentation of this file.
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"
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.
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  SetVector<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.
46 
47  /// Keep track of any COMDATs that require renaming (because COMDAT
48  /// leader was promoted and renamed). Maps from original COMDAT to one
49  /// with new name.
51 
52  /// Check if we should promote the given local value to global scope.
53  bool shouldPromoteLocalToGlobal(const GlobalValue *SGV);
54 
55 #ifndef NDEBUG
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;
60 #endif
61 
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; }
66 
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);
70 
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);
76 
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);
83 
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);
89 
90 public:
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.
99  if (!GlobalsToImport)
100  HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
101 
102 #ifndef NDEBUG
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);
107 #endif
108  }
109 
110  bool run();
111 
112  static bool doImportAsDefinition(const GlobalValue *SGV,
113  SetVector<GlobalValue *> *GlobalsToImport);
114 };
115 
116 /// Perform in-place global value handling on the given Module for
117 /// exported local functions renamed and promoted for ThinLTO.
119  Module &M, const ModuleSummaryIndex &Index,
120  SetVector<GlobalValue *> *GlobalsToImport = nullptr);
121 
122 /// Compute synthetic function entry counts.
124 
125 } // End llvm namespace
126 
127 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Class to handle necessary GlobalValue changes required by ThinLTO function importing, including linkage changes and any necessary renaming.
bool hasExportedFunctions(const Module &M) const
Check if the given Module has any functions available for exporting in the index. ...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue *> &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:596
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:48
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
A vector that has set insertion semantics.
Definition: SetVector.h:41