MachineModuleInfo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/ArrayRef.h"
  37. #include "llvm/ADT/DenseMap.h"
  38. #include "llvm/ADT/PointerIntPair.h"
  39. #include "llvm/IR/PassManager.h"
  40. #include "llvm/MC/MCContext.h"
  41. #include "llvm/MC/MCSymbol.h"
  42. #include "llvm/Pass.h"
  43. #include <memory>
  44. #include <utility>
  45. #include <vector>
  46. namespace llvm {
  47. class BasicBlock;
  48. class Function;
  49. class LLVMTargetMachine;
  50. class MMIAddrLabelMap;
  51. class MachineFunction;
  52. class Module;
  53. //===----------------------------------------------------------------------===//
  54. /// This class can be derived from and used by targets to hold private
  55. /// target-specific information for each Module. Objects of type are
  56. /// accessed/created with MachineModuleInfo::getObjFileInfo and destroyed when
  57. /// the MachineModuleInfo is destroyed.
  58. ///
  59. class MachineModuleInfoImpl {
  60. public:
  61. using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>;
  62. using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>;
  63. virtual ~MachineModuleInfoImpl();
  64. protected:
  65. /// Return the entries from a DenseMap in a deterministic sorted orer.
  66. /// Clears the map.
  67. static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
  68. };
  69. //===----------------------------------------------------------------------===//
  70. /// This class contains meta information specific to a module. Queries can be
  71. /// made by different debugging and exception handling schemes and reformated
  72. /// for specific use.
  73. ///
  74. class MachineModuleInfo {
  75. friend class MachineModuleInfoWrapperPass;
  76. friend class MachineModuleAnalysis;
  77. const LLVMTargetMachine &TM;
  78. /// This is the MCContext used for the entire code generator.
  79. MCContext Context;
  80. // This is an external context, that if assigned, will be used instead of the
  81. // internal context.
  82. MCContext *ExternalContext = nullptr;
  83. /// This is the LLVM Module being worked on.
  84. const Module *TheModule;
  85. /// This is the object-file-format-specific implementation of
  86. /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
  87. /// want.
  88. MachineModuleInfoImpl *ObjFileMMI;
  89. /// \name Exception Handling
  90. /// \{
  91. /// Vector of all personality functions ever seen. Used to emit common EH
  92. /// frames.
  93. std::vector<const Function *> Personalities;
  94. /// The current call site index being processed, if any. 0 if none.
  95. unsigned CurCallSite;
  96. /// \}
  97. /// This map keeps track of which symbol is being used for the specified
  98. /// basic block's address of label.
  99. MMIAddrLabelMap *AddrLabelSymbols;
  100. // TODO: Ideally, what we'd like is to have a switch that allows emitting
  101. // synchronous (precise at call-sites only) CFA into .eh_frame. However,
  102. // even under this switch, we'd like .debug_frame to be precise when using
  103. // -g. At this moment, there's no way to specify that some CFI directives
  104. // go into .eh_frame only, while others go into .debug_frame only.
  105. /// True if debugging information is available in this module.
  106. bool DbgInfoAvailable;
  107. /// True if this module is being built for windows/msvc, and uses floating
  108. /// point. This is used to emit an undefined reference to _fltused.
  109. bool UsesMSVCFloatingPoint;
  110. /// True if the module calls the __morestack function indirectly, as is
  111. /// required under the large code model on x86. This is used to emit
  112. /// a definition of a symbol, __morestack_addr, containing the address. See
  113. /// comments in lib/Target/X86/X86FrameLowering.cpp for more details.
  114. bool UsesMorestackAddr;
  115. /// True if the module contains split-stack functions. This is used to
  116. /// emit .note.GNU-split-stack section as required by the linker for
  117. /// special handling split-stack function calling no-split-stack function.
  118. bool HasSplitStack;
  119. /// True if the module contains no-split-stack functions. This is used to
  120. /// emit .note.GNU-no-split-stack section when it also contains split-stack
  121. /// functions.
  122. bool HasNosplitStack;
  123. /// Maps IR Functions to their corresponding MachineFunctions.
  124. DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
  125. /// Next unique number available for a MachineFunction.
  126. unsigned NextFnNum = 0;
  127. const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
  128. MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
  129. MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
  130. public:
  131. explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
  132. explicit MachineModuleInfo(const LLVMTargetMachine *TM,
  133. MCContext *ExtContext);
  134. MachineModuleInfo(MachineModuleInfo &&MMII);
  135. ~MachineModuleInfo();
  136. void initialize();
  137. void finalize();
  138. const LLVMTargetMachine &getTarget() const { return TM; }
  139. const MCContext &getContext() const {
  140. return ExternalContext ? *ExternalContext : Context;
  141. }
  142. MCContext &getContext() {
  143. return ExternalContext ? *ExternalContext : Context;
  144. }
  145. const Module *getModule() const { return TheModule; }
  146. /// Returns the MachineFunction constructed for the IR function \p F.
  147. /// Creates a new MachineFunction if none exists yet.
  148. MachineFunction &getOrCreateMachineFunction(Function &F);
  149. /// \brief Returns the MachineFunction associated to IR function \p F if there
  150. /// is one, otherwise nullptr.
  151. MachineFunction *getMachineFunction(const Function &F) const;
  152. /// Delete the MachineFunction \p MF and reset the link in the IR Function to
  153. /// Machine Function map.
  154. void deleteMachineFunctionFor(Function &F);
  155. /// Keep track of various per-module pieces of information for backends
  156. /// that would like to do so.
  157. template<typename Ty>
  158. Ty &getObjFileInfo() {
  159. if (ObjFileMMI == nullptr)
  160. ObjFileMMI = new Ty(*this);
  161. return *static_cast<Ty*>(ObjFileMMI);
  162. }
  163. template<typename Ty>
  164. const Ty &getObjFileInfo() const {
  165. return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
  166. }
  167. /// Returns true if valid debug info is present.
  168. bool hasDebugInfo() const { return DbgInfoAvailable; }
  169. void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
  170. bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint; }
  171. void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; }
  172. bool usesMorestackAddr() const {
  173. return UsesMorestackAddr;
  174. }
  175. void setUsesMorestackAddr(bool b) {
  176. UsesMorestackAddr = b;
  177. }
  178. bool hasSplitStack() const {
  179. return HasSplitStack;
  180. }
  181. void setHasSplitStack(bool b) {
  182. HasSplitStack = b;
  183. }
  184. bool hasNosplitStack() const {
  185. return HasNosplitStack;
  186. }
  187. void setHasNosplitStack(bool b) {
  188. HasNosplitStack = b;
  189. }
  190. /// Return the symbol to be used for the specified basic block when its
  191. /// address is taken. This cannot be its normal LBB label because the block
  192. /// may be accessed outside its containing function.
  193. MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) {
  194. return getAddrLabelSymbolToEmit(BB).front();
  195. }
  196. /// Return the symbol to be used for the specified basic block when its
  197. /// address is taken. If other blocks were RAUW'd to this one, we may have
  198. /// to emit them as well, return the whole set.
  199. ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB);
  200. /// If the specified function has had any references to address-taken blocks
  201. /// generated, but the block got deleted, return the symbol now so we can
  202. /// emit it. This prevents emitting a reference to a symbol that has no
  203. /// definition.
  204. void takeDeletedSymbolsForFunction(const Function *F,
  205. std::vector<MCSymbol*> &Result);
  206. /// \name Exception Handling
  207. /// \{
  208. /// Set the call site currently being processed.
  209. void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
  210. /// Get the call site currently being processed, if any. return zero if
  211. /// none.
  212. unsigned getCurrentCallSite() { return CurCallSite; }
  213. /// Provide the personality function for the exception information.
  214. void addPersonality(const Function *Personality);
  215. /// Return array of personality functions ever seen.
  216. const std::vector<const Function *>& getPersonalities() const {
  217. return Personalities;
  218. }
  219. /// \}
  220. // MMI owes MCContext. It should never be invalidated.
  221. bool invalidate(Module &, const PreservedAnalyses &,
  222. ModuleAnalysisManager::Invalidator &) {
  223. return false;
  224. }
  225. }; // End class MachineModuleInfo
  226. class MachineModuleInfoWrapperPass : public ImmutablePass {
  227. MachineModuleInfo MMI;
  228. public:
  229. static char ID; // Pass identification, replacement for typeid
  230. explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr);
  231. explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
  232. MCContext *ExtContext);
  233. // Initialization and Finalization
  234. bool doInitialization(Module &) override;
  235. bool doFinalization(Module &) override;
  236. MachineModuleInfo &getMMI() { return MMI; }
  237. const MachineModuleInfo &getMMI() const { return MMI; }
  238. };
  239. /// An analysis that produces \c MachineInfo for a module.
  240. class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> {
  241. friend AnalysisInfoMixin<MachineModuleAnalysis>;
  242. static AnalysisKey Key;
  243. const LLVMTargetMachine *TM;
  244. public:
  245. /// Provide the result type for this analysis pass.
  246. using Result = MachineModuleInfo;
  247. MachineModuleAnalysis(const LLVMTargetMachine *TM) : TM(TM) {}
  248. /// Run the analysis pass and produce machine module information.
  249. MachineModuleInfo run(Module &M, ModuleAnalysisManager &);
  250. };
  251. } // end namespace llvm
  252. #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H
  253. #ifdef __GNUC__
  254. #pragma GCC diagnostic pop
  255. #endif