1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- 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 // Collect meta information for a module. This information should be in a 10 // neutral form that can be used by different debugging and exception handling 11 // schemes. 12 // 13 // The organization of information is primarily clustered around the source 14 // compile units. The main exception is source line correspondence where 15 // inlining may interleave code from various compile units. 16 // 17 // The following information can be retrieved from the MachineModuleInfo. 18 // 19 // -- Source directories - Directories are uniqued based on their canonical 20 // string and assigned a sequential numeric ID (base 1.) 21 // -- Source files - Files are also uniqued based on their name and directory 22 // ID. A file ID is sequential number (base 1.) 23 // -- Source line correspondence - A vector of file ID, line#, column# triples. 24 // A DEBUG_LOCATION instruction is generated by the DAG Legalizer 25 // corresponding to each entry in the source line list. This allows a debug 26 // emitter to generate labels referenced by debug information tables. 27 // 28 //===----------------------------------------------------------------------===// 29 30 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H 31 #define LLVM_CODEGEN_MACHINEMODULEINFO_H 32 33 #include "llvm/ADT/DenseMap.h" 34 #include "llvm/ADT/PointerIntPair.h" 35 #include "llvm/IR/PassManager.h" 36 #include "llvm/MC/MCContext.h" 37 #include "llvm/MC/MCSymbol.h" 38 #include "llvm/Pass.h" 39 #include "llvm/Support/Compiler.h" 40 #include <memory> 41 #include <utility> 42 #include <vector> 43 44 namespace llvm { 45 46 class Function; 47 class TargetMachine; 48 class MachineFunction; 49 class Module; 50 51 //===----------------------------------------------------------------------===// 52 /// This class can be derived from and used by targets to hold private 53 /// target-specific information for each Module. Objects of type are 54 /// accessed/created with MachineModuleInfo::getObjFileInfo and destroyed when 55 /// the MachineModuleInfo is destroyed. 56 /// 57 class LLVM_ABI MachineModuleInfoImpl { 58 public: 59 using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>; 60 using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>; 61 62 /// A variant of SymbolListTy where the stub is a generalized MCExpr. 63 using ExprStubListTy = std::vector<std::pair<MCSymbol *, const MCExpr *>>; 64 65 virtual ~MachineModuleInfoImpl(); 66 67 protected: 68 /// Return the entries from a DenseMap in a deterministic sorted orer. 69 /// Clears the map. 70 static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&); 71 72 /// Return the entries from a DenseMap in a deterministic sorted orer. 73 /// Clears the map. 74 static ExprStubListTy 75 getSortedExprStubs(DenseMap<MCSymbol *, const MCExpr *> &); 76 }; 77 78 //===----------------------------------------------------------------------===// 79 /// This class contains meta information specific to a module. Queries can be 80 /// made by different debugging and exception handling schemes and reformated 81 /// for specific use. 82 /// 83 class MachineModuleInfo { 84 friend class MachineModuleInfoWrapperPass; 85 friend class MachineModuleAnalysis; 86 87 const TargetMachine &TM; 88 89 /// This is the MCContext used for the entire code generator. 90 MCContext Context; 91 // This is an external context, that if assigned, will be used instead of the 92 // internal context. 93 MCContext *ExternalContext = nullptr; 94 95 /// This is the LLVM Module being worked on. 96 const Module *TheModule = nullptr; 97 98 /// This is the object-file-format-specific implementation of 99 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they 100 /// want. 101 MachineModuleInfoImpl *ObjFileMMI; 102 103 /// Maps IR Functions to their corresponding MachineFunctions. 104 DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions; 105 /// Next unique number available for a MachineFunction. 106 unsigned NextFnNum = 0; 107 const Function *LastRequest = nullptr; ///< Used for shortcut/cache. 108 MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache. 109 110 MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete; 111 112 public: 113 LLVM_ABI explicit MachineModuleInfo(const TargetMachine *TM = nullptr); 114 115 LLVM_ABI explicit MachineModuleInfo(const TargetMachine *TM, 116 MCContext *ExtContext); 117 118 LLVM_ABI MachineModuleInfo(MachineModuleInfo &&MMII); 119 120 LLVM_ABI ~MachineModuleInfo(); 121 122 LLVM_ABI void initialize(); 123 LLVM_ABI void finalize(); 124 getTarget()125 const TargetMachine &getTarget() const { return TM; } 126 getContext()127 const MCContext &getContext() const { 128 return ExternalContext ? *ExternalContext : Context; 129 } getContext()130 MCContext &getContext() { 131 return ExternalContext ? *ExternalContext : Context; 132 } 133 getModule()134 const Module *getModule() const { return TheModule; } 135 136 /// Returns the MachineFunction constructed for the IR function \p F. 137 /// Creates a new MachineFunction if none exists yet. 138 /// NOTE: New pass manager clients shall not use this method to get 139 /// the `MachineFunction`, use `MachineFunctionAnalysis` instead. 140 LLVM_ABI MachineFunction &getOrCreateMachineFunction(Function &F); 141 142 /// \brief Returns the MachineFunction associated to IR function \p F if there 143 /// is one, otherwise nullptr. 144 /// NOTE: New pass manager clients shall not use this method to get 145 /// the `MachineFunction`, use `MachineFunctionAnalysis` instead. 146 LLVM_ABI MachineFunction *getMachineFunction(const Function &F) const; 147 148 /// Delete the MachineFunction \p MF and reset the link in the IR Function to 149 /// Machine Function map. 150 LLVM_ABI void deleteMachineFunctionFor(Function &F); 151 152 /// Add an externally created MachineFunction \p MF for \p F. 153 LLVM_ABI void insertFunction(const Function &F, 154 std::unique_ptr<MachineFunction> &&MF); 155 156 /// Keep track of various per-module pieces of information for backends 157 /// that would like to do so. 158 template<typename Ty> getObjFileInfo()159 Ty &getObjFileInfo() { 160 if (ObjFileMMI == nullptr) 161 ObjFileMMI = new Ty(*this); 162 return *static_cast<Ty*>(ObjFileMMI); 163 } 164 165 template<typename Ty> getObjFileInfo()166 const Ty &getObjFileInfo() const { 167 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>(); 168 } 169 170 /// \} 171 }; // End class MachineModuleInfo 172 173 class LLVM_ABI MachineModuleInfoWrapperPass : public ImmutablePass { 174 MachineModuleInfo MMI; 175 176 public: 177 static char ID; // Pass identification, replacement for typeid 178 explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr); 179 180 explicit MachineModuleInfoWrapperPass(const TargetMachine *TM, 181 MCContext *ExtContext); 182 183 // Initialization and Finalization 184 bool doInitialization(Module &) override; 185 bool doFinalization(Module &) override; 186 getMMI()187 MachineModuleInfo &getMMI() { return MMI; } getMMI()188 const MachineModuleInfo &getMMI() const { return MMI; } 189 }; 190 191 /// An analysis that produces \c MachineModuleInfo for a module. 192 /// This does not produce its own MachineModuleInfo because we need a consistent 193 /// MachineModuleInfo to keep ownership of MachineFunctions regardless of 194 /// analysis invalidation/clearing. So something outside the analysis 195 /// infrastructure must own the MachineModuleInfo. 196 class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> { 197 friend AnalysisInfoMixin<MachineModuleAnalysis>; 198 LLVM_ABI static AnalysisKey Key; 199 200 MachineModuleInfo &MMI; 201 202 public: 203 class Result { 204 MachineModuleInfo &MMI; Result(MachineModuleInfo & MMI)205 Result(MachineModuleInfo &MMI) : MMI(MMI) {} 206 friend class MachineModuleAnalysis; 207 208 public: getMMI()209 MachineModuleInfo &getMMI() { return MMI; } 210 211 // MMI owes MCContext. It should never be invalidated. invalidate(Module &,const PreservedAnalyses &,ModuleAnalysisManager::Invalidator &)212 bool invalidate(Module &, const PreservedAnalyses &, 213 ModuleAnalysisManager::Invalidator &) { 214 return false; 215 } 216 }; 217 MachineModuleAnalysis(MachineModuleInfo & MMI)218 MachineModuleAnalysis(MachineModuleInfo &MMI) : MMI(MMI) {} 219 220 /// Run the analysis pass and produce machine module information. 221 LLVM_ABI Result run(Module &M, ModuleAnalysisManager &); 222 }; 223 224 } // end namespace llvm 225 226 #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H 227