blob: bea10304cba9de28c8446cd858ad2676fe9e50fe [file] [log] [blame]
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +00001//===-- CodeTemplate.h ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Chatelet7f8d3102018-09-26 11:57:24 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000010/// A set of structures and functions to craft instructions for the
11/// SnippetGenerator.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000012///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
16#define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
17
18#include "MCInstrDescView.h"
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000019#include "llvm/ADT/BitmaskEnum.h"
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000020
Fangrui Song32401af2018-10-22 17:10:47 +000021namespace llvm {
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000022namespace exegesis {
23
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000024// A template for an Instruction holding values for each of its Variables.
25struct InstructionTemplate {
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010026 InstructionTemplate(const Instruction *Instr);
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000027
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 Courbet50cdd562019-10-09 11:58:42 +000034 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 Chatelet70ac0192018-09-27 09:23:04 +000038 bool hasImmediateVariables() const;
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010039 const Instruction &getInstr() const { return *Instr; }
40 ArrayRef<MCOperand> getVariableValues() const { return VariableValues; }
Roman Lebedev6030fe02020-02-12 20:54:39 +030041 void setVariableValues(ArrayRef<MCOperand> NewVariableValues) {
42 assert(VariableValues.size() == NewVariableValues.size() &&
43 "Value count mismatch");
44 VariableValues.assign(NewVariableValues.begin(), NewVariableValues.end());
45 }
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000046
Clement Courbet50cdd562019-10-09 11:58:42 +000047 // Builds an MCInst from this InstructionTemplate setting its operands
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000048 // to the corresponding variable values. Precondition: All VariableValues must
49 // be set.
Clement Courbet50cdd562019-10-09 11:58:42 +000050 MCInst build() const;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000051
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010052private:
53 const Instruction *Instr;
Clement Courbet50cdd562019-10-09 11:58:42 +000054 SmallVector<MCOperand, 4> VariableValues;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000055};
56
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000057enum 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.
99bool isEnumValue(ExecutionMode Execution);
100
101// Returns a human readable string for the enum.
Clement Courbet50cdd562019-10-09 11:58:42 +0000102StringRef getName(ExecutionMode Execution);
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000103
104// Returns a sequence of increasing powers of two corresponding to all the
105// Execution flags.
Clement Courbet50cdd562019-10-09 11:58:42 +0000106ArrayRef<ExecutionMode> getAllExecutionBits();
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000107
108// Decomposes Execution into individual set bits.
Clement Courbet50cdd562019-10-09 11:58:42 +0000109SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode);
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000110
111LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
112
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000113// 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 Chateletfcbb6f32018-10-17 11:37:28 +0000115// SnippetGenerator to instantiate it many times with specific values to study
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000116// their impact on instruction's performance.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000117struct 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 Chateletfcbb6f32018-10-17 11:37:28 +0000125 ExecutionMode Execution = ExecutionMode::UNKNOWN;
Clement Courbet49195342019-10-08 09:06:48 +0000126 // See InstructionBenchmarkKey.::Config.
127 std::string Config;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000128 // Some information about how this template has been created.
129 std::string Info;
130 // The list of the instructions for this template.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000131 std::vector<InstructionTemplate> Instructions;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000132 // 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 Chatelet7f8d3102018-09-26 11:57:24 +0000137} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000138} // namespace llvm
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000139
140#endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H