CodeGenTarget.h 7.4 KB

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