LLVM  13.0.0git
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 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/SetVector.h"
20 namespace llvm {
21 class Module;
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;
29  /// Module summary index passed in for function importing/exporting handling.
30  const ModuleSummaryIndex &ImportIndex;
32  /// Globals to import from this module, all other functions will be
33  /// imported as declarations instead of definitions.
34  SetVector<GlobalValue *> *GlobalsToImport;
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;
42  /// Set to true (only applicatable to ELF -fpic) if dso_local should be
43  /// dropped for a declaration.
44  ///
45  /// On ELF, the assembler is conservative and assumes a global default
46  /// visibility symbol can be interposable. No direct access relocation is
47  /// allowed, if the definition is not in the translation unit, even if the
48  /// definition is available in the linkage unit. Thus we need to clear
49  /// dso_local to disable direct access.
50  ///
51  /// This flag should not be set for -fno-pic or -fpie, which would
52  /// unnecessarily disable direct access.
53  bool ClearDSOLocalOnDeclarations;
55  /// Set of llvm.*used values, in order to validate that we don't try
56  /// to promote any non-renamable values.
59  /// Keep track of any COMDATs that require renaming (because COMDAT
60  /// leader was promoted and renamed). Maps from original COMDAT to one
61  /// with new name.
64  /// Check if we should promote the given local value to global scope.
65  bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI);
67 #ifndef NDEBUG
68  /// Check if the given value is a local that can't be renamed (promoted).
69  /// Only used in assertion checking, and disabled under NDEBUG since the Used
70  /// set will not be populated.
71  bool isNonRenamableLocal(const GlobalValue &GV) const;
72 #endif
74  /// Helper methods to check if we are importing from or potentially
75  /// exporting from the current source module.
76  bool isPerformingImport() const { return GlobalsToImport != nullptr; }
77  bool isModuleExporting() const { return HasExportedFunctions; }
79  /// If we are importing from the source module, checks if we should
80  /// import SGV as a definition, otherwise import as a declaration.
81  bool doImportAsDefinition(const GlobalValue *SGV);
83  /// Get the name for a local SGV that should be promoted and renamed to global
84  /// scope in the linked destination module.
85  std::string getPromotedName(const GlobalValue *SGV);
87  /// Process globals so that they can be used in ThinLTO. This includes
88  /// promoting local variables so that they can be reference externally by
89  /// thin lto imported globals and converting strong external globals to
90  /// available_externally.
91  void processGlobalsForThinLTO();
92  void processGlobalForThinLTO(GlobalValue &GV);
94  /// Get the new linkage for SGV that should be used in the linked destination
95  /// module. Specifically, for ThinLTO importing or exporting it may need
96  /// to be adjusted. When \p DoPromote is true then we must adjust the
97  /// linkage for a required promotion of a local to global scope.
98  GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
100 public:
102  SetVector<GlobalValue *> *GlobalsToImport,
103  bool ClearDSOLocalOnDeclarations)
104  : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
105  ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
106  // If we have a ModuleSummaryIndex but no function to import,
107  // then this is the primary module being compiled in a ThinLTO
108  // backend compilation, and we need to see if it has functions that
109  // may be exported to another backend compilation.
110  if (!GlobalsToImport)
111  HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
113 #ifndef NDEBUG
115  // First collect those in the llvm.used set.
116  collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
117  // Next collect those in the llvm.compiler.used set.
118  collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
119  Used = {Vec.begin(), Vec.end()};
120 #endif
121  }
123  bool run();
124 };
126 /// Perform in-place global value handling on the given Module for
127 /// exported local functions renamed and promoted for ThinLTO.
129  Module &M, const ModuleSummaryIndex &Index,
130  bool ClearDSOLocalOnDeclarations,
131  SetVector<GlobalValue *> *GlobalsToImport = nullptr);
133 /// Compute synthetic function entry counts.
134 void computeSyntheticCounts(ModuleSummaryIndex &Index);
136 } // End llvm namespace
138 #endif
bool run()
Definition: FunctionImportUtils.cpp:323
Definition: AllocatorList.h:23
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
Definition: FunctionImportUtils.cpp:328
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1167
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:763
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
Definition: SummaryBasedOptimizations.cpp:47
FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue * > *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
Definition: FunctionImportUtils.h:101
bool hasExportedFunctions(const Module &M) const
Check if the given Module has any functions available for exporting in the index.
Definition: ModuleSummaryIndex.h:1436
Struct that holds a reference to a particular GUID in a global value summary.
Definition: ModuleSummaryIndex.h:166
Definition: GlobalValue.h:44
@ VI
Definition: SIInstrInfo.cpp:7494
uint32_t Index
Definition: ELFObjHandler.cpp:84
Definition: DenseMap.h:714
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
Definition: FunctionImportUtils.h:25
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1026
A vector that has set insertion semantics.
Definition: SetVector.h:40