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 #include "llvm/Support/Compiler.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 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 to true (only applicatable to ELF -fpic) if dso_local should be 44 /// dropped for a declaration. 45 /// 46 /// On ELF, the assembler is conservative and assumes a global default 47 /// visibility symbol can be interposable. No direct access relocation is 48 /// allowed, if the definition is not in the translation unit, even if the 49 /// definition is available in the linkage unit. Thus we need to clear 50 /// dso_local to disable direct access. 51 /// 52 /// This flag should not be set for -fno-pic or -fpie, which would 53 /// unnecessarily disable direct access. 54 bool ClearDSOLocalOnDeclarations; 55 56 /// Set of llvm.*used values, in order to validate that we don't try 57 /// to promote any non-renamable values. 58 SmallPtrSet<GlobalValue *, 4> Used; 59 60 /// Keep track of any COMDATs that require renaming (because COMDAT 61 /// leader was promoted and renamed). Maps from original COMDAT to one 62 /// with new name. 63 DenseMap<const Comdat *, Comdat *> RenamedComdats; 64 65 /// Check if we should promote the given local value to global scope. 66 bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI); 67 68 #ifndef NDEBUG 69 /// Check if the given value is a local that can't be renamed (promoted). 70 /// Only used in assertion checking, and disabled under NDEBUG since the Used 71 /// set will not be populated. 72 bool isNonRenamableLocal(const GlobalValue &GV) const; 73 #endif 74 75 /// Helper methods to check if we are importing from or potentially 76 /// exporting from the current source module. isPerformingImport()77 bool isPerformingImport() const { return GlobalsToImport != nullptr; } isModuleExporting()78 bool isModuleExporting() const { return HasExportedFunctions; } 79 80 /// If we are importing from the source module, checks if we should 81 /// import SGV as a definition, otherwise import as a declaration. 82 bool doImportAsDefinition(const GlobalValue *SGV); 83 84 /// Get the name for a local SGV that should be promoted and renamed to global 85 /// scope in the linked destination module. 86 std::string getPromotedName(const GlobalValue *SGV); 87 88 /// Process globals so that they can be used in ThinLTO. This includes 89 /// promoting local variables so that they can be reference externally by 90 /// thin lto imported globals and converting strong external globals to 91 /// available_externally. 92 void processGlobalsForThinLTO(); 93 void processGlobalForThinLTO(GlobalValue &GV); 94 95 /// Get the new linkage for SGV that should be used in the linked destination 96 /// module. Specifically, for ThinLTO importing or exporting it may need 97 /// to be adjusted. When \p DoPromote is true then we must adjust the 98 /// linkage for a required promotion of a local to global scope. 99 GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote); 100 101 /// The symbols with these names are moved to a different module and should be 102 /// promoted to external linkage where they are defined. 103 DenseSet<GlobalValue::GUID> SymbolsToMove; 104 105 public: 106 LLVM_ABI 107 FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, 108 SetVector<GlobalValue *> *GlobalsToImport, 109 bool ClearDSOLocalOnDeclarations); 110 LLVM_ABI void run(); 111 }; 112 113 /// Perform in-place global value handling on the given Module for 114 /// exported local functions renamed and promoted for ThinLTO. 115 LLVM_ABI void 116 renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, 117 bool ClearDSOLocalOnDeclarations, 118 SetVector<GlobalValue *> *GlobalsToImport = nullptr); 119 120 } // End llvm namespace 121 122 #endif 123