CodeGenTarget.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines wrappers for the Target class and related global
  10. // functionality. This makes it easier to access the data and provides a single
  11. // place that needs to check it for validity. All of these classes abort
  12. // on error conditions.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
  16. #define LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
  17. #include "CodeGenHwModes.h"
  18. #include "CodeGenRegisters.h"
  19. #include "InfoByHwMode.h"
  20. #include "SDNodeProperties.h"
  21. namespace llvm {
  22. class RecordKeeper;
  23. class Record;
  24. class CodeGenInstruction;
  25. struct CodeGenRegister;
  26. class CodeGenSchedModels;
  27. class CodeGenTarget;
  28. /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
  29. /// record corresponds to.
  30. MVT::SimpleValueType getValueType(Record *Rec);
  31. StringRef getName(MVT::SimpleValueType T);
  32. StringRef getEnumName(MVT::SimpleValueType T);
  33. /// getQualifiedName - Return the name of the specified record, with a
  34. /// namespace qualifier if the record contains one.
  35. std::string getQualifiedName(const Record *R);
  36. /// CodeGenTarget - This class corresponds to the Target class in the .td files.
  37. ///
  38. class CodeGenTarget {
  39. RecordKeeper &Records;
  40. Record *TargetRec;
  41. mutable DenseMap<const Record*,
  42. std::unique_ptr<CodeGenInstruction>> Instructions;
  43. mutable std::unique_ptr<CodeGenRegBank> RegBank;
  44. mutable std::vector<Record*> RegAltNameIndices;
  45. mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
  46. CodeGenHwModes CGH;
  47. void ReadRegAltNameIndices() const;
  48. void ReadInstructions() const;
  49. void ReadLegalValueTypes() const;
  50. mutable std::unique_ptr<CodeGenSchedModels> SchedModels;
  51. mutable StringRef InstNamespace;
  52. mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
  53. mutable unsigned NumPseudoInstructions = 0;
  54. public:
  55. CodeGenTarget(RecordKeeper &Records);
  56. ~CodeGenTarget();
  57. Record *getTargetRecord() const { return TargetRec; }
  58. StringRef getName() const;
  59. /// getInstNamespace - Return the target-specific instruction namespace.
  60. ///
  61. StringRef getInstNamespace() const;
  62. /// getRegNamespace - Return the target-specific register namespace.
  63. StringRef getRegNamespace() const;
  64. /// getInstructionSet - Return the InstructionSet object.
  65. ///
  66. Record *getInstructionSet() const;
  67. /// getAllowRegisterRenaming - Return the AllowRegisterRenaming flag value for
  68. /// this target.
  69. ///
  70. bool getAllowRegisterRenaming() const;
  71. /// getAsmParser - Return the AssemblyParser definition for this target.
  72. ///
  73. Record *getAsmParser() const;
  74. /// getAsmParserVariant - Return the AssemblyParserVariant definition for
  75. /// this target.
  76. ///
  77. Record *getAsmParserVariant(unsigned i) const;
  78. /// getAsmParserVariantCount - Return the AssemblyParserVariant definition
  79. /// available for this target.
  80. ///
  81. unsigned getAsmParserVariantCount() const;
  82. /// getAsmWriter - Return the AssemblyWriter definition for this target.
  83. ///
  84. Record *getAsmWriter() const;
  85. /// getRegBank - Return the register bank description.
  86. CodeGenRegBank &getRegBank() const;
  87. /// Return the largest register class on \p RegBank which supports \p Ty and
  88. /// covers \p SubIdx if it exists.
  89. std::optional<CodeGenRegisterClass *>
  90. getSuperRegForSubReg(const ValueTypeByHwMode &Ty, CodeGenRegBank &RegBank,
  91. const CodeGenSubRegIndex *SubIdx,
  92. bool MustBeAllocatable = false) const;
  93. /// getRegisterByName - If there is a register with the specific AsmName,
  94. /// return it.
  95. const CodeGenRegister *getRegisterByName(StringRef Name) const;
  96. const std::vector<Record*> &getRegAltNameIndices() const {
  97. if (RegAltNameIndices.empty()) ReadRegAltNameIndices();
  98. return RegAltNameIndices;
  99. }
  100. const CodeGenRegisterClass &getRegisterClass(Record *R) const {
  101. return *getRegBank().getRegClass(R);
  102. }
  103. /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
  104. /// specified physical register.
  105. std::vector<ValueTypeByHwMode> getRegisterVTs(Record *R) const;
  106. ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const {
  107. if (LegalValueTypes.empty())
  108. ReadLegalValueTypes();
  109. return LegalValueTypes;
  110. }
  111. CodeGenSchedModels &getSchedModels() const;
  112. const CodeGenHwModes &getHwModes() const { return CGH; }
  113. private:
  114. DenseMap<const Record*, std::unique_ptr<CodeGenInstruction>> &
  115. getInstructions() const {
  116. if (Instructions.empty()) ReadInstructions();
  117. return Instructions;
  118. }
  119. public:
  120. CodeGenInstruction &getInstruction(const Record *InstRec) const {
  121. if (Instructions.empty()) ReadInstructions();
  122. auto I = Instructions.find(InstRec);
  123. assert(I != Instructions.end() && "Not an instruction");
  124. return *I->second;
  125. }
  126. /// Returns the number of predefined instructions.
  127. static unsigned getNumFixedInstructions();
  128. /// Returns the number of pseudo instructions.
  129. unsigned getNumPseudoInstructions() const {
  130. if (InstrsByEnum.empty())
  131. ComputeInstrsByEnum();
  132. return NumPseudoInstructions;
  133. }
  134. /// Return all of the instructions defined by the target, ordered by their
  135. /// enum value.
  136. /// The following order of instructions is also guaranteed:
  137. /// - fixed / generic instructions as declared in TargetOpcodes.def, in order;
  138. /// - pseudo instructions in lexicographical order sorted by name;
  139. /// - other instructions in lexicographical order sorted by name.
  140. ArrayRef<const CodeGenInstruction *> getInstructionsByEnumValue() const {
  141. if (InstrsByEnum.empty())
  142. ComputeInstrsByEnum();
  143. return InstrsByEnum;
  144. }
  145. typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator;
  146. inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
  147. inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
  148. /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
  149. ///
  150. bool isLittleEndianEncoding() const;
  151. /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
  152. /// encodings, reverse the bit order of all instructions.
  153. void reverseBitsForLittleEndianEncoding();
  154. /// guessInstructionProperties - should we just guess unset instruction
  155. /// properties?
  156. bool guessInstructionProperties() const;
  157. private:
  158. void ComputeInstrsByEnum() const;
  159. };
  160. /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
  161. /// tablegen class in TargetSelectionDAG.td
  162. class ComplexPattern {
  163. Record *Ty;
  164. unsigned NumOperands;
  165. std::string SelectFunc;
  166. std::vector<Record*> RootNodes;
  167. unsigned Properties; // Node properties
  168. unsigned Complexity;
  169. public:
  170. ComplexPattern(Record *R);
  171. Record *getValueType() const { return Ty; }
  172. unsigned getNumOperands() const { return NumOperands; }
  173. const std::string &getSelectFunc() const { return SelectFunc; }
  174. const std::vector<Record*> &getRootNodes() const {
  175. return RootNodes;
  176. }
  177. bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
  178. unsigned getComplexity() const { return Complexity; }
  179. };
  180. } // End llvm namespace
  181. #endif