CodeGenTarget.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //===- CodeGenTarget.h - Target Class Wrapper -------------------*- 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 wrappers for the Target class and related global
  11. // functionality. This makes it easier to access the data and provides a single
  12. // place that needs to check it for validity. All of these classes abort
  13. // on error conditions.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
  17. #define LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
  18. #include "CodeGenInstruction.h"
  19. #include "CodeGenRegisters.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "llvm/TableGen/Record.h"
  22. #include <algorithm>
  23. namespace llvm {
  24. struct CodeGenRegister;
  25. class CodeGenSchedModels;
  26. class CodeGenTarget;
  27. // SelectionDAG node properties.
  28. // SDNPMemOperand: indicates that a node touches memory and therefore must
  29. // have an associated memory operand that describes the access.
  30. enum SDNP {
  31. SDNPCommutative,
  32. SDNPAssociative,
  33. SDNPHasChain,
  34. SDNPOutGlue,
  35. SDNPInGlue,
  36. SDNPOptInGlue,
  37. SDNPMayLoad,
  38. SDNPMayStore,
  39. SDNPSideEffect,
  40. SDNPMemOperand,
  41. SDNPVariadic,
  42. SDNPWantRoot,
  43. SDNPWantParent
  44. };
  45. /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
  46. /// record corresponds to.
  47. MVT::SimpleValueType getValueType(Record *Rec);
  48. std::string getName(MVT::SimpleValueType T);
  49. std::string getEnumName(MVT::SimpleValueType T);
  50. /// getQualifiedName - Return the name of the specified record, with a
  51. /// namespace qualifier if the record contains one.
  52. std::string getQualifiedName(const Record *R);
  53. /// CodeGenTarget - This class corresponds to the Target class in the .td files.
  54. ///
  55. class CodeGenTarget {
  56. RecordKeeper &Records;
  57. Record *TargetRec;
  58. mutable DenseMap<const Record*,
  59. std::unique_ptr<CodeGenInstruction>> Instructions;
  60. mutable std::unique_ptr<CodeGenRegBank> RegBank;
  61. mutable std::vector<Record*> RegAltNameIndices;
  62. mutable SmallVector<MVT::SimpleValueType, 8> LegalValueTypes;
  63. void ReadRegAltNameIndices() const;
  64. void ReadInstructions() const;
  65. void ReadLegalValueTypes() const;
  66. mutable std::unique_ptr<CodeGenSchedModels> SchedModels;
  67. mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
  68. public:
  69. CodeGenTarget(RecordKeeper &Records);
  70. ~CodeGenTarget();
  71. Record *getTargetRecord() const { return TargetRec; }
  72. const std::string &getName() const;
  73. /// getInstNamespace - Return the target-specific instruction namespace.
  74. ///
  75. std::string getInstNamespace() const;
  76. /// getInstructionSet - Return the InstructionSet object.
  77. ///
  78. Record *getInstructionSet() const;
  79. /// getAsmParser - Return the AssemblyParser definition for this target.
  80. ///
  81. Record *getAsmParser() const;
  82. /// getAsmParserVariant - Return the AssmblyParserVariant definition for
  83. /// this target.
  84. ///
  85. Record *getAsmParserVariant(unsigned i) const;
  86. /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
  87. /// available for this target.
  88. ///
  89. unsigned getAsmParserVariantCount() const;
  90. /// getAsmWriter - Return the AssemblyWriter definition for this target.
  91. ///
  92. Record *getAsmWriter() const;
  93. /// getRegBank - Return the register bank description.
  94. CodeGenRegBank &getRegBank() const;
  95. /// getRegisterByName - If there is a register with the specific AsmName,
  96. /// return it.
  97. const CodeGenRegister *getRegisterByName(StringRef Name) const;
  98. const std::vector<Record*> &getRegAltNameIndices() const {
  99. if (RegAltNameIndices.empty()) ReadRegAltNameIndices();
  100. return RegAltNameIndices;
  101. }
  102. const CodeGenRegisterClass &getRegisterClass(Record *R) const {
  103. return *getRegBank().getRegClass(R);
  104. }
  105. /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
  106. /// specified physical register.
  107. std::vector<MVT::SimpleValueType> getRegisterVTs(Record *R) const;
  108. ArrayRef<MVT::SimpleValueType> getLegalValueTypes() const {
  109. if (LegalValueTypes.empty()) ReadLegalValueTypes();
  110. return LegalValueTypes;
  111. }
  112. /// isLegalValueType - Return true if the specified value type is natively
  113. /// supported by the target (i.e. there are registers that directly hold it).
  114. bool isLegalValueType(MVT::SimpleValueType VT) const {
  115. ArrayRef<MVT::SimpleValueType> LegalVTs = getLegalValueTypes();
  116. for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i)
  117. if (LegalVTs[i] == VT) return true;
  118. return false;
  119. }
  120. CodeGenSchedModels &getSchedModels() const;
  121. private:
  122. DenseMap<const Record*, std::unique_ptr<CodeGenInstruction>> &
  123. getInstructions() const {
  124. if (Instructions.empty()) ReadInstructions();
  125. return Instructions;
  126. }
  127. public:
  128. CodeGenInstruction &getInstruction(const Record *InstRec) const {
  129. if (Instructions.empty()) ReadInstructions();
  130. auto I = Instructions.find(InstRec);
  131. assert(I != Instructions.end() && "Not an instruction");
  132. return *I->second;
  133. }
  134. /// getInstructionsByEnumValue - Return all of the instructions defined by the
  135. /// target, ordered by their enum value.
  136. const std::vector<const CodeGenInstruction*> &
  137. getInstructionsByEnumValue() const {
  138. if (InstrsByEnum.empty()) ComputeInstrsByEnum();
  139. return InstrsByEnum;
  140. }
  141. typedef std::vector<const CodeGenInstruction*>::const_iterator inst_iterator;
  142. inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
  143. inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
  144. iterator_range<inst_iterator> instructions() const {
  145. return iterator_range<inst_iterator>(inst_begin(), inst_end());
  146. }
  147. /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
  148. ///
  149. bool isLittleEndianEncoding() const;
  150. /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
  151. /// encodings, reverse the bit order of all instructions.
  152. void reverseBitsForLittleEndianEncoding();
  153. /// guessInstructionProperties - should we just guess unset instruction
  154. /// properties?
  155. bool guessInstructionProperties() const;
  156. private:
  157. void ComputeInstrsByEnum() const;
  158. };
  159. /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
  160. /// tablegen class in TargetSelectionDAG.td
  161. class ComplexPattern {
  162. MVT::SimpleValueType Ty;
  163. unsigned NumOperands;
  164. std::string SelectFunc;
  165. std::vector<Record*> RootNodes;
  166. unsigned Properties; // Node properties
  167. public:
  168. ComplexPattern() : NumOperands(0) {}
  169. ComplexPattern(Record *R);
  170. MVT::SimpleValueType getValueType() const { return Ty; }
  171. unsigned getNumOperands() const { return NumOperands; }
  172. const std::string &getSelectFunc() const { return SelectFunc; }
  173. const std::vector<Record*> &getRootNodes() const {
  174. return RootNodes;
  175. }
  176. bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
  177. };
  178. } // End llvm namespace
  179. #endif