TargetLoweringObjectFile.h 12 KB

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