CodeTemplate.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===-- CodeTemplate.cpp ----------------------------------------*- 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. #include "CodeTemplate.h"
  9. namespace llvm {
  10. namespace exegesis {
  11. CodeTemplate::CodeTemplate(const CodeTemplate &) = default;
  12. CodeTemplate::CodeTemplate(CodeTemplate &&) = default;
  13. CodeTemplate &CodeTemplate::operator=(CodeTemplate &&) = default;
  14. CodeTemplate &CodeTemplate::operator=(const CodeTemplate &) = default;
  15. CodeTemplate CodeTemplate::clone() const {
  16. CodeTemplate CT = *this;
  17. return CT;
  18. }
  19. InstructionTemplate::InstructionTemplate(const Instruction *Instr)
  20. : Instr(Instr), VariableValues(Instr->Variables.size()) {}
  21. InstructionTemplate::InstructionTemplate(InstructionTemplate &&) = default;
  22. InstructionTemplate &InstructionTemplate::
  23. operator=(InstructionTemplate &&) = default;
  24. InstructionTemplate::InstructionTemplate(const InstructionTemplate &) = default;
  25. InstructionTemplate &InstructionTemplate::
  26. operator=(const InstructionTemplate &) = default;
  27. unsigned InstructionTemplate::getOpcode() const {
  28. return Instr->Description.getOpcode();
  29. }
  30. MCOperand &InstructionTemplate::getValueFor(const Variable &Var) {
  31. return VariableValues[Var.getIndex()];
  32. }
  33. const MCOperand &InstructionTemplate::getValueFor(const Variable &Var) const {
  34. return VariableValues[Var.getIndex()];
  35. }
  36. MCOperand &InstructionTemplate::getValueFor(const Operand &Op) {
  37. return getValueFor(Instr->Variables[Op.getVariableIndex()]);
  38. }
  39. const MCOperand &InstructionTemplate::getValueFor(const Operand &Op) const {
  40. return getValueFor(Instr->Variables[Op.getVariableIndex()]);
  41. }
  42. bool InstructionTemplate::hasImmediateVariables() const {
  43. return any_of(Instr->Variables, [this](const Variable &Var) {
  44. return Instr->getPrimaryOperand(Var).isImmediate();
  45. });
  46. }
  47. MCInst InstructionTemplate::build() const {
  48. MCInst Result;
  49. Result.setOpcode(Instr->Description.Opcode);
  50. for (const auto &Op : Instr->Operands)
  51. if (Op.isExplicit())
  52. Result.addOperand(getValueFor(Op));
  53. return Result;
  54. }
  55. bool isEnumValue(ExecutionMode Execution) {
  56. return isPowerOf2_32(static_cast<uint32_t>(Execution));
  57. }
  58. StringRef getName(ExecutionMode Bit) {
  59. assert(isEnumValue(Bit) && "Bit must be a power of two");
  60. switch (Bit) {
  61. case ExecutionMode::UNKNOWN:
  62. return "UNKNOWN";
  63. case ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS:
  64. return "ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS";
  65. case ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS:
  66. return "ALWAYS_SERIAL_TIED_REGS_ALIAS";
  67. case ExecutionMode::SERIAL_VIA_MEMORY_INSTR:
  68. return "SERIAL_VIA_MEMORY_INSTR";
  69. case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS:
  70. return "SERIAL_VIA_EXPLICIT_REGS";
  71. case ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR:
  72. return "SERIAL_VIA_NON_MEMORY_INSTR";
  73. case ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF:
  74. return "ALWAYS_PARALLEL_MISSING_USE_OR_DEF";
  75. case ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS:
  76. return "PARALLEL_VIA_EXPLICIT_REGS";
  77. }
  78. llvm_unreachable("Missing enum case");
  79. }
  80. ArrayRef<ExecutionMode> getAllExecutionBits() {
  81. static const ExecutionMode kAllExecutionModeBits[] = {
  82. ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS,
  83. ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS,
  84. ExecutionMode::SERIAL_VIA_MEMORY_INSTR,
  85. ExecutionMode::SERIAL_VIA_EXPLICIT_REGS,
  86. ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR,
  87. ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF,
  88. ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS,
  89. };
  90. return ArrayRef(kAllExecutionModeBits);
  91. }
  92. SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode Execution) {
  93. SmallVector<ExecutionMode, 4> Result;
  94. for (const auto Bit : getAllExecutionBits())
  95. if ((Execution & Bit) == Bit)
  96. Result.push_back(Bit);
  97. return Result;
  98. }
  99. } // namespace exegesis
  100. } // namespace llvm