1 //===-- CodeTemplate.h ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// A set of structures and functions to craft instructions for the
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
18 #include "MCInstrDescView.h"
19 #include "llvm/ADT/BitmaskEnum.h"
24 // A template for an Instruction holding values for each of its Variables.
25 struct InstructionTemplate
{
26 InstructionTemplate(const Instruction
*Instr
);
28 InstructionTemplate(const InstructionTemplate
&); // default
29 InstructionTemplate
&operator=(const InstructionTemplate
&); // default
30 InstructionTemplate(InstructionTemplate
&&); // default
31 InstructionTemplate
&operator=(InstructionTemplate
&&); // default
33 unsigned getOpcode() const;
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;
38 bool hasImmediateVariables() const;
39 const Instruction
&getInstr() const { return *Instr
; }
40 ArrayRef
<MCOperand
> getVariableValues() const { return VariableValues
; }
42 // Builds an MCInst from this InstructionTemplate setting its operands
43 // to the corresponding variable values. Precondition: All VariableValues must
48 const Instruction
*Instr
;
49 SmallVector
<MCOperand
, 4> VariableValues
;
52 enum class ExecutionMode
: uint8_t {
54 // The instruction is always serial because implicit Use and Def alias.
55 // e.g. AAA (alias via EFLAGS)
56 ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS
= 1u << 0,
58 // The instruction is always serial because one Def is tied to a Use.
59 // e.g. AND32ri (alias via tied GR32)
60 ALWAYS_SERIAL_TIED_REGS_ALIAS
= 1u << 1,
62 // The execution can be made serial by inserting a second instruction that
63 // clobbers/reads memory.
65 SERIAL_VIA_MEMORY_INSTR
= 1u << 2,
67 // The execution can be made serial by picking one Def that aliases with one
69 // e.g. VXORPSrr XMM1, XMM1, XMM2
70 SERIAL_VIA_EXPLICIT_REGS
= 1u << 3,
72 // The execution can be made serial by inserting a second instruction that
73 // uses one of the Defs and defs one of the Uses.
75 // 1st instruction: MMX_PMOVMSKBrr ECX, MM7
76 // 2nd instruction: MMX_MOVD64rr MM7, ECX
77 // or instruction: MMX_MOVD64to64rr MM7, ECX
78 // or instruction: MMX_PINSRWrr MM7, MM7, ECX, 1
79 SERIAL_VIA_NON_MEMORY_INSTR
= 1u << 4,
81 // The execution is always parallel because the instruction is missing Use or
83 ALWAYS_PARALLEL_MISSING_USE_OR_DEF
= 1u << 5,
85 // The execution can be made parallel by repeating the same instruction but
86 // making sure that Defs of one instruction do not alias with Uses of the
88 PARALLEL_VIA_EXPLICIT_REGS
= 1u << 6,
90 LLVM_MARK_AS_BITMASK_ENUM(/*Largest*/ PARALLEL_VIA_EXPLICIT_REGS
)
93 // Returns whether Execution is one of the values defined in the enum above.
94 bool isEnumValue(ExecutionMode Execution
);
96 // Returns a human readable string for the enum.
97 StringRef
getName(ExecutionMode Execution
);
99 // Returns a sequence of increasing powers of two corresponding to all the
101 ArrayRef
<ExecutionMode
> getAllExecutionBits();
103 // Decomposes Execution into individual set bits.
104 SmallVector
<ExecutionMode
, 4> getExecutionModeBits(ExecutionMode
);
106 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
108 // A CodeTemplate is a set of InstructionTemplates that may not be fully
109 // specified (i.e. some variables are not yet set). This allows the
110 // SnippetGenerator to instantiate it many times with specific values to study
111 // their impact on instruction's performance.
112 struct CodeTemplate
{
113 CodeTemplate() = default;
115 CodeTemplate(CodeTemplate
&&); // default
116 CodeTemplate
&operator=(CodeTemplate
&&); // default
117 CodeTemplate(const CodeTemplate
&) = delete;
118 CodeTemplate
&operator=(const CodeTemplate
&) = delete;
120 ExecutionMode Execution
= ExecutionMode::UNKNOWN
;
121 // See InstructionBenchmarkKey.::Config.
123 // Some information about how this template has been created.
125 // The list of the instructions for this template.
126 std::vector
<InstructionTemplate
> Instructions
;
127 // If the template uses the provided scratch memory, the register in which
128 // the pointer to this memory is passed in to the function.
129 unsigned ScratchSpacePointerInReg
= 0;
132 } // namespace exegesis
135 #endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H