LLVM  10.0.0svn
FunctionImportUtils.h
Go to the documentation of this file.
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"
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.
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.
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.
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:
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.
118  Module &M, const ModuleSummaryIndex &Index,
119  SetVector<GlobalValue *> *GlobalsToImport = nullptr);
120 
121 /// Compute synthetic function entry counts.
123 
124 } // End llvm namespace
125 
126 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:598
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
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:40