MachineModuleInfo.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Collect meta information for a module. This information should be in a
  15. // neutral form that can be used by different debugging and exception handling
  16. // schemes.
  17. //
  18. // The organization of information is primarily clustered around the source
  19. // compile units. The main exception is source line correspondence where
  20. // inlining may interleave code from various compile units.
  21. //
  22. // The following information can be retrieved from the MachineModuleInfo.
  23. //
  24. // -- Source directories - Directories are uniqued based on their canonical
  25. // string and assigned a sequential numeric ID (base 1.)
  26. // -- Source files - Files are also uniqued based on their name and directory
  27. // ID. A file ID is sequential number (base 1.)
  28. // -- Source line correspondence - A vector of file ID, line#, column# triples.
  29. // A DEBUG_LOCATION instruction is generated by the DAG Legalizer
  30. // corresponding to each entry in the source line list. This allows a debug
  31. // emitter to generate labels referenced by debug information tables.
  32. //
  33. //===----------------------------------------------------------------------===//
  34. #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
  35. #define LLVM_CODEGEN_MACHINEMODULEINFO_H
  36. #include "llvm/ADT/DenseMap.h"
  37. #include "llvm/ADT/PointerIntPair.h"
  38. #include "llvm/IR/PassManager.h"
  39. #include "llvm/MC/MCContext.h"
  40. #include "llvm/MC/MCSymbol.h"
  41. #include "llvm/Pass.h"
  42. #include <memory>
  43. #include <utility>
  44. #include <vector>
  45. namespace llvm {
  46. class Function;
  47. class LLVMTargetMachine;
  48. class MachineFunction;
  49. class Module;
  50. //===----------------------------------------------------------------------===//
  51. /// This class can be derived from and used by targets to hold private
  52. /// target-specific information for each Module. Objects of type are
  53. /// accessed/created with MachineModuleInfo::getObjFileInfo and destroyed when
  54. /// the MachineModuleInfo is destroyed.
  55. ///
  56. class MachineModuleInfoImpl {
  57. public:
  58. using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>;
  59. using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>;
  60. virtual ~MachineModuleInfoImpl();
  61. protected:
  62. /// Return the entries from a DenseMap in a deterministic sorted orer.
  63. /// Clears the map.
  64. static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
  65. };
  66. //===----------------------------------------------------------------------===//
  67. /// This class contains meta information specific to a module. Queries can be
  68. /// made by different debugging and exception handling schemes and reformated
  69. /// for specific use.
  70. ///
  71. class MachineModuleInfo {
  72. friend class MachineModuleInfoWrapperPass;
  73. friend class MachineModuleAnalysis;
  74. const LLVMTargetMachine &TM;
  75. /// This is the MCContext used for the entire code generator.
  76. MCContext Context;
  77. // This is an external context, that if assigned, will be used instead of the
  78. // internal context.
  79. MCContext *ExternalContext = nullptr;
  80. /// This is the LLVM Module being worked on.
  81. const Module *TheModule;
  82. /// This is the object-file-format-specific implementation of
  83. /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
  84. /// want.
  85. MachineModuleInfoImpl *ObjFileMMI;
  86. /// \name Exception Handling
  87. /// \{
  88. /// The current call site index being processed, if any. 0 if none.
  89. unsigned CurCallSite;
  90. /// \}
  91. // TODO: Ideally, what we'd like is to have a switch that allows emitting
  92. // synchronous (precise at call-sites only) CFA into .eh_frame. However,
  93. // even under this switch, we'd like .debug_frame to be precise when using
  94. // -g. At this moment, there's no way to specify that some CFI directives
  95. // go into .eh_frame only, while others go into .debug_frame only.
  96. /// True if debugging information is available in this module.
  97. bool DbgInfoAvailable;
  98. /// True if this module is being built for windows/msvc, and uses floating
  99. /// point. This is used to emit an undefined reference to _fltused.
  100. bool UsesMSVCFloatingPoint;
  101. /// Maps IR Functions to their corresponding MachineFunctions.
  102. DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
  103. /// Next unique number available for a MachineFunction.
  104. unsigned NextFnNum = 0;
  105. const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
  106. MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
  107. MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
  108. public:
  109. explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
  110. explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  111. MCContext *ExtContext);
  112. MachineModuleInfo(MachineModuleInfo &&MMII);
  113. ~MachineModuleInfo();
  114. void initialize();
  115. void finalize();
  116. const LLVMTargetMachine &getTarget() const { return TM; }
  117. const MCContext &getContext() const {
  118. return ExternalContext ? *ExternalContext : Context;
  119. }
  120. MCContext &getContext() {
  121. return ExternalContext ? *ExternalContext : Context;
  122. }
  123. const Module *getModule() const { return TheModule; }
  124. /// Returns the MachineFunction constructed for the IR function \p F.
  125. /// Creates a new MachineFunction if none exists yet.
  126. MachineFunction &getOrCreateMachineFunction(Function &F);
  127. /// \brief Returns the MachineFunction associated to IR function \p F if there
  128. /// is one, otherwise nullptr.
  129. MachineFunction *getMachineFunction(const Function &F) const;
  130. /// Delete the MachineFunction \p MF and reset the link in the IR Function to
  131. /// Machine Function map.
  132. void deleteMachineFunctionFor(Function &F);
  133. /// Add an externally created MachineFunction \p MF for \p F.
  134. void insertFunction(const Function &F, std::unique_ptr<MachineFunction> &&MF);
  135. /// Keep track of various per-module pieces of information for backends
  136. /// that would like to do so.
  137. template<typename Ty>
  138. Ty &getObjFileInfo() {
  139. if (ObjFileMMI == nullptr)
  140. ObjFileMMI = new Ty(*this);
  141. return *static_cast<Ty*>(ObjFileMMI);
  142. }
  143. template<typename Ty>
  144. const Ty &getObjFileInfo() const {
  145. return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
  146. }
  147. /// Returns true if valid debug info is present.
  148. bool hasDebugInfo() const { return DbgInfoAvailable; }
  149. bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint; }
  150. void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; }
  151. /// \name Exception Handling
  152. /// \{
  153. /// Set the call site currently being processed.
  154. void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
  155. /// Get the call site currently being processed, if any. return zero if
  156. /// none.
  157. unsigned getCurrentCallSite() { return CurCallSite; }
  158. /// \}
  159. // MMI owes MCContext. It should never be invalidated.
  160. bool invalidate(Module &, const PreservedAnalyses &,
  161. ModuleAnalysisManager::Invalidator &) {
  162. return false;
  163. }
  164. }; // End class MachineModuleInfo
  165. class MachineModuleInfoWrapperPass : public ImmutablePass {
  166. MachineModuleInfo MMI;
  167. public:
  168. static char ID; // Pass identification, replacement for typeid
  169. explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr);
  170. explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
  171. MCContext *ExtContext);
  172. // Initialization and Finalization
  173. bool doInitialization(Module &) override;
  174. bool doFinalization(Module &) override;
  175. MachineModuleInfo &getMMI() { return MMI; }
  176. const MachineModuleInfo &getMMI() const { return MMI; }
  177. };
  178. /// An analysis that produces \c MachineInfo for a module.
  179. class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> {
  180. friend AnalysisInfoMixin<MachineModuleAnalysis>;
  181. static AnalysisKey Key;
  182. const LLVMTargetMachine *TM;
  183. public:
  184. /// Provide the result type for this analysis pass.
  185. using Result = MachineModuleInfo;
  186. MachineModuleAnalysis(const LLVMTargetMachine *TM) : TM(TM) {}
  187. /// Run the analysis pass and produce machine module information.
  188. MachineModuleInfo run(Module &M, ModuleAnalysisManager &);
  189. };
  190. } // end namespace llvm
  191. #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H
  192. #ifdef __GNUC__
  193. #pragma GCC diagnostic pop
  194. #endif