MCInstrDescView.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //===-- MCInstrDescView.h ---------------------------------------*- 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. /// \file
  10. /// Provide views around LLVM structures to represents an instruction instance,
  11. /// as well as its implicit and explicit arguments in a uniform way.
  12. /// Arguments that are explicit and independant (non tied) also have a Variable
  13. /// associated to them so the instruction can be fully defined by reading its
  14. /// Variables.
  15. ///
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H
  18. #define LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H
  19. #include <memory>
  20. #include <random>
  21. #include <unordered_map>
  22. #include "RegisterAliasing.h"
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/Optional.h"
  25. #include "llvm/MC/MCInst.h"
  26. #include "llvm/MC/MCInstrDesc.h"
  27. #include "llvm/MC/MCInstrInfo.h"
  28. namespace llvm {
  29. namespace exegesis {
  30. // A variable represents the value associated to an Operand or a set of Operands
  31. // if they are tied together.
  32. struct Variable {
  33. // Returns the index of this Variable inside Instruction's Variable.
  34. unsigned getIndex() const;
  35. // Returns the index of the Operand linked to this Variable.
  36. unsigned getPrimaryOperandIndex() const;
  37. // Returns whether this Variable has more than one Operand linked to it.
  38. bool hasTiedOperands() const;
  39. // The indices of the operands tied to this Variable.
  40. SmallVector<unsigned, 2> TiedOperands;
  41. // The index of this Variable in Instruction.Variables and its associated
  42. // Value in InstructionBuilder.VariableValues.
  43. Optional<uint8_t> Index;
  44. };
  45. // MCOperandInfo can only represents Explicit operands. This object gives a
  46. // uniform view of Implicit and Explicit Operands.
  47. // - Index: can be used to refer to MCInstrDesc::operands for Explicit operands.
  48. // - Tracker: is set for Register Operands and is used to keep track of possible
  49. // registers and the registers reachable from them (aliasing registers).
  50. // - Info: a shortcut for MCInstrDesc::operands()[Index].
  51. // - TiedToIndex: the index of the Operand holding the value or -1.
  52. // - ImplicitReg: a pointer to the register value when Operand is Implicit,
  53. // nullptr otherwise.
  54. // - VariableIndex: the index of the Variable holding the value for this Operand
  55. // or -1 if this operand is implicit.
  56. struct Operand {
  57. bool isExplicit() const;
  58. bool isImplicit() const;
  59. bool isImplicitReg() const;
  60. bool isDef() const;
  61. bool isUse() const;
  62. bool isReg() const;
  63. bool isTied() const;
  64. bool isVariable() const;
  65. bool isMemory() const;
  66. bool isImmediate() const;
  67. unsigned getIndex() const;
  68. unsigned getTiedToIndex() const;
  69. unsigned getVariableIndex() const;
  70. unsigned getImplicitReg() const;
  71. const RegisterAliasingTracker &getRegisterAliasing() const;
  72. const MCOperandInfo &getExplicitOperandInfo() const;
  73. // Please use the accessors above and not the following fields.
  74. Optional<uint8_t> Index;
  75. bool IsDef = false;
  76. const RegisterAliasingTracker *Tracker = nullptr; // Set for Register Op.
  77. const MCOperandInfo *Info = nullptr; // Set for Explicit Op.
  78. Optional<uint8_t> TiedToIndex; // Set for Reg&Explicit Op.
  79. const MCPhysReg *ImplicitReg = nullptr; // Set for Implicit Op.
  80. Optional<uint8_t> VariableIndex; // Set for Explicit Op.
  81. };
  82. /// A cache of BitVector to reuse between Instructions.
  83. /// The cache will only be exercised during Instruction initialization.
  84. /// For X86, this is ~160 unique vectors for all of the ~15K Instructions.
  85. struct BitVectorCache {
  86. // Finds or allocates the provided BitVector in the cache and retrieves it's
  87. // unique instance.
  88. const BitVector *getUnique(BitVector &&BV) const;
  89. private:
  90. mutable std::vector<std::unique_ptr<BitVector>> Cache;
  91. };
  92. // A view over an MCInstrDesc offering a convenient interface to compute
  93. // Register aliasing.
  94. struct Instruction {
  95. // Create an instruction for a particular Opcode.
  96. static std::unique_ptr<Instruction>
  97. create(const MCInstrInfo &InstrInfo, const RegisterAliasingTrackerCache &RATC,
  98. const BitVectorCache &BVC, unsigned Opcode);
  99. // Prevent copy or move, instructions are allocated once and cached.
  100. Instruction(const Instruction &) = delete;
  101. Instruction(Instruction &&) = delete;
  102. Instruction &operator=(const Instruction &) = delete;
  103. Instruction &operator=(Instruction &&) = delete;
  104. // Returns the Operand linked to this Variable.
  105. // In case the Variable is tied, the primary (i.e. Def) Operand is returned.
  106. const Operand &getPrimaryOperand(const Variable &Var) const;
  107. // Whether this instruction is self aliasing through its tied registers.
  108. // Repeating this instruction is guaranteed to executes sequentially.
  109. bool hasTiedRegisters() const;
  110. // Whether this instruction is self aliasing through its implicit registers.
  111. // Repeating this instruction is guaranteed to executes sequentially.
  112. bool hasAliasingImplicitRegisters() const;
  113. // Whether this instruction is self aliasing through some registers.
  114. // Repeating this instruction may execute sequentially by picking aliasing
  115. // Use and Def registers. It may also execute in parallel by picking non
  116. // aliasing Use and Def registers.
  117. bool hasAliasingRegisters(const BitVector &ForbiddenRegisters) const;
  118. // Whether this instruction's registers alias with OtherInstr's registers.
  119. bool hasAliasingRegistersThrough(const Instruction &OtherInstr,
  120. const BitVector &ForbiddenRegisters) const;
  121. // Returns whether this instruction has Memory Operands.
  122. // Repeating this instruction executes sequentially with an instruction that
  123. // reads or write the same memory region.
  124. bool hasMemoryOperands() const;
  125. // Returns whether this instruction as at least one use or one def.
  126. // Repeating this instruction may execute sequentially by adding an
  127. // instruction that aliases one of these.
  128. bool hasOneUseOrOneDef() const;
  129. // Convenient function to help with debugging.
  130. void dump(const MCRegisterInfo &RegInfo,
  131. const RegisterAliasingTrackerCache &RATC,
  132. raw_ostream &Stream) const;
  133. const MCInstrDesc &Description;
  134. const StringRef Name; // The name of this instruction.
  135. const SmallVector<Operand, 8> Operands;
  136. const SmallVector<Variable, 4> Variables;
  137. const BitVector &ImplDefRegs; // The set of aliased implicit def registers.
  138. const BitVector &ImplUseRegs; // The set of aliased implicit use registers.
  139. const BitVector &AllDefRegs; // The set of all aliased def registers.
  140. const BitVector &AllUseRegs; // The set of all aliased use registers.
  141. private:
  142. Instruction(const MCInstrDesc *Description, StringRef Name,
  143. SmallVector<Operand, 8> Operands,
  144. SmallVector<Variable, 4> Variables, const BitVector *ImplDefRegs,
  145. const BitVector *ImplUseRegs, const BitVector *AllDefRegs,
  146. const BitVector *AllUseRegs);
  147. };
  148. // Instructions are expensive to instantiate. This class provides a cache of
  149. // Instructions with lazy construction.
  150. struct InstructionsCache {
  151. InstructionsCache(const MCInstrInfo &InstrInfo,
  152. const RegisterAliasingTrackerCache &RATC);
  153. // Returns the Instruction object corresponding to this Opcode.
  154. const Instruction &getInstr(unsigned Opcode) const;
  155. private:
  156. const MCInstrInfo &InstrInfo;
  157. const RegisterAliasingTrackerCache &RATC;
  158. mutable std::unordered_map<unsigned, std::unique_ptr<Instruction>>
  159. Instructions;
  160. const BitVectorCache BVC;
  161. };
  162. // Represents the assignment of a Register to an Operand.
  163. struct RegisterOperandAssignment {
  164. RegisterOperandAssignment(const Operand *Operand, MCPhysReg Reg)
  165. : Op(Operand), Reg(Reg) {}
  166. const Operand *Op; // Pointer to an Explicit Register Operand.
  167. MCPhysReg Reg;
  168. bool operator==(const RegisterOperandAssignment &other) const;
  169. };
  170. // Represents a set of Operands that would alias through the use of some
  171. // Registers.
  172. // There are two reasons why operands would alias:
  173. // - The registers assigned to each of the operands are the same or alias each
  174. // other (e.g. AX/AL)
  175. // - The operands are tied.
  176. struct AliasingRegisterOperands {
  177. SmallVector<RegisterOperandAssignment, 1> Defs; // Unlikely size() > 1.
  178. SmallVector<RegisterOperandAssignment, 2> Uses;
  179. // True is Defs and Use contain an Implicit Operand.
  180. bool hasImplicitAliasing() const;
  181. bool operator==(const AliasingRegisterOperands &other) const;
  182. };
  183. // Returns all possible configurations leading Def registers of DefInstruction
  184. // to alias with Use registers of UseInstruction.
  185. struct AliasingConfigurations {
  186. AliasingConfigurations(const Instruction &DefInstruction,
  187. const Instruction &UseInstruction);
  188. bool empty() const; // True if no aliasing configuration is found.
  189. bool hasImplicitAliasing() const;
  190. SmallVector<AliasingRegisterOperands, 32> Configurations;
  191. };
  192. // Writes MCInst to OS.
  193. // This is not assembly but the internal LLVM's name for instructions and
  194. // registers.
  195. void DumpMCInst(const MCRegisterInfo &MCRegisterInfo,
  196. const MCInstrInfo &MCInstrInfo, const MCInst &MCInst,
  197. raw_ostream &OS);
  198. } // namespace exegesis
  199. } // namespace llvm
  200. #endif // LLVM_TOOLS_LLVM_EXEGESIS_MCINSTRDESCVIEW_H