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 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. 45 SmallPtrSet<GlobalValue *, 8> Used; 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. 50 DenseMap<const Comdat *, Comdat *> RenamedComdats; 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. isPerformingImport()64 bool isPerformingImport() const { return GlobalsToImport != nullptr; } isModuleExporting()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: 91 FunctionImportGlobalProcessing( 92 Module &M, const ModuleSummaryIndex &Index, 93 SetVector<GlobalValue *> *GlobalsToImport = nullptr) M(M)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. 118 bool renameModuleForThinLTO( 119 Module &M, const ModuleSummaryIndex &Index, 120 SetVector<GlobalValue *> *GlobalsToImport = nullptr); 121 122 /// Compute synthetic function entry counts. 123 void computeSyntheticCounts(ModuleSummaryIndex &Index); 124 125 } // End llvm namespace 126 127 #endif 128