TargetLoweringObjectFile.h 12 KB

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