MIParser.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MIParser.h - Machine Instructions Parser -----------------*- 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 declares the function that parses the machine instructions.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_MIRPARSER_MIPARSER_H
  18. #define LLVM_CODEGEN_MIRPARSER_MIPARSER_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/CodeGen/MachineMemOperand.h"
  22. #include "llvm/CodeGen/Register.h"
  23. #include "llvm/Support/Allocator.h"
  24. #include "llvm/Support/SMLoc.h"
  25. #include <utility>
  26. namespace llvm {
  27. class MachineBasicBlock;
  28. class MachineFunction;
  29. class MDNode;
  30. class RegisterBank;
  31. struct SlotMapping;
  32. class SMDiagnostic;
  33. class SourceMgr;
  34. class StringRef;
  35. class TargetRegisterClass;
  36. class TargetSubtargetInfo;
  37. struct VRegInfo {
  38. enum uint8_t {
  39. UNKNOWN, NORMAL, GENERIC, REGBANK
  40. } Kind = UNKNOWN;
  41. bool Explicit = false; ///< VReg was explicitly specified in the .mir file.
  42. union {
  43. const TargetRegisterClass *RC;
  44. const RegisterBank *RegBank;
  45. } D;
  46. Register VReg;
  47. Register PreferredReg;
  48. };
  49. using Name2RegClassMap = StringMap<const TargetRegisterClass *>;
  50. using Name2RegBankMap = StringMap<const RegisterBank *>;
  51. struct PerTargetMIParsingState {
  52. private:
  53. const TargetSubtargetInfo &Subtarget;
  54. /// Maps from instruction names to op codes.
  55. StringMap<unsigned> Names2InstrOpCodes;
  56. /// Maps from register names to registers.
  57. StringMap<Register> Names2Regs;
  58. /// Maps from register mask names to register masks.
  59. StringMap<const uint32_t *> Names2RegMasks;
  60. /// Maps from subregister names to subregister indices.
  61. StringMap<unsigned> Names2SubRegIndices;
  62. /// Maps from target index names to target indices.
  63. StringMap<int> Names2TargetIndices;
  64. /// Maps from direct target flag names to the direct target flag values.
  65. StringMap<unsigned> Names2DirectTargetFlags;
  66. /// Maps from direct target flag names to the bitmask target flag values.
  67. StringMap<unsigned> Names2BitmaskTargetFlags;
  68. /// Maps from MMO target flag names to MMO target flag values.
  69. StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
  70. /// Maps from register class names to register classes.
  71. Name2RegClassMap Names2RegClasses;
  72. /// Maps from register bank names to register banks.
  73. Name2RegBankMap Names2RegBanks;
  74. void initNames2InstrOpCodes();
  75. void initNames2Regs();
  76. void initNames2RegMasks();
  77. void initNames2SubRegIndices();
  78. void initNames2TargetIndices();
  79. void initNames2DirectTargetFlags();
  80. void initNames2BitmaskTargetFlags();
  81. void initNames2MMOTargetFlags();
  82. void initNames2RegClasses();
  83. void initNames2RegBanks();
  84. public:
  85. /// Try to convert an instruction name to an opcode. Return true if the
  86. /// instruction name is invalid.
  87. bool parseInstrName(StringRef InstrName, unsigned &OpCode);
  88. /// Try to convert a register name to a register number. Return true if the
  89. /// register name is invalid.
  90. bool getRegisterByName(StringRef RegName, Register &Reg);
  91. /// Check if the given identifier is a name of a register mask.
  92. ///
  93. /// Return null if the identifier isn't a register mask.
  94. const uint32_t *getRegMask(StringRef Identifier);
  95. /// Check if the given identifier is a name of a subregister index.
  96. ///
  97. /// Return 0 if the name isn't a subregister index class.
  98. unsigned getSubRegIndex(StringRef Name);
  99. /// Try to convert a name of target index to the corresponding target index.
  100. ///
  101. /// Return true if the name isn't a name of a target index.
  102. bool getTargetIndex(StringRef Name, int &Index);
  103. /// Try to convert a name of a direct target flag to the corresponding
  104. /// target flag.
  105. ///
  106. /// Return true if the name isn't a name of a direct flag.
  107. bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
  108. /// Try to convert a name of a bitmask target flag to the corresponding
  109. /// target flag.
  110. ///
  111. /// Return true if the name isn't a name of a bitmask target flag.
  112. bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
  113. /// Try to convert a name of a MachineMemOperand target flag to the
  114. /// corresponding target flag.
  115. ///
  116. /// Return true if the name isn't a name of a target MMO flag.
  117. bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag);
  118. /// Check if the given identifier is a name of a register class.
  119. ///
  120. /// Return null if the name isn't a register class.
  121. const TargetRegisterClass *getRegClass(StringRef Name);
  122. /// Check if the given identifier is a name of a register bank.
  123. ///
  124. /// Return null if the name isn't a register bank.
  125. const RegisterBank *getRegBank(StringRef Name);
  126. PerTargetMIParsingState(const TargetSubtargetInfo &STI)
  127. : Subtarget(STI) {
  128. initNames2RegClasses();
  129. initNames2RegBanks();
  130. }
  131. ~PerTargetMIParsingState() = default;
  132. void setTarget(const TargetSubtargetInfo &NewSubtarget);
  133. };
  134. struct PerFunctionMIParsingState {
  135. BumpPtrAllocator Allocator;
  136. MachineFunction &MF;
  137. SourceMgr *SM;
  138. const SlotMapping &IRSlots;
  139. PerTargetMIParsingState &Target;
  140. std::map<unsigned, TrackingMDNodeRef> MachineMetadataNodes;
  141. std::map<unsigned, std::pair<TempMDTuple, SMLoc>> MachineForwardRefMDNodes;
  142. DenseMap<unsigned, MachineBasicBlock *> MBBSlots;
  143. DenseMap<Register, VRegInfo *> VRegInfos;
  144. StringMap<VRegInfo *> VRegInfosNamed;
  145. DenseMap<unsigned, int> FixedStackObjectSlots;
  146. DenseMap<unsigned, int> StackObjectSlots;
  147. DenseMap<unsigned, unsigned> ConstantPoolSlots;
  148. DenseMap<unsigned, unsigned> JumpTableSlots;
  149. /// Maps from slot numbers to function's unnamed values.
  150. DenseMap<unsigned, const Value *> Slots2Values;
  151. PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM,
  152. const SlotMapping &IRSlots,
  153. PerTargetMIParsingState &Target);
  154. VRegInfo &getVRegInfo(Register Num);
  155. VRegInfo &getVRegInfoNamed(StringRef RegName);
  156. const Value *getIRValue(unsigned Slot);
  157. };
  158. /// Parse the machine basic block definitions, and skip the machine
  159. /// instructions.
  160. ///
  161. /// This function runs the first parsing pass on the machine function's body.
  162. /// It parses only the machine basic block definitions and creates the machine
  163. /// basic blocks in the given machine function.
  164. ///
  165. /// The machine instructions aren't parsed during the first pass because all
  166. /// the machine basic blocks aren't defined yet - this makes it impossible to
  167. /// resolve the machine basic block references.
  168. ///
  169. /// Return true if an error occurred.
  170. bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
  171. StringRef Src, SMDiagnostic &Error);
  172. /// Parse the machine instructions.
  173. ///
  174. /// This function runs the second parsing pass on the machine function's body.
  175. /// It skips the machine basic block definitions and parses only the machine
  176. /// instructions and basic block attributes like liveins and successors.
  177. ///
  178. /// The second parsing pass assumes that the first parsing pass already ran
  179. /// on the given source string.
  180. ///
  181. /// Return true if an error occurred.
  182. bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src,
  183. SMDiagnostic &Error);
  184. bool parseMBBReference(PerFunctionMIParsingState &PFS,
  185. MachineBasicBlock *&MBB, StringRef Src,
  186. SMDiagnostic &Error);
  187. bool parseRegisterReference(PerFunctionMIParsingState &PFS,
  188. Register &Reg, StringRef Src,
  189. SMDiagnostic &Error);
  190. bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg,
  191. StringRef Src, SMDiagnostic &Error);
  192. bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
  193. VRegInfo *&Info, StringRef Src,
  194. SMDiagnostic &Error);
  195. bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI,
  196. StringRef Src, SMDiagnostic &Error);
  197. bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src,
  198. SMDiagnostic &Error);
  199. bool parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src,
  200. SMRange SourceRange, SMDiagnostic &Error);
  201. } // end namespace llvm
  202. #endif // LLVM_CODEGEN_MIRPARSER_MIPARSER_H
  203. #ifdef __GNUC__
  204. #pragma GCC diagnostic pop
  205. #endif