CodeTemplate.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===-- CodeTemplate.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. /// A set of structures and functions to craft instructions for the
  11. /// SnippetGenerator.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
  15. #define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
  16. #include "MCInstrDescView.h"
  17. #include "llvm/ADT/BitmaskEnum.h"
  18. namespace llvm {
  19. namespace exegesis {
  20. // A template for an Instruction holding values for each of its Variables.
  21. struct InstructionTemplate {
  22. InstructionTemplate(const Instruction *Instr);
  23. InstructionTemplate(const InstructionTemplate &); // default
  24. InstructionTemplate &operator=(const InstructionTemplate &); // default
  25. InstructionTemplate(InstructionTemplate &&); // default
  26. InstructionTemplate &operator=(InstructionTemplate &&); // default
  27. unsigned getOpcode() const;
  28. MCOperand &getValueFor(const Variable &Var);
  29. const MCOperand &getValueFor(const Variable &Var) const;
  30. MCOperand &getValueFor(const Operand &Op);
  31. const MCOperand &getValueFor(const Operand &Op) const;
  32. bool hasImmediateVariables() const;
  33. const Instruction &getInstr() const { return *Instr; }
  34. ArrayRef<MCOperand> getVariableValues() const { return VariableValues; }
  35. void setVariableValues(ArrayRef<MCOperand> NewVariableValues) {
  36. assert(VariableValues.size() == NewVariableValues.size() &&
  37. "Value count mismatch");
  38. VariableValues.assign(NewVariableValues.begin(), NewVariableValues.end());
  39. }
  40. // Builds an MCInst from this InstructionTemplate setting its operands
  41. // to the corresponding variable values. Precondition: All VariableValues must
  42. // be set.
  43. MCInst build() const;
  44. private:
  45. const Instruction *Instr;
  46. SmallVector<MCOperand, 4> VariableValues;
  47. };
  48. enum class ExecutionMode : uint8_t {
  49. UNKNOWN = 0U,
  50. // The instruction is always serial because implicit Use and Def alias.
  51. // e.g. AAA (alias via EFLAGS)
  52. ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS = 1u << 0,
  53. // The instruction is always serial because one Def is tied to a Use.
  54. // e.g. AND32ri (alias via tied GR32)
  55. ALWAYS_SERIAL_TIED_REGS_ALIAS = 1u << 1,
  56. // The execution can be made serial by inserting a second instruction that
  57. // clobbers/reads memory.
  58. // e.g. MOV8rm
  59. SERIAL_VIA_MEMORY_INSTR = 1u << 2,
  60. // The execution can be made serial by picking one Def that aliases with one
  61. // Use.
  62. // e.g. VXORPSrr XMM1, XMM1, XMM2
  63. SERIAL_VIA_EXPLICIT_REGS = 1u << 3,
  64. // The execution can be made serial by inserting a second instruction that
  65. // uses one of the Defs and defs one of the Uses.
  66. // e.g.
  67. // 1st instruction: MMX_PMOVMSKBrr ECX, MM7
  68. // 2nd instruction: MMX_MOVD64rr MM7, ECX
  69. // or instruction: MMX_MOVD64to64rr MM7, ECX
  70. // or instruction: MMX_PINSRWrr MM7, MM7, ECX, 1
  71. SERIAL_VIA_NON_MEMORY_INSTR = 1u << 4,
  72. // The execution is always parallel because the instruction is missing Use or
  73. // Def operands.
  74. ALWAYS_PARALLEL_MISSING_USE_OR_DEF = 1u << 5,
  75. // The execution can be made parallel by repeating the same instruction but
  76. // making sure that Defs of one instruction do not alias with Uses of the
  77. // second one.
  78. PARALLEL_VIA_EXPLICIT_REGS = 1u << 6,
  79. LLVM_MARK_AS_BITMASK_ENUM(/*Largest*/ PARALLEL_VIA_EXPLICIT_REGS)
  80. };
  81. // Returns whether Execution is one of the values defined in the enum above.
  82. bool isEnumValue(ExecutionMode Execution);
  83. // Returns a human readable string for the enum.
  84. StringRef getName(ExecutionMode Execution);
  85. // Returns a sequence of increasing powers of two corresponding to all the
  86. // Execution flags.
  87. ArrayRef<ExecutionMode> getAllExecutionBits();
  88. // Decomposes Execution into individual set bits.
  89. SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode);
  90. LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
  91. // A CodeTemplate is a set of InstructionTemplates that may not be fully
  92. // specified (i.e. some variables are not yet set). This allows the
  93. // SnippetGenerator to instantiate it many times with specific values to study
  94. // their impact on instruction's performance.
  95. struct CodeTemplate {
  96. CodeTemplate() = default;
  97. CodeTemplate(CodeTemplate &&); // default
  98. CodeTemplate &operator=(CodeTemplate &&); // default
  99. CodeTemplate clone() const;
  100. ExecutionMode Execution = ExecutionMode::UNKNOWN;
  101. // See InstructionBenchmarkKey.::Config.
  102. std::string Config;
  103. // Some information about how this template has been created.
  104. std::string Info;
  105. // The list of the instructions for this template.
  106. std::vector<InstructionTemplate> Instructions;
  107. // If the template uses the provided scratch memory, the register in which
  108. // the pointer to this memory is passed in to the function.
  109. unsigned ScratchSpacePointerInReg = 0;
  110. #if defined(__GNUC__) && (defined(__clang__) || LLVM_GNUC_PREREQ(8, 0, 0))
  111. // FIXME: GCC7 bug workaround. Drop #if after GCC7 no longer supported.
  112. private:
  113. #endif
  114. CodeTemplate(const CodeTemplate &); // default
  115. CodeTemplate &operator=(const CodeTemplate &); // default
  116. };
  117. } // namespace exegesis
  118. } // namespace llvm
  119. #endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H