TargetMachine.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the TargetMachine and LLVMTargetMachine classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TARGET_TARGETMACHINE_H
  14. #define LLVM_TARGET_TARGETMACHINE_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/IR/DataLayout.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/Support/CodeGen.h"
  20. #include "llvm/Target/TargetOptions.h"
  21. #include <cassert>
  22. #include <string>
  23. namespace llvm {
  24. class InstrItineraryData;
  25. class GlobalValue;
  26. class Mangler;
  27. class MachineFunctionInitializer;
  28. class MCAsmInfo;
  29. class MCCodeGenInfo;
  30. class MCContext;
  31. class MCInstrInfo;
  32. class MCRegisterInfo;
  33. class MCSubtargetInfo;
  34. class MCSymbol;
  35. class Target;
  36. class DataLayout;
  37. class TargetLibraryInfo;
  38. class TargetFrameLowering;
  39. class TargetIRAnalysis;
  40. class TargetIntrinsicInfo;
  41. class TargetLowering;
  42. class TargetPassConfig;
  43. class TargetRegisterInfo;
  44. class TargetSelectionDAGInfo;
  45. class TargetSubtargetInfo;
  46. class TargetTransformInfo;
  47. class formatted_raw_ostream;
  48. class raw_ostream;
  49. class raw_pwrite_stream;
  50. class TargetLoweringObjectFile;
  51. // The old pass manager infrastructure is hidden in a legacy namespace now.
  52. namespace legacy {
  53. class PassManagerBase;
  54. }
  55. using legacy::PassManagerBase;
  56. // //
  57. ///////////////////////////////////////////////////////////////////////////////
  58. ///
  59. /// Primary interface to the complete machine description for the target
  60. /// machine. All target-specific information should be accessible through this
  61. /// interface.
  62. ///
  63. class TargetMachine {
  64. TargetMachine(const TargetMachine &) = delete;
  65. void operator=(const TargetMachine &) = delete;
  66. protected: // Can only create subclasses.
  67. TargetMachine(const Target &T, StringRef DataLayoutString,
  68. const Triple &TargetTriple, StringRef CPU, StringRef FS,
  69. const TargetOptions &Options);
  70. /// The Target that this machine was created for.
  71. const Target &TheTarget;
  72. /// For ABI type size and alignment.
  73. const DataLayout DL;
  74. /// Triple string, CPU name, and target feature strings the TargetMachine
  75. /// instance is created with.
  76. Triple TargetTriple;
  77. std::string TargetCPU;
  78. std::string TargetFS;
  79. /// Low level target information such as relocation model. Non-const to
  80. /// allow resetting optimization level per-function.
  81. MCCodeGenInfo *CodeGenInfo;
  82. /// Contains target specific asm information.
  83. const MCAsmInfo *AsmInfo;
  84. const MCRegisterInfo *MRI;
  85. const MCInstrInfo *MII;
  86. const MCSubtargetInfo *STI;
  87. unsigned RequireStructuredCFG : 1;
  88. public:
  89. mutable TargetOptions Options;
  90. virtual ~TargetMachine();
  91. const Target &getTarget() const { return TheTarget; }
  92. const Triple &getTargetTriple() const { return TargetTriple; }
  93. StringRef getTargetCPU() const { return TargetCPU; }
  94. StringRef getTargetFeatureString() const { return TargetFS; }
  95. /// Virtual method implemented by subclasses that returns a reference to that
  96. /// target's TargetSubtargetInfo-derived member variable.
  97. virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
  98. return nullptr;
  99. }
  100. virtual TargetLoweringObjectFile *getObjFileLowering() const {
  101. return nullptr;
  102. }
  103. /// This method returns a pointer to the specified type of
  104. /// TargetSubtargetInfo. In debug builds, it verifies that the object being
  105. /// returned is of the correct type.
  106. template <typename STC> const STC &getSubtarget(const Function &F) const {
  107. return *static_cast<const STC*>(getSubtargetImpl(F));
  108. }
  109. /// Deprecated in 3.7, will be removed in 3.8. Use createDataLayout() instead.
  110. ///
  111. /// This method returns a pointer to the DataLayout for the target. It should
  112. /// be unchanging for every subtarget.
  113. const DataLayout *getDataLayout() const { return &DL; }
  114. /// Create a DataLayout.
  115. const DataLayout createDataLayout() const { return DL; }
  116. /// \brief Reset the target options based on the function's attributes.
  117. // FIXME: Remove TargetOptions that affect per-function code generation
  118. // from TargetMachine.
  119. void resetTargetOptions(const Function &F) const;
  120. /// Return target specific asm information.
  121. const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
  122. const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
  123. const MCInstrInfo *getMCInstrInfo() const { return MII; }
  124. const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
  125. /// If intrinsic information is available, return it. If not, return null.
  126. virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
  127. return nullptr;
  128. }
  129. bool requiresStructuredCFG() const { return RequireStructuredCFG; }
  130. void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
  131. /// Returns the code generation relocation model. The choices are static, PIC,
  132. /// and dynamic-no-pic, and target default.
  133. Reloc::Model getRelocationModel() const;
  134. /// Returns the code model. The choices are small, kernel, medium, large, and
  135. /// target default.
  136. CodeModel::Model getCodeModel() const;
  137. /// Returns the TLS model which should be used for the given global variable.
  138. TLSModel::Model getTLSModel(const GlobalValue *GV) const;
  139. /// Returns the optimization level: None, Less, Default, or Aggressive.
  140. CodeGenOpt::Level getOptLevel() const;
  141. /// \brief Overrides the optimization level.
  142. void setOptLevel(CodeGenOpt::Level Level) const;
  143. void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
  144. bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
  145. /// Returns the default value of asm verbosity.
  146. ///
  147. bool getAsmVerbosityDefault() const {
  148. return Options.MCOptions.AsmVerbose;
  149. }
  150. bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
  151. /// Return true if data objects should be emitted into their own section,
  152. /// corresponds to -fdata-sections.
  153. bool getDataSections() const {
  154. return Options.DataSections;
  155. }
  156. /// Return true if functions should be emitted into their own section,
  157. /// corresponding to -ffunction-sections.
  158. bool getFunctionSections() const {
  159. return Options.FunctionSections;
  160. }
  161. /// \brief Get a \c TargetIRAnalysis appropriate for the target.
  162. ///
  163. /// This is used to construct the new pass manager's target IR analysis pass,
  164. /// set up appropriately for this target machine. Even the old pass manager
  165. /// uses this to answer queries about the IR.
  166. virtual TargetIRAnalysis getTargetIRAnalysis();
  167. /// These enums are meant to be passed into addPassesToEmitFile to indicate
  168. /// what type of file to emit, and returned by it to indicate what type of
  169. /// file could actually be made.
  170. enum CodeGenFileType {
  171. CGFT_AssemblyFile,
  172. CGFT_ObjectFile,
  173. CGFT_Null // Do not emit any output.
  174. };
  175. /// Add passes to the specified pass manager to get the specified file
  176. /// emitted. Typically this will involve several steps of code generation.
  177. /// This method should return true if emission of this file type is not
  178. /// supported, or false on success.
  179. virtual bool addPassesToEmitFile(
  180. PassManagerBase &, raw_pwrite_stream &, CodeGenFileType,
  181. bool /*DisableVerify*/ = true, AnalysisID /*StartBefore*/ = nullptr,
  182. AnalysisID /*StartAfter*/ = nullptr, AnalysisID /*StopAfter*/ = nullptr,
  183. MachineFunctionInitializer * /*MFInitializer*/ = nullptr) {
  184. return true;
  185. }
  186. /// Add passes to the specified pass manager to get machine code emitted with
  187. /// the MCJIT. This method returns true if machine code is not supported. It
  188. /// fills the MCContext Ctx pointer which can be used to build custom
  189. /// MCStreamer.
  190. ///
  191. virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
  192. raw_pwrite_stream &,
  193. bool /*DisableVerify*/ = true) {
  194. return true;
  195. }
  196. void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
  197. Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
  198. MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
  199. };
  200. /// This class describes a target machine that is implemented with the LLVM
  201. /// target-independent code generator.
  202. ///
  203. class LLVMTargetMachine : public TargetMachine {
  204. protected: // Can only create subclasses.
  205. LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
  206. const Triple &TargetTriple, StringRef CPU, StringRef FS,
  207. TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
  208. CodeGenOpt::Level OL);
  209. void initAsmInfo();
  210. public:
  211. /// \brief Get a TargetIRAnalysis implementation for the target.
  212. ///
  213. /// This analysis will produce a TTI result which uses the common code
  214. /// generator to answer queries about the IR.
  215. TargetIRAnalysis getTargetIRAnalysis() override;
  216. /// Create a pass configuration object to be used by addPassToEmitX methods
  217. /// for generating a pipeline of CodeGen passes.
  218. virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
  219. /// Add passes to the specified pass manager to get the specified file
  220. /// emitted. Typically this will involve several steps of code generation.
  221. bool addPassesToEmitFile(
  222. PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
  223. bool DisableVerify = true, AnalysisID StartBefore = nullptr,
  224. AnalysisID StartAfter = nullptr, AnalysisID StopAfter = nullptr,
  225. MachineFunctionInitializer *MFInitializer = nullptr) override;
  226. /// Add passes to the specified pass manager to get machine code emitted with
  227. /// the MCJIT. This method returns true if machine code is not supported. It
  228. /// fills the MCContext Ctx pointer which can be used to build custom
  229. /// MCStreamer.
  230. bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
  231. raw_pwrite_stream &OS,
  232. bool DisableVerify = true) override;
  233. };
  234. } // End llvm namespace
  235. #endif