TargetLoweringObjectFile.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- 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. // This file implements classes used to handle lowerings specific to common
  15. // object file formats.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H
  19. #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H
  20. #include "llvm/MC/MCObjectFileInfo.h"
  21. #include <cstdint>
  22. namespace llvm {
  23. class Constant;
  24. class DataLayout;
  25. class Function;
  26. class GlobalObject;
  27. class GlobalValue;
  28. class MachineBasicBlock;
  29. class MachineModuleInfo;
  30. class Mangler;
  31. class MCContext;
  32. class MCExpr;
  33. class MCSection;
  34. class MCSymbol;
  35. class MCSymbolRefExpr;
  36. class MCStreamer;
  37. class MCValue;
  38. class Module;
  39. class SectionKind;
  40. class StringRef;
  41. class TargetMachine;
  42. class DSOLocalEquivalent;
  43. class TargetLoweringObjectFile : public MCObjectFileInfo {
  44. /// Name-mangler for global names.
  45. Mangler *Mang = nullptr;
  46. protected:
  47. bool SupportIndirectSymViaGOTPCRel = false;
  48. bool SupportGOTPCRelWithOffset = true;
  49. bool SupportDebugThreadLocalLocation = true;
  50. bool SupportDSOLocalEquivalentLowering = false;
  51. /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values
  52. /// for EH.
  53. unsigned PersonalityEncoding = 0;
  54. unsigned LSDAEncoding = 0;
  55. unsigned TTypeEncoding = 0;
  56. unsigned CallSiteEncoding = 0;
  57. /// This section contains the static constructor pointer list.
  58. MCSection *StaticCtorSection = nullptr;
  59. /// This section contains the static destructor pointer list.
  60. MCSection *StaticDtorSection = nullptr;
  61. const TargetMachine *TM = nullptr;
  62. public:
  63. TargetLoweringObjectFile() = default;
  64. TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
  65. TargetLoweringObjectFile &
  66. operator=(const TargetLoweringObjectFile &) = delete;
  67. virtual ~TargetLoweringObjectFile();
  68. Mangler &getMangler() const { return *Mang; }
  69. /// This method must be called before any actual lowering is done. This
  70. /// specifies the current context for codegen, and gives the lowering
  71. /// implementations a chance to set up their default sections.
  72. virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
  73. virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
  74. const MCSymbol *Sym) const;
  75. /// Emit the module-level metadata that the platform cares about.
  76. virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
  77. /// Emit Call Graph Profile metadata.
  78. void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;
  79. /// Get the module-level metadata that the platform cares about.
  80. virtual void getModuleMetadata(Module &M) {}
  81. /// Given a constant with the SectionKind, return a section that it should be
  82. /// placed in.
  83. virtual MCSection *getSectionForConstant(const DataLayout &DL,
  84. SectionKind Kind, const Constant *C,
  85. Align &Alignment) const;
  86. virtual MCSection *
  87. getSectionForMachineBasicBlock(const Function &F,
  88. const MachineBasicBlock &MBB,
  89. const TargetMachine &TM) const;
  90. /// Classify the specified global variable into a set of target independent
  91. /// categories embodied in SectionKind.
  92. static SectionKind getKindForGlobal(const GlobalObject *GO,
  93. const TargetMachine &TM);
  94. /// This method computes the appropriate section to emit the specified global
  95. /// variable or function definition. This should not be passed external (or
  96. /// available externally) globals.
  97. MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  98. const TargetMachine &TM) const;
  99. /// This method computes the appropriate section to emit the specified global
  100. /// variable or function definition. This should not be passed external (or
  101. /// available externally) globals.
  102. MCSection *SectionForGlobal(const GlobalObject *GO,
  103. const TargetMachine &TM) const;
  104. virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
  105. const GlobalValue *GV,
  106. const TargetMachine &TM) const;
  107. virtual MCSection *getSectionForJumpTable(const Function &F,
  108. const TargetMachine &TM) const;
  109. virtual MCSection *getSectionForLSDA(const Function &F,
  110. const TargetMachine &TM) const {
  111. return LSDASection;
  112. }
  113. virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
  114. const Function &F) const;
  115. /// Targets should implement this method to assign a section to globals with
  116. /// an explicit section specfied. The implementation of this method can
  117. /// assume that GO->hasSection() is true.
  118. virtual MCSection *
  119. getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  120. const TargetMachine &TM) const = 0;
  121. /// Return an MCExpr to use for a reference to the specified global variable
  122. /// from exception handling information.
  123. virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
  124. unsigned Encoding,
  125. const TargetMachine &TM,
  126. MachineModuleInfo *MMI,
  127. MCStreamer &Streamer) const;
  128. /// Return the MCSymbol for a private symbol with global value name as its
  129. /// base, with the specified suffix.
  130. MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
  131. StringRef Suffix,
  132. const TargetMachine &TM) const;
  133. // The symbol that gets passed to .cfi_personality.
  134. virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
  135. const TargetMachine &TM,
  136. MachineModuleInfo *MMI) const;
  137. unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
  138. unsigned getLSDAEncoding() const { return LSDAEncoding; }
  139. unsigned getTTypeEncoding() const { return TTypeEncoding; }
  140. unsigned getCallSiteEncoding() const;
  141. const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
  142. MCStreamer &Streamer) const;
  143. virtual MCSection *getStaticCtorSection(unsigned Priority,
  144. const MCSymbol *KeySym) const {
  145. return StaticCtorSection;
  146. }
  147. virtual MCSection *getStaticDtorSection(unsigned Priority,
  148. const MCSymbol *KeySym) const {
  149. return StaticDtorSection;
  150. }
  151. /// Create a symbol reference to describe the given TLS variable when
  152. /// emitting the address in debug info.
  153. virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
  154. virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
  155. const GlobalValue *RHS,
  156. const TargetMachine &TM) const {
  157. return nullptr;
  158. }
  159. /// Target supports a native lowering of a dso_local_equivalent constant
  160. /// without needing to replace it with equivalent IR.
  161. bool supportDSOLocalEquivalentLowering() const {
  162. return SupportDSOLocalEquivalentLowering;
  163. }
  164. virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
  165. const TargetMachine &TM) const {
  166. return nullptr;
  167. }
  168. /// Target supports replacing a data "PC"-relative access to a symbol
  169. /// through another symbol, by accessing the later via a GOT entry instead?
  170. bool supportIndirectSymViaGOTPCRel() const {
  171. return SupportIndirectSymViaGOTPCRel;
  172. }
  173. /// Target GOT "PC"-relative relocation supports encoding an additional
  174. /// binary expression with an offset?
  175. bool supportGOTPCRelWithOffset() const {
  176. return SupportGOTPCRelWithOffset;
  177. }
  178. /// Target supports TLS offset relocation in debug section?
  179. bool supportDebugThreadLocalLocation() const {
  180. return SupportDebugThreadLocalLocation;
  181. }
  182. /// Get the target specific PC relative GOT entry relocation
  183. virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
  184. const MCSymbol *Sym,
  185. const MCValue &MV,
  186. int64_t Offset,
  187. MachineModuleInfo *MMI,
  188. MCStreamer &Streamer) const {
  189. return nullptr;
  190. }
  191. /// If supported, return the section to use for the llvm.commandline
  192. /// metadata. Otherwise, return nullptr.
  193. virtual MCSection *getSectionForCommandLines() const {
  194. return nullptr;
  195. }
  196. /// On targets that use separate function descriptor symbols, return a section
  197. /// for the descriptor given its symbol. Use only with defined functions.
  198. virtual MCSection *
  199. getSectionForFunctionDescriptor(const Function *F,
  200. const TargetMachine &TM) const {
  201. return nullptr;
  202. }
  203. /// On targets that support TOC entries, return a section for the entry given
  204. /// the symbol it refers to.
  205. /// TODO: Implement this interface for existing ELF targets.
  206. virtual MCSection *getSectionForTOCEntry(const MCSymbol *S,
  207. const TargetMachine &TM) const {
  208. return nullptr;
  209. }
  210. /// On targets that associate external references with a section, return such
  211. /// a section for the given external global.
  212. virtual MCSection *
  213. getSectionForExternalReference(const GlobalObject *GO,
  214. const TargetMachine &TM) const {
  215. return nullptr;
  216. }
  217. /// Targets that have a special convention for their symbols could use
  218. /// this hook to return a specialized symbol.
  219. virtual MCSymbol *getTargetSymbol(const GlobalValue *GV,
  220. const TargetMachine &TM) const {
  221. return nullptr;
  222. }
  223. /// If supported, return the function entry point symbol.
  224. /// Otherwise, returns nulltpr.
  225. /// Func must be a function or an alias which has a function as base object.
  226. virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
  227. const TargetMachine &TM) const {
  228. return nullptr;
  229. }
  230. protected:
  231. virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
  232. SectionKind Kind,
  233. const TargetMachine &TM) const = 0;
  234. };
  235. } // end namespace llvm
  236. #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H
  237. #ifdef __GNUC__
  238. #pragma GCC diagnostic pop
  239. #endif