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