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