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