CodeTemplate.cpp 3.8 KB

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